git.schokokeks.org
Repositories
Help
Report an Issue
SS2012-StatisticalMethods.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
8505c07
Branches
Tags
master
SS2012-StatisticalMethods.git
2
solution.r
add solutions for Problem sheet 1 and 2
Michi
commited
8505c07
at 2012-05-16 13:38:38
solution.r
Blame
History
Raw
library(LearnBayes) n=seq(0,100, by = 1) p=dpois(n,5) plot(n,p,type='l',main='Poisson',xlab='n',ylab='f(n,Np)',col='blue') p=dpois(n,10) points(n,p,type='l',col='red') p=dpois(n,50) points(n,p,type='l',col='green') legend('topright',legend=c('N=5','N=10','N=50'),lty=c(1,1,1),col=c('blue','red','green')) #### c=seq(0,10,by=0.01) e=dexp(c,1./0.5) plot(c,e,type='l',main='Exponential Distribution',xlab='x',ylab='f(x,xi)',col=blue) e=dexp(c,1./2) points(c,e,type='l',col='red') e=dexp(c,1./4) points(c,e,type='l',col='green') legend('topright',legend=c('0.5','2','4'),lty=c(1,1,1),col=c('blue','red','green')) #### n2=seq(0,20, by = 1) b=dbinom(n2,5,0.5) plot(n2,b,type='h',main='Binomial Distribution',xlab='n',ylab='f(n;N,p)',col='black') b=dbinom(n2,10,0.5) points(n2+0.1,b,type='h',col='red') b=dbinom(n2,20,0.5) points(n2+0.2,b,type='h',col='green') b=dbinom(n2,20,0.8) points(n2+0.3,b,type='h',col='blue') b=dbinom(n2,20,0.2) points(n2+0.4,b,type='h',col='cyan') legend('topright',legend=c('(5,0.5)','(10,0.5)','(20,0.5)','(20,0.8)','(20,0.2)'),lty=c(1,1,1,1,1),col=c('black','red','green','blue','cyan')) #To fix #### x3=seq(0,10,by=0.01) l=dlnorm(x3,0,0.5) plot(x3,l,type='l',main='Log-Normal Dist.',xlab='x',ylab='f(x;mu,sigma)') l=dlnorm(x3,0,1) points(x3,l,type='l',col='green') l=dlnorm(x3,2,1) points(x3,l,type='l',col='red') legend('topright',legend=c('mu=0 ,sigma=0.5','mu=0 ,sigma=1','mu=2 ,sigma=1'),lty=c(1,1,1),col=c('black','green','red')) #ToDO x=seq(-10,10,by = 0.1) gg=dnorm(x,2,1) g=rnorm(100,2,1) mu=mean(g) med=median(g) sig=sd(g) hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density') lines(x,gg,type='l',col='blue') legend('topleft',legend='N = 100') legend('topright',legend=c(med,mu,sig)) g=rnorm(1000,2,1) mu=mean(g) med=median(g) sig=sd(g) hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density') lines(x,gg,type='l',col='blue') legend('topleft',legend='N = 1000') legend('topright',legend=c(med,mu,sig)) ##### g=rnorm(10000,2,1) mu=mean(g) med=median(g) sig=sd(g) hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density') lines(x,gg,type='l',col='blue') legend('topleft',legend='N = 10000') legend('topright',legend=c(med,mu,sig)) ###### # copy paste x2=seq(-10000,10000, by = 0.1) dn2=dnorm(x2,2,sqrt(2)) p=rpois(1000,2) hist(p,freq=FALSE,xlab='n',ylab='Prob. Density') lines(x2,dn2,type='l',col='blue') legend('topright',legend=c('Np=2','Gaussian'),col=c('black','blue'),lty=c(1,1)) ###### dn2=dnorm(x2,5,sqrt(5)) p=rpois(1000,5) hist(p,freq=FALSE,xlab='n',ylab='Prob. Density') lines(x2,dn2,type='l',col='blue') legend('topleft',legend=c('Np=5','Gaussian'),col=c('black','blue'),lty=c(1,1)) ##### dn2=dnorm(x2,10,sqrt(10)) p=rpois(1000,10) hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density') lines(x2,dn2,type='l',col='blue') legend('topright',legend=c('Np=10','Gaussian'),col=c('black','blue'),lty=c(1,1)) ##### dn2=dnorm(x2,100,sqrt(100)) p=rpois(1000,100) hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density') lines(x2,dn2,type='l',col='blue') legend('topright',legend=c('Np=100','Gaussian'),col=c('black','blue'),lty=c(1,1)) #### dn2=dnorm(x2,1000,sqrt(1000)) p=rpois(1000,1000) hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density') lines(x2,dn2,type='l',col='blue') legend('topright',legend=c('Np=1000','Gaussian'),col=c('black','blue'),lty=c(1,1)) yy = rnorm(10000,0,1) x=seq(0,10, by = 0.1) l = dlnorm(x,0,1) xx=exp(yy) hist(xx,breaks=100,freq=FALSE,main='Distribution of a Log-Normal Sample',xlab='x',ylab='Prob. Density',xlim=c(0,10),ylim=c(0,0.7)) lines(x,l,type='l',col='blue') legend('topright',legend=c('exp(Random Sample)','Log-Normal Dist.'),col=c('black','blue'),lty=c(1,1))