knitr::opts_chunk$set(echo = TRUE)

From:

Dale, R. & Bhat, H. S. (in press). Equations of mind: Data science for inferring nonlinear dynamics of socio-cognitive systems. Cognitive Systems Research.

Here we use SINDy to reconstruct the two-well attractor model described in Tuller et al. (1994) and Duran and Dale (2012). First, here's the potential of this system, V, over which the system variable fluctuates:

library(sindyr)

x = seq(from=-2,to=2,by=.01)
V = (0*x - x^2/2 + x^4/4)
plot(x,V,xlab='x',type='l',lwd=2)

Now let's run the attractor model over this potential, collecting 100 trials. In each trial, the system variable (x) accumulates like a drift-diffusion model until it reaches an absolute summed threshold (10)

deets = c()
noise = 0.01
set.seed(666)
for (i in 1:100) { # 100 trials of the run
  x_trial = c()
  x = 0
  k = runif(1)-.5
  x_sum = 0
  for (j in 1:1000) { # run a while until it hits threshold (almost always much shorter of course)
    x_trial = rbind(x_trial,data.frame(k=k,x=x,x_sum=x_sum))
    x = x + (-k + x - x^3) + noise*(rnorm(1)) # update state variable
    x_sum = x_sum + x

    if (abs(x_sum)>20) { 
      if (i==1) { # if it's the first run, let's show an example run
        plot(x_trial$x_sum,(1:j)/j,type='l',xlim=c(-21,21),xlab='Cumulative x',ylab='Iteration (max scaled)')
      } else { # otherwise, we add to it
        points(x_trial$x_sum,(1:j)/j,type='l')
      }
      break; # we reached threshold, next trial
    }

  }
  deets = rbind(deets,x_trial)
}
xs=deets[1:(nrow(deets)-1),1:2]
dx=as.matrix(deets[2:nrow(deets),1:2])
non.zeros = dx[,2]!=0&xs[,2]!=0
sindy.obj = sindy(xs=xs[non.zeros,1:2],dx=dx[non.zeros,1:2],lambda=.7,plot.eq.graph=T);
print(sindy.obj$B)


racdale/sindyr documentation built on Sept. 12, 2023, 12:20 p.m.