I was going through the Default Risk and Income Fluctuations Lecture, trying to get the business cycle statistics of the lecture model similar to the one Arellano [Are08] provides in Table 4 of the paper.
From exercise 3, I can simulate 250 draws of the Arellano Economy ae
and obtain an output, a foreign assets, a bond price and a default state vector.
# set random seed for consistent result
srand(348938)
# simulate
T = 250
y_vec, B_vec, q_vec, default_vec = simulate(ae, T)
In the paper Arellano finds 100 default events, extracts 74 observations before the default event, and reports mean statistics from these 100 samples.
Being not very proficient in manipulating data in Julia I first tried to put the vectors into an array
and then convert the array into a DataFrame. From there I can filter by default state but I fail to filter and aggregate the rows before the default state.
sim_array = [y_vec B_vec q_vec default_vec]
df = convert(DataFrame, sim_array)
df[(df[:x4] .== 1),:] # 1 refers to states where the economy is in default
x1 | x2 | x3 | x4 |
---|---|---|---|
1.0231957810795722 | 0.0032 | 0.8923119382988378 | 0.0 |
1.0712139563752547 | -0.064 | 0.8747488101074827 | 0.0 |
1.0712139563752547 | -0.1824 | 0.8747488101074827 | 0.0 |
1.0712139563752547 | -0.1824 | 0.8747488101074827 | 0.0 |
1.0712139563752547 | -0.1824 | 0.8747488101074827 | 0.0 |
0.9783682298832389 | -0.1824 | 0.9832841691248771 | 1.0 |
… | … | … | … |
0.9551740574234442 | 0.0 | 0.3542536600561192 | 0.0 |
0.9335203243465698 | -0.0192 | 0.9832841691248771 | 1.0 |
0.9335203243465698 | 0.0 | 0.9832841691248771 | 1.0 |
0.9123574799747649 | 0.0 | 0.9832841691248771 | 1.0 |
0.9335203243465698 | 0.0 | 0.9832841691248771 | 1.0 |
0.9335203243465698 | 0.0 | 0.37297704451873875 | 0.0 |
How would I filter out the rows before the default event? Did I maneuver myself in a bad spot by converting the arrays to a DataFrame and there exists a better approach?