How to plot a factorial function in R -


i'm trying plot:

enter image description here

using following r code no success:

n= seq(from=150, to=2000) p=((factorial(60) / factorial(50))*(factorial(n-60) /factorial(n-150))) /(factorial(n) /factorial(n-100)) plot(n,p) 

almost always, probability expression involving factorial result of "n choose k" computation:

enter image description here

but inefficient compute via factorial, , importantly, not numerically stable. have @ code using factorial(): got nan.

in r, choose(n, k) function computes "n choose k" fast , stably.

now, careful inspection of given formulation shows equivalent to:

choose(n-100, 50) / choose(n, 60) 

so, can do:

p <- choose(n-100, 50) / choose(n, 60) plot(n, p, type = "l") 

enter image description here


follow-up

hi, efficient function. mean, mode, , median of plot doesn't match ones have in course materials same plot? mean should 727, mode= 600, median= 679!! how can these descriptives suggested plot?

i confused course material trying do. probability give conditional probability p(d | n), i.e., probability random variable d. while sketch p against n. hence, plot above not probability mass function! then, how can use compute statistics mean, mode , median, random variable n???

well anyway, since ask , insist on getting answer, let's pretend probability mass function random variable n. since not true one, sum(p) not or close 1. have sum(p) = 3.843678e-12. so, use proper probability mass function, need normalize first.

p <- p / sum(p) 

now p sum 1.

to compute mean, do

sum(n * p) # [1] 726.978 

to compute mode, do

n[which.max(p)] # 599 

to compute median, do

n[which(cumsum(p) > 0.5)[1]] # 679 

Comments