def task_simulation_and_statistics_range_r(depends_on, produces):
    """
    Task for estimating the parameters in interactive fixed effects model by using
    different numbers of factors.
    """
    # set parameters
    simulate = json.loads(depends_on.read_text(encoding="utf-8"))
    dgp_func = globals()[simulate["dgp_func"]]
    all_r = simulate["all_r"]
    np.random.seed(simulate["rng_seed"])
    # Run the monte carlo simulation
    df_sim_range_r = pd.DataFrame()
    df_statistic_range_r = pd.DataFrame()
    for r in all_r:
        df_sim_result = simulation_coefficient(
            dgp_func=dgp_func,
            all_N=simulate["all_N"],
            all_T=simulate["all_T"],
            nsims=simulate["nsims"],
            need_sde=simulate["need_sde"],
            tolerance=simulate["tolerance"],
            r=r,
            interactive_start_value_effect=simulate[
                "interactive_start_value_effect"],
            within_effect=simulate["within_effect"],
            **simulate["beta_true"],
        )
        df_statistic = statistics_coefficient(
            all_N=simulate["all_N"],
            all_T=simulate["all_T"],
            nsims=simulate["nsims"],
            df_sim_result=df_sim_result,
            **simulate["beta_true"],
        )
        df_sim_range_r = df_sim_range_r.append(
            pd.concat(
                [
                    pd.DataFrame({"r": [r] * df_sim_result.shape[0]}),
                    df_sim_result
                ],
                axis=1,
            ))
        df_statistic_range_r = df_statistic_range_r.append(
            pd.concat([
                pd.DataFrame({"r": [r] * df_statistic.shape[0]}), df_statistic
            ],
                      axis=1))
    # Store df_sim_range_r and df_statistic_range_r
    df_sim_range_r = df_sim_range_r.reset_index(drop=True)
    df_sim_range_r.to_csv(produces["sim_result"], index=False)
    df_statistic_range_r = df_statistic_range_r.reset_index(drop=True)
    df_statistic_range_r.to_csv(produces["statistic"], index=False)
Ejemplo n.º 2
0
def task_simulation_coefficient(depends_on, produces):
    simulate = json.loads(depends_on.read_text(encoding="utf-8"))

    np.random.seed(simulate["rng_seed"])
    dgp_func = globals()[simulate["dgp_func"]]
    # Run the monte carlo simulation
    df_sim_result = simulation_coefficient(
        dgp_func=dgp_func,
        all_N=simulate["all_N"],
        all_T=simulate["all_T"],
        nsims=simulate["nsims"],
        need_sde=simulate["need_sde"],
        tolerance=simulate["tolerance"],
        r=simulate["r"],
        interactive_start_value_effect=simulate["interactive_start_value_effect"],
        within_effect=simulate["within_effect"],
        **simulate["beta_true"],
    )
    # Store df_sim_result with locations after each round
    df_sim_result.to_csv(produces, index=False)
def test_simulation_coefficient_model3():
    dgp_fun = dgp_interactive_fixed_effects_model
    all_N = [10, 20]
    all_T = [9, 19]
    nsims = 2
    df_sim_result = simulation_coefficient(
        dgp_fun,
        all_N,
        all_T,
        nsims,
        need_sde=True,
        interactive_start_value_effect="pooling",
        within_effect="twoways",
        beta1=1,
        beta2=3,
        mu=5,
        gamma=2,
        delta=4,
    )
    assert df_sim_result.shape == (4, 15)
    assert not df_sim_result.isna().any(axis=None, skipna=False)
def test_simulation_coefficient_model4():
    dgp_fun = dgp_interactive_fixed_effects_model_with_common_and_time_invariant
    all_N = [10, 20]
    all_T = [9, 19]
    nsims = 2
    df_sim_result = simulation_coefficient(
        dgp_fun,
        all_N,
        all_T,
        nsims,
        need_sde=True,
        interactive_start_value_effect="pooling",
        within_effect="twoways",
        beta1=1,
        beta2=3,
        mu=5,
        gamma=2,
        delta=4,
    )
    assert df_sim_result.shape == (4, 23)
    na_expect = np.full(23, False)
    na_expect[[11, 12, 21, 22]] = True
    np.testing.assert_array_equal(
        df_sim_result.isna().any(axis=0, skipna=False), na_expect)
def test_statistics_coefficient():
    dgp_fun = dgp_interactive_fixed_effects_model_with_common_and_time_invariant
    all_N = [10, 20]
    all_T = [9, 19]
    nsims = 2
    df_sim_result = simulation_coefficient(
        dgp_fun,
        all_N,
        all_T,
        nsims,
        need_sde=True,
        interactive_start_value_effect="pooling",
        within_effect="twoways",
        beta1=1,
        beta2=3,
        mu=5,
        gamma=2,
        delta=4,
    )
    df_statistic = statistics_coefficient(all_N,
                                          all_T,
                                          nsims,
                                          df_sim_result,
                                          beta1=1,
                                          beta2=3,
                                          mu=5,
                                          gamma=2,
                                          delta=4)
    assert df_statistic.shape == (2, 62)
    na_expect = np.full(62, False)
    na_expect[[35, 36, 40, 41, 45, 46, 50, 51, 55, 56, 60, 61]] = True
    np.testing.assert_array_equal(
        df_statistic.isna().any(axis=0, skipna=False), na_expect)
    np.testing.assert_almost_equal(
        df_statistic.loc[0, "mean_interactive.1"],
        df_sim_result.loc[0:1, "beta_interactive.1"].mean(axis=None),
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[1, "mean_interactive.1"],
        df_sim_result.loc[2:3, "beta_interactive.1"].mean(axis=None),
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[0, "bias_interactive.2"],
        abs(df_statistic.loc[0, "mean_interactive.2"] - 3) / 3,
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[1, "sde_within.1"],
        df_sim_result.loc[2:3, "sde_within.1"].mean(axis=None),
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[1, "ci_l_within.1"],
        df_statistic.loc[1, "mean_within.1"] -
        df_statistic.loc[1, "sde_within.1"] * 1.959963984540054,
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[1, "ci_u_within.1"],
        df_statistic.loc[1, "mean_within.1"] +
        df_statistic.loc[1, "sde_within.1"] * 1.959963984540054,
    )
    np.testing.assert_almost_equal(
        df_statistic.loc[0, "rmse_interactive.2"],
        np.sqrt(((df_sim_result.loc[0:1, "beta_interactive.2"] -
                  3)**2).mean(axis=None)),
    )