DifferentialEquations & DynamicalSystems: Converting output Vector{Vector{Float64}} to StateSpaceSet

I edited the post and added my steps below


I am having compatibility issues with the DynamicalSystems and DifferentialEquations packages.

I have implemented the following rule set using the DifferentialEquations package:

using DifferentialEquations
using Distributions
using DynamicalSystems
using Random

function threePlusOneDimensions!(du, u, p, t)
    Smax, Rₛ, λₛ, τx, P, Rᵦ, λᵦ, L, τᵧ, S, α, β, τz, ζ, λf, τf  = p

    du[1] = (Smax /(1 + exp((Rₛ - u[2]) / λₛ)) - u[1]) / τx
    du[2] = (P / (1 + exp((Rᵦ - u[2]) / λᵦ)) + u[4] * L - u[1] * u[2] - u[3]) / τᵧ
    du[3] = (S * (α * u[1] + β * u[2]) * p[14] - u[3]) / τz
    du[4] = (u[2] - λf * u[4]) / τf
    return nothing

function affect!(integrator)
    integrator.p[14] = clamp(rand(Normal(0, 0.5)), -1.0, 1.0)
    return nothing

cb = PeriodicCallback(affect!, 0.01)

p₀ = [10, 1, 0.1, 14, 10, 1.04, 0.05, 0.2, 14, 4, 0.5, 0.5, 1, 0.2, 1, 720]
u₀ = [0.0, 0.01, 0.0, 0.0]

prob = ODEProblem(threePlusOneDimensions!, u₀, (0.0,12000.0), p₀)
X = solve(prob, Tsit5(), callback=cb, reltol = 1e-9, abstol = 1e-9, saveat = 0.01)

I would like to calculate recurrence statistics on the basis of the trajectory. I have tried implementing this using the trajectory function of the DynamicalSystems package, but I can’t figure out how to use the callback argument with it, if it even supports it. I am now using solve() instead, but this outputs the trajectory I need as a Vector{Vector{Float64}}, while RecurrenceMatrix requires a StateSpaceSet. How can I convert the Vector{Vector{Float64}} to a StateSpaceSet, so that I can calculate the recurrence statistics?

I used the keyword ε explicity for the RecurrenceMatrix function using a symbol copied from the internet, and that’s why it gave the type error (from the RecurrenceMatrix documentation). I tried using similar code @empet gave before, but then it gave a different type error because of the ε-sign. I did not notice this, assuming it was the same error as I mentioned before.

I solved it using the next snippet of code:

matrix = StateSpaceSet(healthy[:,:]')

RecurrenceMatrix(matrix[1:100], 0.1)

where before it was

RecurrenceMatrix(matrix[1:100], ε = 0.1)

instead of that last line.

Then, it works. I was wondering! I heard all this jazz about multiple dispatch. Would it be possible to add a method to RecurrenceMatrix so that it directly works on ODESolutions, so that it converts the type for you?

1 Like

Convert the vector of vectors to a matrix, having as rows the vectors. Below it is the transpose, matrix', that fulfills this condition. Then
the last line of code converts matrix' to a StateSpaceSet.

vecofvecs = [rand(3) for _ in 1:5]
matrix = hcat(vecofvecs...)
ssset = StateSpaceSet(matrix')

Make life simple :sweat_smile: . It’s already represented as a matrix in indexing. Just call Matrix(X') or whatnot.

1 Like