Probability distribution function for Binomal(5,0.3)
In [35]:
x=rbinom(1000,5,0.3)
y=dbinom(x,5,0.3)
x is sample of 1000 draws from the Bernoulli distribution with parameter 0.3
In [36]:
x
Out[36]:
We can summarize it in terms of counts or proportions:
In [37]:
barplot(table(x))
In [38]:
barplot(table(x)/sum(table(x)))
Now let's plot the likelihood $L = p^3 (1-p)^2$ for $n=5$ and $x=2$.
In [39]:
p = seq(from=0,to=1,by=.01)
L = p^3*(1-p)^2
plot(p,L, type="l")
title(xlab="p",ylab="L(p ;x)", cex=0.8)
and plot the loglikelihoods for $n=5$, at $x=0$, $x=1$, and $x=2$.
In [69]:
l=0*log(p)+5*log(1-p)
plot(p,l, type="l")
title(xlab="p",ylab="l(p;x)", cex=0.8)
In [41]:
l=log(p)+4*log(1-p)
plot(p,l, type="l")
title(xlab="p",ylab="l(p;x)", cex=0.8)
In [42]:
l=2*log(p)+3*log(1-p)
plot(p,l, type="l")
title(xlab="p",ylab="l(p;x)", cex=0.8)
In [43]:
##Alternative way of plooting likelihood and loglikelihood
likelhd = function(p) dbinom(2,5,p)
loglik = function(p) dbinom(2,5,p, log=TRUE)
plot(likelhd,0,1,xlab="pi",ylab="L(p)",main="Binomial likelihood, N=5, X=2")
plot(loglik,0,1,xlab="pi",ylab="l(p)",main="Binomial log-likelihood, N=5, X=2")
In [59]:
loglik = function(p) dbinom(500000,1000000,p, log=TRUE)
In [60]:
plot(loglik,0,1,xlab="pi",ylab="l(p)",main="Binomial log-likelihood, N=50, X=2")
In [67]:
x=rbinom(100000,50,0.3)
In [68]:
barplot(table(x))
In [ ]: