Example #1
0
def test_G_explosive_root(f_arma32):
    """
    Test how to handle explosive root in Smoother
    """
    # Initialize the model
    x = 1  # used to calculate stationary mean
    model = BCM()
    model.set_f(f_arma32)
    theta_intend = np.array([0.1, 0.2, 0.25])
    theta_test = np.array([0.59358299, 0.91708496, 0.54634604])
    T = 250
    my_ft = lambda theta, T, **kwargs: ft(theta, f_arma32, T, **kwargs)
    x_col = ['const']
    Xt = pd.DataFrame({x_col[0]: x * np.ones(T)})

    # Build simulated data
    df, y_col, xi_col = model.simulated_data(input_theta=theta_intend, Xt=Xt)
    Xt = df_to_tensor(df, x_col)
    Yt = df_to_tensor(df, y_col)
    kf = Filter(my_ft, Yt, Xt, for_smoother=True)
    kf.fit(theta_test)
    ks = Smoother()
    ks.fit(kf)
    result = ks.G(theta_test)
    assert not np.isnan(result)
Example #2
0
def test_simulated_data_no_ft(scipy_solver):
    """
    Test error when no ft
    """
    model = BCM()
    model.set_solver(scipy_solver)
    with pytest.raises(ValueError) as error:
        model.simulated_data(np.array([2]))
    expected_result = 'Model needs ft'
    result = str(error.value)
    assert result == expected_result
Example #3
0
def test_init_override(scipy_solver, f_ar1, df_Y):
    init_pred = {
        'P_star_t': 9 * np.ones([1, 1]),
        'xi_t': 60 * np.ones([1, 1]),
        'q': 0
    }
    init_fit = {'P_star_t': 3 * np.ones([1, 1]), 'xi_t': 80 * np.ones([1, 1])}
    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    model.set_solver(scipy_solver)
    theta = np.array([1.1, 0.5, 0.1, 0.3])
    y_col = list(df_Y.columns)
    theta_init = theta.copy()
    model.fit(df_Y, theta_init, y_col=y_col, init_state=init_fit)
    df_out = model.predict(df_Y, init_state=init_pred)
    ks = model.ks_fitted

    # Test fit values
    np.testing.assert_array_equal(ks.xi_t[0][0], np.array([[80]]))
    np.testing.assert_array_equal(ks.P_star_t[0][0], np.array([[3]]))

    # Test predict values
    expected_xi_1_0 = 60
    expected_P_1_0 = 9
    assert expected_xi_1_0 == df_out.loc[0, 'xi_0_filtered']
    assert expected_P_1_0 == df_out.loc[0, 'P_0_filtered']
Example #4
0
def test_set_f_reset_things(scipy_solver, f_ar1, df_Y):
    """
    Test if provide a new ft, reset  
    """
    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    assert model.theta_opt is None

    # Fit model, self.theta_opt should have values
    model.set_solver(scipy_solver)
    theta = np.array([1.1, 0.5, 0.1, 0.3])
    y_col = list(df_Y.columns)
    theta_init = theta.copy()
    model.fit(df_Y, theta_init, y_col=y_col)
    assert model.theta_opt is not None

    # Reset for set_f
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    assert model.theta_opt is None
Example #5
0
def test_simulated_data_no_theta(scipy_solver, f_ar1):
    """
    Test error when no theta
    """
    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    model.set_solver(scipy_solver)
    with pytest.raises(ValueError) as error:
        model.simulated_data()
    expected_result = 'Model needs theta'
    result = str(error.value)
    assert result == expected_result
Example #6
0
def test_ft_kwargs_flow(scipy_solver, f_ar1, df_Y):
    """
    Test whether first value of filtered var
    """
    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    model.set_solver(scipy_solver)
    theta = np.array([1.1, 0.5, 0.1, 0.3])
    y_col = list(df_Y.columns)
    theta_init = theta.copy()
    model.fit(df_Y, theta_init, y_col=y_col)
    df_out = model.predict(df_Y)

    expected_xi_1_0 = 10
    expected_P_1_0 = 4

    assert expected_xi_1_0 == df_out.loc[0, 'xi_0_filtered']
    assert expected_P_1_0 == df_out.loc[0, 'P_0_filtered']
Example #7
0
def test_break_point(scipy_solver, f_ar1, df_Y):

    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    model.set_solver(scipy_solver)
    theta = np.array([1.1, 0.5, 0.1, 0.3])
    y_col = list(df_Y.columns)
    theta_init = theta.copy()

    # split data in to trian and test
    len_df = df_Y.shape[0]
    cutoff = len_df // 2
    df_train = df_Y.loc[df_Y.index < cutoff].copy()
    df_test = df_Y.loc[df_Y.index >= cutoff].copy()

    # Train model
    model.fit(df_train, theta_init, y_col=y_col)
    df_out1 = model.predict(df_Y)
    df_out2 = model.predict_t(df_test, t_index=-1)

    result1 = df_out1.loc[len_df - 1, 'xi_0_filtered']
    result2 = df_out2.loc[len_df - 1, 'xi_0_filtered']

    assert result1 == result2
Example #8
0
def test_simulated_data_init_state(scipy_solver, f_ar1):
    """
    Test if init_state value works for simulated_data
    """
    init_state = {'xi_t': 100 * np.ones([1, 1]), 'P_star_t': np.zeros([1, 1])}

    theta = 0.1 * np.ones(4)
    model = BCM()
    model.set_f(f_ar1, xi_1_0=np.array([[10]]), P_1_0=np.array([[4]]))
    model.set_solver(scipy_solver)
    df, _, _ = model.simulated_data(theta, T=4, init_state=init_state)
    result = np.array([df.loc[0, ['xi_0']]]).T
    expected_result = 100 * np.ones([1, 1])
    np.testing.assert_array_equal(result, expected_result)
Example #9
0
        print('theta is {}. Function value is: {}.'.format(x, obj_func(x)))

    callbackf = None
    if verbose:
        callbackf = disp_f
    res = minimize(obj_, param, callback=callbackf, **kwargs)
    theta_opt = np.array(res.x)
    fval_opt = res.fun
    return theta_opt, fval_opt


# In[14]:

# Initialize the model
x = 1  # used to calculate stationary mean
model = BCM()
model.set_f(my_f, x_0=x * np.ones([1, 1]))
model.set_solver(my_solver,
                 method='nelder-mead',
                 options={
                     'xatol': 1e-8,
                     'maxfev': 200
                 },
                 verbose=False)

# # Generate Synthetic Data
# Same as the standard setup, but I cross off some measurements during training period and see how `linkalman` handles them. I generate some partial missing data for each of the measurements.

# In[15]:

# Some initial parameters