FUNCTION Prepared by Rakesh gupta
Question 1: Write a function that returns the absolute value of an integer vector x of length one.
Question 2: Write a function that returns the number of values that are larger than the mean of a vector. You are allowed to use mean().
Question 3: Write a function to get the probability of P(X=x), if X ~ Bin (n , p).
Question 4: Write a function to get the Cumulative probability of X and Y, if X~Bin(n,p) and Y~ Exp(lambda) .
Question 5: Write a function to get value of a, when P(X<a) = p , when X ~ Exp(lambda)
FUNCTION Prepared by Rakesh gupta
Exercise 1
x <- -10
abs <- x
if (x < 0) {
abs = -x
}
cat("The absolute value of ", x, " is ", abs , "\n" )
Solution 2
x <- c(-100, 10, 20, 30, 50, 51, 52, 53, 54, 55)
counter <- 0
mean <- mean(x)
for (i in 1:length(x)){
if(x[i] > mean){
counter <- counter +1
}
}
cat("The number of values that are bigger than the mean is",
counter, "\n")
## The number of values that are bigger than the mean is 7
Solution 3:
db<-function(x,n,p){choose(n,x)*p^(x)*(1-p)^(n-x)}
Db(5,10,0.5)
[1] 0.2460938
Solution 4:
> Cb<-function(x,n,p){ s<-0
+ for(i in 0:x){
+ y<- choose(n,i)*p^(i)*(1-p)^(n-i)
+ s<-s+y
+ }
+ return(s)
+ }
> Cb(5,10,0.5)
[1] 0.6230469
Solution 5:
expin<-function(p,l){(-1/l)*log(1-p)}
> expin(0.5,2)
[1] 0.3465736