Content deleted Content added
No edit summary |
m →Example Tags: repeating characters gettingstarted edit |
||
Line 276:
The calculations above reveal that the most probable weather state on every day except for the third one was "rain." They tell us more than this, however, as they now provide a way to quantify the probabilities of each state at different times. Perhaps most importantly, our value at <math>\mathbf{\gamma_5}</math> quantifies our knowledge of the state vector at the end of the observation sequence. We can then use this to predict the probability of the various weather states tomorrow as well as the probability of observing an umbrella.
The R hard-code for the above example : By- Vishwash Raj Verma
########## INITIALIZING ##############
T<-matrix(c(.7,.3,.3,.7),nrow=2,ncol=2,byrow=TRUE)
B<-matrix(c(.9,.2,.1,.8),nrow=2,ncol=2,byrow=TRUE)
O1<- matrix(c(.9,0,0,.2),nrow=2,ncol=2,byrow=TRUE)
O2<- matrix(c(.9,0,0,.2),nrow=2,ncol=2,byrow=TRUE)
O4<- matrix(c(.9,0,0,.2),nrow=2,ncol=2,byrow=TRUE)
O5<- matrix(c(.9,0,0,.2),nrow=2,ncol=2,byrow=TRUE)
O3<- matrix(c(.1,0,0,.8),nrow=2,ncol=2,byrow=TRUE)
MAX_S=5
A<-array(c(O1,O2,O3,O4,O5), c(2,2,5))
F<-array(0, c(2,1,5))
G<-array(0, c(2,1,5))
H<-array(0, c(2,1,6))
V<-array(c(O1,O2,O3,O4,O5), c(2,2,5))
fp<-matrix(c(.5,.5),nrow=2,ncol=1)
bp<-matrix(c(1,1),nrow=2,ncol=1)
############ FORWARD PROBABILITY CALCULATION ###############################
for (i in 1:5)
{
if( i==1)
{
F[,,i]<-A[,,i]%*%T%*%fp
F[,,i]<-(1/(F[1,1,i]+F[2,1,i]))*F[,,i]
}
else
{
F[,,i]<-A[,,i]%*%T%*%F[,,i-1]
F[,,i]<-(1/(F[1,1,i]+F[2,1,i]))*F[,,i]
}
}
############# BACKWARD PROBABILITY CALCULATION ###############
for (k in 1:5)
{
if( k==1)
{
G[,,k]<-(T%*%V[,,k])%*%bp
G[,,k]<-(1/(G[1,1,k]+G[2,1,k]))*G[,,k]
}
else
{
G[,,k]<-(T%*%V[,,k])%*%G[,,k-1]
G[,,k]<-(1/(G[1,1,k]+G[2,1,k]))*G[,,k]
}
}
#################### SMOOTHED PROBABILITY CALCULATION ###################
k=5
for( j in 1:6)
{
if (j ==1)
{
H[,,j]=fp*G[,,k]
H[,,j]=(1/(H[1,1,j]+H[2,1,j]))*H[,,j]
k=k-1
}
else if (j==6)
{
H[,,j]=F[,,j-1]*bp
H[,,j]=(1/(H[1,1,j]+H[2,1,j]))*H[,,j]
}
else
{
H[,,j]=F[,,j-1]*G[,,k]
H[,,j]=(1/(H[1,1,j]+H[2,1,j]))*H[,,j]
k=k-1
}
}
### After running the code hit F to see Forward calculated values, hit G for backward calculated values and hit H for Smoothed Probability.
==Performance ==
|