add solutions for Problem sheet 1 and 2

Michi commited on 2012-05-16 13:38:38
Zeige 2 geänderte Dateien mit 153 Einfügungen und 0 Löschungen.

• 0000000..f9f201c
• 0000000..f1c5d5d
 ... ... `@@ -0,0 +1,20 @@` 1 `+library(LearnBayes)` 2 `+` 3 `+p = seq(0.05, 0.95, by = 0.1)` 4 `+prior = c(1,5.2,8,7.2,4.6,2.1, 0.7, 0.1, 0.0, 0.0)` 5 `+prior = prior/sum(prior)` 6 `+` 7 `+` 8 `+data = c(11,6)` 9 `+post = pdisc(p, prior, data)` 10 `+round(cbind(p,prior,post),2)` 11 `+` 12 `+par(font.lab=2)` 13 `+par(font.axis=2)` 14 `+plot(p, prior, type = "h", xlab='p',ylab ="probability",lwd=2, ylim=c(0,0.5),xlim=c(0,1))` 15 `+lines(p,post, type="h",lwd=2,lty=2,col="blue")` 16 `+` 17 `+legend("topleft",legend=c("prior","posterior"),lty=c(1,2),lwd=c(2,2),col=c("black","blue"))` 18 `+` 19 `+` 20 `+`
 ... ... `@@ -0,0 +1,133 @@` 1 `+library(LearnBayes)` 2 `+` 3 `+` 4 `+` 5 `+n=seq(0,100, by = 1) ` 6 `+p=dpois(n,5)` 7 `+plot(n,p,type='l',main='Poisson',xlab='n',ylab='f(n,Np)',col='blue')` 8 `+p=dpois(n,10)` 9 `+points(n,p,type='l',col='red')` 10 `+p=dpois(n,50)` 11 `+points(n,p,type='l',col='green')` 12 `+legend('topright',legend=c('N=5','N=10','N=50'),lty=c(1,1,1),col=c('blue','red','green'))` 13 `+####` 14 `+c=seq(0,10,by=0.01)` 15 `+e=dexp(c,1./0.5)` 16 `+plot(c,e,type='l',main='Exponential Distribution',xlab='x',ylab='f(x,xi)',col=blue)` 17 `+e=dexp(c,1./2)` 18 `+points(c,e,type='l',col='red')` 19 `+e=dexp(c,1./4)` 20 `+points(c,e,type='l',col='green')` 21 `+legend('topright',legend=c('0.5','2','4'),lty=c(1,1,1),col=c('blue','red','green'))` 22 `+####` 23 `+n2=seq(0,20, by = 1)` 24 `+b=dbinom(n2,5,0.5)` 25 `+plot(n2,b,type='h',main='Binomial Distribution',xlab='n',ylab='f(n;N,p)',col='black')` 26 `+b=dbinom(n2,10,0.5)` 27 `+points(n2+0.1,b,type='h',col='red')` 28 `+` 29 `+b=dbinom(n2,20,0.5)` 30 `+points(n2+0.2,b,type='h',col='green')` 31 `+` 32 `+b=dbinom(n2,20,0.8)` 33 `+points(n2+0.3,b,type='h',col='blue')` 34 `+` 35 `+b=dbinom(n2,20,0.2)` 36 `+points(n2+0.4,b,type='h',col='cyan')` 37 `+` 38 `+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` 39 `+####` 40 `+x3=seq(0,10,by=0.01)` 41 `+` 42 `+` 43 `+l=dlnorm(x3,0,0.5)` 44 `+plot(x3,l,type='l',main='Log-Normal Dist.',xlab='x',ylab='f(x;mu,sigma)')` 45 `+` 46 `+l=dlnorm(x3,0,1)` 47 `+points(x3,l,type='l',col='green')` 48 `+` 49 `+l=dlnorm(x3,2,1)` 50 `+points(x3,l,type='l',col='red')` 51 `+` 52 `+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'))` 53 `+#ToDO` 54 `+` 55 `+x=seq(-10,10,by = 0.1)` 56 `+gg=dnorm(x,2,1)` 57 `+` 58 `+g=rnorm(100,2,1)` 59 `+mu=mean(g)` 60 `+med=median(g)` 61 `+sig=sd(g)` 62 `+` 63 `+hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density')` 64 `+lines(x,gg,type='l',col='blue')` 65 `+` 66 `+legend('topleft',legend='N = 100')` 67 `+legend('topright',legend=c(med,mu,sig))` 68 `+g=rnorm(1000,2,1)` 69 `+mu=mean(g)` 70 `+med=median(g)` 71 `+sig=sd(g)` 72 `+` 73 `+` 74 `+hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density')` 75 `+lines(x,gg,type='l',col='blue')` 76 `+` 77 `+legend('topleft',legend='N = 1000')` 78 `+legend('topright',legend=c(med,mu,sig))` 79 `+#####` 80 `+` 81 `+g=rnorm(10000,2,1)` 82 `+mu=mean(g)` 83 `+med=median(g)` 84 `+sig=sd(g)` 85 `+hist(g,freq=FALSE,main='Random Sample',xlab='x',ylab='Prob. Density')` 86 `+lines(x,gg,type='l',col='blue')` 87 `+legend('topleft',legend='N = 10000')` 88 `+legend('topright',legend=c(med,mu,sig))` 89 `+######` 90 `+# copy paste` 91 `+x2=seq(-10000,10000, by = 0.1)` 92 `+dn2=dnorm(x2,2,sqrt(2))` 93 `+p=rpois(1000,2)` 94 `+hist(p,freq=FALSE,xlab='n',ylab='Prob. Density')` 95 `+lines(x2,dn2,type='l',col='blue')` 96 `+legend('topright',legend=c('Np=2','Gaussian'),col=c('black','blue'),lty=c(1,1))` 97 `+######` 98 `+` 99 `+dn2=dnorm(x2,5,sqrt(5))` 100 `+p=rpois(1000,5)` 101 `+hist(p,freq=FALSE,xlab='n',ylab='Prob. Density')` 102 `+lines(x2,dn2,type='l',col='blue')` 103 `+legend('topleft',legend=c('Np=5','Gaussian'),col=c('black','blue'),lty=c(1,1))` 104 `+#####` 105 `+` 106 `+dn2=dnorm(x2,10,sqrt(10))` 107 `+p=rpois(1000,10)` 108 `+hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density')` 109 `+lines(x2,dn2,type='l',col='blue')` 110 `+legend('topright',legend=c('Np=10','Gaussian'),col=c('black','blue'),lty=c(1,1))` 111 `+#####` 112 `+` 113 `+dn2=dnorm(x2,100,sqrt(100))` 114 `+p=rpois(1000,100)` 115 `+hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density')` 116 `+lines(x2,dn2,type='l',col='blue')` 117 `+legend('topright',legend=c('Np=100','Gaussian'),col=c('black','blue'),lty=c(1,1))` 118 `+####` 119 `+` 120 `+dn2=dnorm(x2,1000,sqrt(1000))` 121 `+p=rpois(1000,1000)` 122 `+hist(p,freq=FALSE,main='Random Sample',xlab='n',ylab='Prob. Density')` 123 `+lines(x2,dn2,type='l',col='blue')` 124 `+legend('topright',legend=c('Np=1000','Gaussian'),col=c('black','blue'),lty=c(1,1))` 125 `+yy = rnorm(10000,0,1)` 126 `+x=seq(0,10, by = 0.1)` 127 `+l = dlnorm(x,0,1)` 128 `+xx=exp(yy)` 129 `+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))` 130 `+lines(x,l,type='l',col='blue')` 131 `+` 132 `+legend('topright',legend=c('exp(Random Sample)','Log-Normal Dist.'),col=c('black','blue'),lty=c(1,1))` 133 `+` 0 134