#binomial distribution
#pdf
dbinom(11,40,0.6)
## [1] 2.417431e-05
barplot(dbinom(0:40,40,0.6),names.arg=0:40,xlab="number of successes",ylab="probability",main="binomial distribution")

#poisson distribution
#probability of no typos in a 300-page book
dpois(0,4*3)
## [1] 6.144212e-06
plot(0:15,dpois(0:15,12),type="b",xlab="number of typos",ylab="probability", main="poisson distribution")

#probability of more than 5 typos
1-ppois(5,12)
## [1] 0.979659
plot(0:15,1-ppois(0:15,12),type="b",xlab="number of typos",ylab="probability",main="poisson distribution")

#run simulations
rpois(100,12)
## [1] 13 17 11 10 17 18 11 11 10 15 12 12 10 7 9 13 7 17 12 18 8 17 12 15 14
## [26] 12 16 11 13 14 12 11 20 7 15 14 6 11 12 15 17 11 16 18 11 18 19 12 14 10
## [51] 15 11 17 10 12 9 9 4 10 12 14 16 10 4 14 12 10 11 11 9 10 16 13 17 8
## [76] 16 9 12 9 12 13 7 7 13 18 8 15 15 22 8 6 12 13 18 15 9 5 16 15 8
hist(100,main="simulated typos",xlab="number of typos",ylab="frequency",col="orange")

#normal distribution
#probability of income less than $10000
pnorm(10000,25000,10000)
## [1] 0.0668072
#probability of income between $20000 and $30000
pnorm(30000,25000,10000)-pnorm(20000,25000,10000)
## [1] 0.3829249
curve(dnorm(x,25000,10000),from=0,to=50000,xlab="family income",ylab="density",main="normal distribution",col="blue",lwd=2)

#exponential distribution
pexp(12,1/8)-pexp(3,1/8)
## [1] 0.4641591
curve(dexp(x,1/8),from=0,to=40,xlab="months",ylab="density",main="exponential distribution",col="orange",lwd=2)

#beta distribution
#mean
(2/6)
## [1] 0.3333333
#variance
(2*4)
## [1] 8
#probability of at least 80% sold
pbeta(0.8,2,4,lower.tail=FALSE)
## [1] 0.00672
curve(dbeta(x,2,4),from=0,to=1,xlab="probability",ylab="density",main="beta distribution",col="blue",lwd=2)
