Home > Blockchain >  summation of choose function in R
summation of choose function in R

Time:12-31

I am trying to create a function, that is identical to the pbinom in R, i.e. the density for a binomial distribution.

My code is the following:

MyPBin <- function(X,n,p) {
  choose(n,X)*p^X*(1-p)^(n-X)
}

which should match the function used in pbinom in R: p(x) = choose(n, x) p^x (1-p)^(n-x)

I am, however, not getting the correct result. I think i have located the reason -- if i take the sum of all the individual choose functions (n=1, n=2, n=3 etc.) i get the same result as pbinom.

So my question is: how can i sum all of the individual choose functions that the function uses, when i run it?

CodePudding user response:

Use sum. These all give the same result. (Note that MyPBin gives the same result as dbinom.)

sum(MyPBin(0:3, 10, .3))
## [1] 0.6496107

sum(dbinom(0:3, 10, .3))
## [1] 0.6496107

pbinom(3, 10, .3)
## [1] 0.6496107
  • Related