Saving SDE data using DataFrames.jl naming of columns

I am simulating an SDE using DifferentialEquations.jl and I am using DataFrames.jl to save the solution data.

I get a data frame with columns named timestamp, value1, value2 and value3.

I would like to use SDEFunction to be able to use the kwarg syms to able to name the elements rather than have value1, value2 and value3.

Further information is found here : SDE Problems

  • syms: the symbol names for the elements of the equation. This should match u0 in size. For example, if u0 = [0.0,1.0] and syms = [:x, :y], this will apply a canonical naming to the values, allowing sol[:x] in the solution and automatically naming values in plots.

And here Saving and Loading Solution Data

If we set syms in the DiffEqFunction, then those names will be used:

f = ODEFunction(f_2dlinear,syms=[:a,:b,:c,:d])
prob = ODEProblem(f,rand(2,2),(0.0,1.0));
sol1 =solve(prob,Euler();dt=1//2^(4));
df = DataFrame(sol1)

However I when I try using SDEFunction and try passing any information such as my function f (and g) it doesn’t work. Namely, as instructed I try passing

!(du,u,p,t) or du = f(u,p,t)

it returns an error. For instance, passing

du = f(u,p,t)

will give the following

ERROR: UndefVarError: u not defined

I don’t understand what you actually did. Show me an MWE. It’s really nothing more than f = SDEFunction(f,g,syms=[:a,:b,:c,:d])

Here is a MWE (without using SDEFunction)

using DifferentialEquations, DataFrames, Plots
u0 = [1.2]
function f(du,u,p,t)
du[1] = 0.1*sin(u[1])
end
function g(du,u,p,t)
du[1] = 1.2
end
prob = SDEProblem(f,g,u0,(0.0,1.0))
sol = solve(prob, dt=0.01, EM())
df = Dataframe(sol)
@show df
Plots.plot(sol)

Now if I were to use SDEFunction method, what should I put?

prob = SDEProblem(SDEFunction(f,g),u0,(0.0,1.0))

I copy and pasted

prob = SDEProblem(SDEFunction(f,g),u0,(0.0,1.0))

Now I have

using DifferentialEquations,DataFrames, Plots
u0 = [1.2]
function f(du,u,p,t)
du[1] = 0.1*sin(u[1])
end
function g(du,u,p,t)
du[1] = 1.2
end
prob = SDEProblem(SDEFunction(f,g),u0,(0.0,1.0))
sol = solve(prob, dt=0.01, EM())
df = DataFrame(sol)
@show df
Plots.plot(sol)

Upon running I get

ERROR: MethodError: no method matching SDEProblem(::SDEFunction{true, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, ::Vector{Float64}, ::Tuple{Float64, Float64})
Closest candidates are:
SDEProblem(::SciMLBase.AbstractSDEFunction, ::Any, ::Any, ::Any) at ~/.julia/packages/SciMLBase/IJbT7/src/problems/sde_problems.jl:124
SDEProblem(::SciMLBase.AbstractSDEFunction, ::Any, ::Any, ::Any, ::Any; kwargs…) at ~/.julia/packages/SciMLBase/IJbT7/src/problems/sde_problems.jl:124
SDEProblem(::Any, ::Any, ::Any, ::Any) at ~/.julia/packages/SciMLBase/IJbT7/src/problems/sde_problems.jl:128

Stacktrace:
[1] top-level scope
@ ~/Param/MWE.jl:9

Oh oops prob = SDEProblem(SDEFunction(f,g),g,u0,(0.0,1.0)) it’s a bit odd that it needs the two but it does. See:

https://diffeq.sciml.ai/stable/types/sde_types/#SciMLBase.SDEProblem

1 Like

Ah, okay got it, bit odd indeed. Thanks for the help.