def test_ODE_stdev():
    """
    Make sure that parameters from ODEModels get standard deviations.
    """
    x, v, t = sf.variables('x, v, t')
    k = sf.Parameter(name='k')

    k.min = 0
    k.value = 10
    a = -k * x

    model = sf.ODEModel({
        sf.D(v, t): a,
        sf.D(x, t): v,
    },
                        initial={
                            v: 0,
                            x: 1,
                            t: 0
                        })
    t_data = np.linspace(0, 10, 150)
    noise = np.random.normal(1, 0.05, t_data.shape)
    x_data = model(t=t_data, k=11).x * noise
    v_data = model(t=t_data, k=11).v * noise
    fit = sf.Fit(model, t=t_data, x=x_data, v=v_data)
    result = fit.execute()
    assert result.stdev(k) is not None
    assert np.isfinite(result.stdev(k))
Esempio n. 2
0
def test_harmonic_oscillator_errors():
    """
    Make sure the errors produced by fitting ODE's are the same as when
    fitting an exact solution.
    """
    x, v, t = sf.variables('x, v, t')
    k = sf.Parameter(name='k', value=100)
    m = 1
    a = -k/m * x
    ode_model = sf.ODEModel({sf.D(v, t): a,
                             sf.D(x, t): v},
                            initial={t: 0, v: 0, x: 1})

    t_data = np.linspace(0, 10, 250)
    np.random.seed(2)
    noise = np.random.normal(1, 0.05, size=t_data.shape)
    x_data = ode_model(t=t_data, k=100).x * noise

    ode_fit = sf.Fit(ode_model, t=t_data, x=x_data, v=None)
    ode_result = ode_fit.execute()

    phi = 0
    A = 1
    model = sf.Model({x: A * sf.cos(sf.sqrt(k/m) * t + phi)})
    fit = sf.Fit(model, t=t_data, x=x_data)
    result = fit.execute()

    assert result.value(k) == pytest.approx(ode_result.value(k), 1e-4)
    assert result.stdev(k) == pytest.approx(ode_result.stdev(k), 1e-2)
    assert result.stdev(k) >= ode_result.stdev(k)
Esempio n. 3
0
ode_model = {}
for country in countries:
    t, S, I, R, D = sf.variables("t, S, I, R, D")
    p_susceptible = 0.00085
    N_mu = populations[country] * p_susceptible
    β_0, γ_0, μ_0 = 0.35, 0.1, 0.03
    N_0 = N_mu
    β = sf.Parameter("β", value=β_0, min=0.1, max=0.5)
    γ = sf.Parameter("γ", value=γ_0, min=0.01, max=0.2)
    N = sf.Parameter("N", value=N_0, min=1e4, max=1e7)
    μ = sf.Parameter("μ", value=μ_0, min=0.0001, max=0.1)

    print(country, N_0)

    model_dict = {
        sf.D(S, t): -β * I * S / N,
        sf.D(I, t): β * I * S / N - γ * I - μ * I,
        sf.D(R, t): γ * I,
        sf.D(D, t): μ * I,
    }

    p_infected = 0.01
    I_0, R_0, D_0 = N_mu * p_infected, N_mu * p_infected - 100.0, 100.0
    S_0 = N_mu - I_0 - R_0 - D_0
    ode_model[country] = sf.ODEModel(model_dict,
                                     initial={
                                         t: 0.0,
                                         S: S_0,
                                         I: I_0,
                                         R: R_0,
                                         D: D_0