0

Here is my code.

forw<-function(x,Pi,delta) 
{n <- length(x)
m <- 2 
nu<- matrix(0, nrow = n, ncol = m)

#Initialisation
nu[1, ] <- delta[,x[1]]/sum(delta[,x[1]]) 
########
  for (i in 2:n)
{
  nu[i, ] <- (nu[i - 1, ] %*% Pi[(2*x[i-1]-1):(2*x[i-1]),(2*x[i]-1):(2*x[i])])/sum(nu[i - 1, ] %*% Pi[(2*x[i-1]-1):(2*x[i-1]),(2*x[i]-1):(2*x[i])])
   }
return(nu); 
}
#####################
#DATA INPUT
x<-c(2,1,2,1,2,2,1,2,2,1,2,2,1,2,2,1,2,1,2,1,2,2,1,2,2,1,2,2,1,2,2,1)   
p=0.3
q=1-p
lambda1=1
lambda2=0
mju1=0
mju2=1
Pi=matrix(c(p*lambda1,p*(1-lambda1),p*(1-lambda1),1+p*lambda1-2*p,q*mju1,p-q*mju1, q*(1-mju1),1+q*mju1-p-q,p*lambda2,q-p*lambda2,p*(1-lambda2),1+p*lambda2-q-p,q*mju2,q*(1-mju2),q*(1-mju2),1+q*mju2-2*q),nrow=4, byrow=T, dimnames = list(c("11","21","12","22"), c("11","21","12","22")))
delta=matrix(rep(0.25,4), nrow=2, byrow=T)
####################################
alpha=forw(x,Pi,delta)

As you know, in R, if there are several max values, R chooses the first one. Since in my example delta, the uniform initial distribution, leads to all alphas being the same, the function output should be reflected as a sequence of 1’s independently of the observations.

However, as you'll see, the function output does not consist of 1's only.

The problem arises because at some point an error starts occurring although in practice alphas should be the same.

E.g., the forward probabilities look as follows:

print(alpha, digits=12)
      [,1] [,2]
 [1,]  0.5  0.5
 [2,]  0.5  0.5
 [3,]  0.5  0.5
 [4,]  0.5  0.5
 [5,]  0.5  0.5
 [6,]  0.5  0.5
 [7,]  0.5  0.5
 [8,]  0.5  0.5
 [9,]  0.5  0.5
[10,]  0.5  0.5
[11,]  0.5  0.5
[12,]  0.5  0.5
[13,]  0.5  0.5
[14,]  0.5  0.5
[15,]  0.5  0.5
[16,]  0.5  0.5
[17,]  0.5  0.5
[18,]  0.5  0.5
[19,]  0.5  0.5
[20,]  0.5  0.5
[21,]  0.5  0.5
[22,]  0.5  0.5
[23,]  0.5  0.5
[24,]  0.5  0.5
[25,]  0.5  0.5
[26,]  0.5  0.5
[27,]  0.5  0.5
[28,]  0.5  0.5
[29,]  0.5  0.5

The columns look identical to 12 digits, however, the following output contradicts that. (In theoretical calculations all alphas are identical).

alpha[,1]-alpha[,2]
 [1]  0.000000e+00  0.000000e+00  0.000000e+00
 [4]  0.000000e+00  0.000000e+00  0.000000e+00
 [7]  0.000000e+00  0.000000e+00  0.000000e+00
[10]  0.000000e+00  0.000000e+00  0.000000e+00
[13]  0.000000e+00  0.000000e+00  0.000000e+00
[16]  0.000000e+00  0.000000e+00  0.000000e+00
[19] -1.110223e-16 -3.330669e-16 -3.885781e-16
[22] -4.996004e-16 -6.106227e-16 -7.216450e-16
[25] -8.881784e-16 -1.110223e-15 -1.332268e-15
[28] -1.498801e-15 -1.609823e-15

Question: how can these differences be eliminated? Since they appear for the reason that is unclear to me I am now worried about the extent I can trust R outputs at all.

Lola
  • 97
  • 4
  • 11
  • 2
    I take it you want these calculations to be done exactly (as in, with no approximation whatsoever)? If so, R is probably not the tool you want for the job; see `sin(pi)` for an example. You may want to investigate something more like Mathematica or Maple to accomplish whatever you're trying to do if it's really important to have exact answers. Of course, there may be a package that permits this sort of thing; I don't know, because I've never needed R to work to this level of specificity. – Aaron Montgomery Jul 04 '20 at 17:38
  • 1
    Use `round(alpha, 10)` to round to 10 digits. – G. Grothendieck Jul 04 '20 at 19:03
  • @G. Grothendieck: thank you very much! Any ideas why this is happening at all? My calculations are so basic. I wasn't expecting such issues to arise. – Lola Jul 04 '20 at 20:47
  • 1
    You are not going to get exact answers from floating point calculations. That is how floating point works in all computer languages. – G. Grothendieck Jul 04 '20 at 20:53
  • @G. Grothendieck: But why are the first 18 entries exact then? – Lola Jul 05 '20 at 11:07
  • 1
    All these answers are right to 12 decimal places; the ones that show a difference don't show a difference until 15 or more places beyond the decimal. The earlier differences just happened to be close enough to round to 0, either in the floating point computations or in whatever rounding was used to display those differences. But all computations are done as floating point computations under the hood. – Aaron Montgomery Jul 05 '20 at 19:09

0 Answers0