Assignment 1 Create an R function to calculate factorials.

Preparation

I don’t like to write something in the beautiful code, so I will put the main explanation in the preparation. 1.The concept of factorials \[n!=n*(n-1)*(n-2)...*1\] where\[0!=1\] 2.Discusion about what kinds of mechanism we need At first we should check whether the x we need to deal with is 0 or not.If it was 0, we just output the 1. And then, to calculation of factrials (we call it function(X)) need us to multiply the x and at the same time decrease the x by 1 until 1. And this means we have to loop the caculation. And on the other side, we will need another one (we call it value_factorials) to save the cumulated value we have multiply the x on. So ‘if’ code and ‘while’ code will be used in the function. As we can see, it’s very easy to create the fuction by using the ‘if’ and ‘while’ code. We can also use ‘for’ code. However, the problem will appear latter because this fuction can only deal with the vector whose length is 1.Therefore we can’t use it to get a gragh in assignment 4.To solve this we need the x[i] code and another loop to output all the calculated result in a vector. And put all the result in another vector,then ‘return’ it finally. ##Code Let’s just start writing the code.

my_factorials <- function(x)
  {count<-1
  ##count is used to output all the result in the vector for it will increase through the loop
  v<-NULL
  ##We use v to save all the calculated result
  while(count<=length(x))
  {
 if (x[count]==0)
   ##Check whether it is 0 or not
 {
   v<-c(v ,1)
   ##Save the result to v
 } else
   {
  value_factorials<-1
  ##value_factor is used to save the cumulated value
  while(x[count]>1)
  {
    value_factorials<-value_factorials*x[count]
    x[count]<-x[count]-1
    ##x decrease by 1 through the loop everytime
  }
  v<-c(v,value_factorials)
  ##Save the result to v 1 by 1 through the loop
 }
count<-count+1
  }
  return(v)
}

Let test the new function.

my_factorials(6)
## [1] 720
##First we test whether it can calculate a usual number
b<-(0:2)
my_factorials(b)
## [1] 1 1 2
##Then test whether it can calculate a vector whose length>1
class(my_factorials(6))
## [1] "numeric"
##At last test the class of output

Perfect,let’s deal with the next assignment. #Assignment 2 and 3 Assignment 2 and 3 are piece of cakes if we finish the assignment one.We just write the fuction and put the formula in. Little explanation needed for the steps.

combination_num<-function(n,k)
{
return(
  my_factorials(n)/(my_factorials(k)*my_factorials(n-k))
  ##Write the formula
  )
}
combination_num(4,2)
## [1] 6
##Have a test

bino_distribution<-function(x,n,p)
{
  return(
  combination_num(n,x)*p^x*(1-p)^(n-x)
##Write the formula
  )
}
bino_distribution(2,4,.5)
## [1] 0.375
##have a test

Assignment 4

Assignment is also easy base on the result above.We create a vector x ,and use the code ‘plot’,then put the function in. Let’s set the p=0.5

x<-0:2
plot(x,bino_distribution(x,2,.5))

##when n=2
x<-0:5
plot(x,bino_distribution(x,5,.5))

##when n=5
x<-0:10
plot(x,bino_distribution(x,10,.5))

##when n=10
x<-0:50
plot(x,bino_distribution(x,50,.5))

##when n=50

Ok,type the code like an idiot again and again.Let’s just use ‘for’ to make it easy. we create and vector n which include all the value we need. And use the loop to do it atomatically. Let’s set the p=0.3

n<-c(2,5,10,50)
for(i in n)
{
  x<-0:i
  plot(x,bino_distribution(x,i,.3))
}