Default Risk and Income Fluctuations - Matching the data


#1

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?


#2

Hi @Nicolas_Reigl

Just to make sure I understand right; you trying to get all of the events that preceed or follow the first ‘default event’?

I’m not deeply familiar with Julia, so there’s probably a more elegant way to do this. I would use a DataFrame exactly like you have constructed. The get events preceeding the first default I would select the rows from 1 up until the row before where the default vector first becomes 1. The key is to use find(df[:x4].==1) which returns the positions of the elements of df[:x4] which match the condition.

#subset df from row 1 until the row before the index of the first time default vec is 1
df_before_default = df[1:find(df[:x4].==1)[1]-1,:]

You could get all events following (and including) the first default like so:

df_after_default = df[find(df[:x4].==1)[1]:end,:]

Let me know whether that answers your question :slight_smile:


#3

Hi @Augustus_Hebblewhite

Your proposed solution approach helped a great deal understanding indexing and subsetting DataFrames in Julia but there is maybe a slight misunderstanding in what I’m trying to do, at least I think so based on my reading of Arellano 2008 :wink:

To make things more clear I cite Arellano 2008 page 705:

To make the model business cycle statistics comparable to the data, we choose observations prior to the default events from the limiting distribution of assets. In particular, we simulate the model over time, find 100 default events, extract the 74 observations before the default event, and report mean statistics from these 100 samples.

Footnote: We choose 74 observations prior to a default event to mimic the period length between 1983:III and 2001:IV in Argentina, which constitutes the period between default events

I would have interpreted these steps as:

  1. Sample the Arellano economy 100 times
  2. Extract the last observation prior to a default
  3. Move to the next default event and extract again the last observation prior to default
  4. Continue this throughout the sample
  5. Stack the extracted single last observations and calculate mean statistics from

Its possible that my interpretation is wrong and I have to calculate group means for all periods prior to a default episode or maybe even more simple, average over all non-default events :thinking:

Maybe someone can provide some insight on what basis Arellano calculates the mean statistics from the simulation.

In any case, your function

helps me to find the rows prior to a default. I could, given my interpretation is correct, extract the last row from the df_before_default DataFrame with

df_before_default[end, :]

Now I need to figure out how to do this not only for the first default event but for every consecutive one.

Given my interpretation is wrong I would start in the same why, calculate the group mean of df_before_default, do this every time prior to a default and then average over the (74) group means.

Hope this was more clear.

Edit:
After posting this reply I found out that some of my confusion stemmed from the fact that the table I pasted from Atom in my first post contained wrong values for the default state vector x4 :sob: . When executing the code from the lecture in Juno I got much more default states for the simulation than when executing the code in the REPL or Jupyter. I have nuked my Atom/Juno settings and reinstalled everything. This seems to have solved the problem.

With having much fewer default states, I think I understand now how Arellano calculated the mean statistics and with @Augustus_Hebblewhite code I should be able to extract the necessary rows.

I’m terribly sorry for the noise and any confusion caused.


#5

This paper by Hatchondo et al describes the procedure in section 5.2. I hope it helps.

Cheers,