# Koopman inference fails for simple example while SINDy works fine

**URL:** <https://discourse.julialang.org/t/koopman-inference-fails-for-simple-example-while-sindy-works-fine/78706>\
**Category:** Modelling & Simulations\
**Tags:** koopman\
**Created:** [March 29, 2022, 11:17pm UTC](https://discourse.julialang.org/t/koopman-inference-fails-for-simple-example-while-sindy-works-fine/78706 "2022-03-29T23:17:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [March 29, 2022, 11:17pm UTC](https://discourse.julialang.org/t/koopman-inference-fails-for-simple-example-while-sindy-works-fine/78706/1 "2022-03-29T23:17:02Z")

</div>

Hi All,

I’m trying to put together a `DataDrivenDiffEq.jl` example for my demo system of an SIR epidemiological model.

```julia
using OrdinaryDiffEq
using DataDrivenDiffEq
using ModelingToolkit

function sir_ode(u,p,t)
    (s,i,r) = u
    (β,γ) = p
    ds = -β*s*i
    di = β*s*i - γ*i
    dr = γ*i
    [ds,di,dr]
end

p = [0.5,0.25]
u0 = [0.99, 0.01, 0.0]
tspan = (0.0, 40.0)
solver = ExplicitRK()
sir_prob = ODEProblem(sir_ode, u0, tspan, p)
sir_sol = solve(sir_prob, solver)

```

I use a sufficient basis to capture the above dynamics.

```julia
dd_prob = ContinuousDataDrivenProblem(sir_sol)
@parameters t
@variables u[1:3](t)
Ψ = Basis([u; u[1]*u[2]], u, independent_variable = t)

```

Koopman inference generates the correct set of equations, but the parameter values are incorrect.

```julia
res_koopman = solve(dd_prob, Ψ, DMDPINV(), digits = 1)
sys_koopman = result(res_koopman)

```

```julia
julia> equations(sys_koopman)
3-element Vector{Equation}:
 Differential(t)(u[1](t)) ~ p₁*u[1](t)*u[2](t)
 Differential(t)(u[2](t)) ~ p₂*u[2](t) + p₃*u[1](t)*u[2](t)
 Differential(t)(u[3](t)) ~ p₄*u[2](t)
julia> parameter_map(res_koopman)
4-element Vector{Pair{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64}}:
 p₁ => -0.5
 p₂ => -0.2
 p₃ => 0.5
 p₄ => 0.2

```

p₂ and p₄ should be -0.25 and 0.25, while the rest of the solution is correct. The true values of all parameters are correctly identified using SINDy.

```julia
res_sindy = solve(dd_prob, Ψ, STLSQ(), digits = 1)
sys_sindy = result(res_sindy)

```

```julia
julia> parameter_map(res_sindy)
4-element Vector{Pair{Sym{Real, Base.ImmutableDict{DataType, Any}}, Float64}}:
 p₁ => -0.5
 p₂ => -0.25
 p₃ => 0.5
 p₄ => 0.25

```

Does anyone have an idea why Koopman inference comes so close, but doesn’t get it correct?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 30, 2022, 11:00am UTC](https://discourse.julialang.org/t/koopman-inference-fails-for-simple-example-while-sindy-works-fine/78706/2 "2022-03-30T11:00:17Z")

</div>

Open an issue and Julius will take a look.
