Arellano (2008) Default model

I am reading the Python codes replicating Arellano (2008) in the series of Quant lecture
Link for the code:

There are some codes I could not understand,

  1. Allocate memory

     self.Vd = np.zeros(ny)
     self.Vc = np.zeros((ny, nB))
     self.V = np.zeros((ny, nB))
     self.Q = np.ones((ny, nB)) * .95  # Initial guess for prices
     self.default_prob = np.empty((ny, nB))
     # Compute the value functions, prices, and default prob
     self.solve(tol=tol, maxit=maxit)
     # Compute the optimal savings policy conditional on no default

    def solve(self, tol=1e-8, maxit=10000):
    # Iteration Stuff
    it = 0
    dist = 10.

     # Alloc memory to store next iterate of value function
     V_upd = np.zeros((self.ny, self.nB))
     # == Main loop == #
     while dist > tol and maxit > it:
         # Compute expectations for this iteration
         Vs = self.V, self.Vd, self.Vc
         EV, EVd, EVc = (self.Py @ v for v in Vs)
         # Run inner loop to update value functions Vc and Vd.
         # Note that Vc and Vd are updated in place.  Other objects
         # are not modified.
         _inner_loop(self.ygrid, self.def_y,
                     self.Bgrid, self.Vd, self.Vc,
                     EVc, EVd, EV, self.Q,
                     self.β, self.θ, self.γ)

I do not understand what “inner_loop” actually do here? I googled but could not find this syntax.

  1. Update prices

Vd_compat = np.repeat(self.Vd, self.nB).reshape(self.ny, self.nB)
default_states = Vd_compat > self.Vc

I don’t know what will be returned from “default_states= Vd > Vc”? I guess it is kind of a matrix which assign 1 for position whose element in Vd is greater than Vc, and 0 other wise; but I am not so sure, I searched but could not find similar code to see its meaning.

Is there anyone having an idea?

Any help is really appreciated.

Thank you!

Hi @sunflower2010

  1. _inner_loop() is a function defined above that code, and acts like any other function.

  2. Using the > and < will return a Boolean, equal to either True or False. This can also be converted to 1 or 0. As these are arrays, it will act element-wise and look at each element in the array individually.

Hope that helps.

1 Like

Hi @natashawatkins

Thanks for your help.
I am still reading the code, and encounter another confusion.
I don’t know what is the value of "None " here? (the code is also from Arellano 2008,

def simulate(self, T, y_init=None, B_init=None):
Simulate time series for output, consumption, B’

Thank you,

None is just another type of value -

1 Like

Hi @natashawatkins,

Thanks for your help.
If possible, could you plz help me with this question now about simulation. I am totally lost when reading the simulation part. (Still in Arellano 2008 code).

I do not understand why we need to choose y_int and B_int are the index which near the mean of grid or near 0?

def simulate(self, T, y_init=None, B_init=None):
Simulate time series for output, consumption, B’.
# Find index i such that Bgrid[i] is near 0
zero_B_index = np.searchsorted(self.Bgrid, 0)

    if y_init is None:
        # Set to index near the mean of the ygrid
        y_init = np.searchsorted(self.ygrid, self.ygrid.mean())
    if B_init is None:
        B_init = zero_B_index
    # Start off not in default
    in_default = False

y_sim_indices =, init=y_init)
B_sim_indices = np.empty(T, dtype=np.int64)
B_sim_indices[0] = B_init
q_sim = np.empty(T)
in_default_series = np.zeros(T, dtype=np.int64)