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))
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)
def test_1_1_model():
    '''Tests the case with 1 component and 1 parameter'''
    x, y = sf.variables('x, y')
    a = sf.Parameter(name='a')
    model = sf.Model({y: 3 * a * x**2})
    x_data = np.arange(10)

    exact = model.eval_jacobian(x=x_data, a=3.5)
    approx = model.finite_difference(x=x_data, a=3.5)
    _assert_equal(exact, approx)

    exact = model.eval_jacobian(x=3, a=3.5)
    approx = model.finite_difference(x=3, a=3.5)
    _assert_equal(exact, approx)
Exemple #4
0
}
sorted_data["100_cases"] = sorted_data.apply(
    lambda x: country_first_dates[x.country], axis=1)
sorted_data["days_since_100_cases"] = (
    sorted_data.index - sorted_data["100_cases"]).apply(lambda x: x.days)

logger.info("Training...")
fit_result = {}
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