Skip to content

examples/example_frac_nodemog module

Executable example demonstrating FRAC estimation without demographics. The example uses simulated data with all X1 variables generated as N(0,1) iid; the user can modify the dimensions and other parameters of the simulation as desired.

run_example(T=50, J=20, n_X1_exo=1, n_X1_endo=1, n_X2_exo=0, n_X2_endo=1, n_Z=1, sigma_x=1.0, sigma_xi=1.0, rho_x_xi=np.sqrt(0.5), rho_x_z=np.sqrt(0.5), betas=np.array([-4.3, 1.0]), sigmas=np.array([1.0]))

Simulates data and estimates it with FRAC without demographics; then repeats the estimation using the real data interface.

Source code in frac_blp/examples/example_frac_nodemog.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def run_example(
    T: int = 50,
    J: int = 20,
    n_X1_exo: int = 1,
    n_X1_endo: int = 1,
    n_X2_exo: int = 0,
    n_X2_endo: int = 1,
    n_Z: int = 1,
    sigma_x: float = 1.0,
    sigma_xi: float = 1.0,
    rho_x_xi: float = np.sqrt(0.5),
    rho_x_z: float = np.sqrt(0.5),
    betas: np.ndarray = np.array([-4.3, 1.0]),
    sigmas: np.ndarray = np.array([1.0]),
):
    """Simulates data and estimates it with FRAC without demographics;
    then repeats the estimation using the real data interface.
    """
    print_stars("Hello from frac_blp!")
    simulated_frac_data = simulate_frac_nodemog_data(
        T=T,
        J=J,
        n_X1_exo=n_X1_exo,
        n_X1_endo=n_X1_endo,
        n_X2_exo=n_X2_exo,
        n_X2_endo=n_X2_endo,
        n_Z=n_Z,
        sigma_x=sigma_x,
        sigma_xi=sigma_xi,
        rho_x_xi=rho_x_xi,
        rho_x_z=rho_x_z,
        betas=betas,
        sigmas=sigmas,
    )
    print_stars("Simulated Data:")
    print(simulated_frac_data)

    print_stars("Estimating with FRAC")
    _, _ = frac_nodemog_estimate(simulated_frac_data, degree_Z=3, degree_X1=3)

    print_stars("Example with the real data interface:")
    df_X1 = pd.DataFrame(
        np.column_stack(
            (
                simulated_frac_data.X1_exo,
                simulated_frac_data.X1_endo,
            )
        ),
        columns=simulated_frac_data.names_X1,
    )
    real_frac_data = FracNoDemogRealData(
        T=simulated_frac_data.T,
        J=simulated_frac_data.J,
        df_X1=df_X1,
        Z=simulated_frac_data.Z,
        names_X1_exo=simulated_frac_data.names_X1_exo,
        names_X1_endo=simulated_frac_data.names_X1_endo,
        names_X2_exo=simulated_frac_data.names_X2_exo,
        names_X2_endo=simulated_frac_data.names_X2_endo,
        shares=simulated_frac_data.shares,
    )
    print_stars("Estimating with FRAC")
    _, _ = frac_nodemog_estimate(real_frac_data, degree_Z=3, degree_X1=3)