Multivariate Statistik - Aufgabe 5 - Diskriminanz-/Hauptkomponentenanalyse

From StatWiki
Jump to: navigation, search

Contents

Beispiel 1

The function to calculate the apriori probability is

p(k|x) = \frac{p(k) \cdot f(x|k)}{f(x)}

where f(x) = \sum p(k) \cdot f(x|k) \,

> library(mvtnorm)
>
> aposteriori = function(x,apriori) {
+         m=matrix(c(0,0,0,1,2,3,3,3,4),nrow=3)
+         s=matrix(c(1,0.5,0.5,0.5,1,0.5,0.5,0.5,1),nrow=3,byrow=T)
+
+         denum=0
+         for(i in 1:ncol(m)) {
+                 denum=denum+dmvnorm(x,m[,i],s)*apriori[i]
+         }
+
+         ret = rep(0,ncol(m))
+
+         for(i in 1:ncol(m)) {
+                 ret[i] = (dmvnorm(x,m[,i],s)*apriori[i]) / denum
+         }
+         ret
+ }</pre>

dmvnorm(...) returns 

f_X(x_1, \dots, x_N)
=
\frac
 {1}
 {(2\pi)^{N/2} \left|\Sigma\right|^{1/2}}
\exp
\left(
 -\frac{1}{2}
 ( x - \mu)^\top \Sigma^{-1} (x - \mu)
\right)
 as found in en:Multivariate normal distribution


For the 3 observations 
x_1 = \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \,, 
x_2 = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \, and 
x_3 = \begin{pmatrix} 3 \\ 3 \\ 4 \end{pmatrix} \, with aprioris p(1) = p(2) = p(3) = \frac{1}{3} \,
the aposterios are given by

Engine.php: unknown attribute(s) "direct"
in

library(mvtnorm) aposteriori(c(0,0,0),rep(1/3,3)) aposteriori(c(1,2,3),rep(1/3,3)) aposteriori(c(0.5,1,1.5),rep(1/3,3))

Therefore x_1 \, is assigned to group 1, x_2 \, is assigned to group 2 and x_3 \, is just between group 1 and 2.


For aprioris p(1) = p(3) = 0.49 \, and p(2) = 0.02 \,. The aposterios are given by

Engine.php: unknown attribute(s) "direct"
in

library(mvtnorm) aposteriori(c(0,0,0),c(0.49,0.02,0.49)) aposteriori(c(1,2,3),c(0.49,0.02,0.49)) aposteriori(c(0.5,1,1.5),c(0.49,0.02,0.49))

Therefore x_1 \, and x_3 \, are assigned to group 1 and x_2 \, is assigned to group 3.