Home > database >  Placing "functionalized index" in R (MATlab code to R)
Placing "functionalized index" in R (MATlab code to R)

Time:09-21

I am trying to translate some code from matlab into R.

If necessary: The full matlab code can be viewed here, STARTIN LINE 46, "Dynamical perception": https://github.com/rssmith33/Active-Inference-Tutorial-Scripts/blob/main/Pencil_and_paper_exercise_solutions.m

BACKGROUND if necessary: pencil and paper example for active inference (similar predictive coding, it is essentially approx. Bayes inference...): https://psyarxiv.com/b4jm6/ at page 135


MY QUESTION:

I am pretty new to this, but I was able to fully replicate the math (essentially approx Bayes) of the matlab script in R without doing a loop, now I want to replicate the loop.

In the matlab code it says:

>% observations = vectors!
o{1,1} = [1 0]';
o{1,2} = [0 0]';
o{2,1} = [1 0]';
o{2,2} = [1 0]';

In the matlab loop it e.g. says:

% likelihood
 lnAo = nat_log(A'*o{t,tau});

So the o{x,y} works as index for the upper o{x,y} vector, or in my case: t (i.e. timestep) and tau:

o{t,tau} where t = timestep {1,} or {2,} and tau is {,1} or {,2} of the upper o{ , }

For calculating the math WITHOUT a loop I used these so far:

# True observation at t-1
o11 = c(1, 0)   
o12 = c(0, 0)

# True observation at t-2
o21 = c(1, 0)       
o22= c(0, 1)   

# I used it for something like this, where I manually added the right o{x,y} to the formula.
st2all = ... ...    ((log(t(A))%*%o22))    

My guess I could either target the last character of my "o12" for tau and the second last for t (time) and define it some way, but how (and does it make sense)?

Or is there an elegant equivalent for this function in R somehow?

Thanks in advance!

CodePudding user response:

Maybe this is something like the cell structure in Matlab

> o <- matrix(list(c(1, 0), c(0, 0), c(1, 0), c(1, 0)), 2, byrow = TRUE)

> o[[1, 1]]
[1] 1 0

> o[[1, 2]]
[1] 0 0

> o[[2, 1]]
[1] 1 0

> o[[2, 2]]
[1] 1 0

CodePudding user response:

Are you looking for a way to return the value of o depending on the values of t and tau, so you can easily put this code into a loop? If so, you could define a function o(t, tau) that returns the values given in the Matlab example above.

Note that t() is the transpose function in R, so I'm using the variable name timestep instead of t. You can define a variable named t but then you won't be able to call the function, and it looks like you'll need to transpose matrices to transalte the rest of your Matlab code.

Here's a function o(timestep, tau) that gives the results you want:

o <- function(timestep,tau){
  # default response is c(1,0)
  result <- c(1,0)
  # response is c(0,0) iff t==1 and tau ==2
  if (timestep == 1 & tau == 2) result <- c(0,0)
  
  return (result)
}

And here's a simplified example of two nested loops that will iterate over all combinations of 1 and 2 for timestep and tau, calling our function o() and doing some simple demonstration math:

# set up a starting value for demonstration
x <- c(1,1)

# loop over all values of 1 and 2 for t and tau
for(timestep in 1:2){
  for (tau in 1:2){

    # each iteration, call our function `o` and do some math
    x <- sqrt(x)   o(timestep,tau)
    
  }
}

The math in your loops will look different of course because you're doing more complicated stuff.

CodePudding user response:

I figured it out the rest via https://www.latecnosfera.com/2016/05/matlab-cell-arrays-equivalent-in-r.html

o <- vector("list", length = 2 * 2)
dim(o) <- c(2, 2)
o

o[[1, 1]] <- c(1, 0)
o[[1, 2]] <- c(0, 0)
o[[2, 1]] <- c(1, 0)
o[[2, 2]] <- c(0, 1)

# This worked so far and the loop shouldn't be too much of a problem now I hope. :)
st2all2 = .....    ((log(t(A) nzlog)%*%o[[2,2]])) 

Thanks for all the help!!

  • Related