Getting an indented error, can someone help

Author: Nayha M

Hi there,

This is my code that I am setting up:

Implements iteration on the Bellman equations to solve the McCall growth model

import numpy as np

from numba import jit

A default utility function

@jit
def u(c, sigma):
    if c > 0:
        return (c**(1 - sigma) - 1) / (1 - sigma)
    else:
        return -10e6

class MyModel:

Stores the parameters and functions associated with a given model.

    def __init__(self,beta=0.997, # discount factor
                 deltaC=0.05, # job separation rate for construction workers
                  b=0.4, # uemployed benefits
                 c=1, # cost of posting a vacancy
                 N=1000, #grid points
                 gammaC=0.592, #match efficiency for construction
                 g=0.5, #matching function elasticity 
                 tauC=0.07, #sector demand shares
                 eta=2.00, #elasticity of substitution
                 wageC=2.956, #wages C              
                 VC=0.0182, #vacancy C 
                 UC=0.106, #Unemploymwnt C 
                 fc=0.2453, # job finding probability C
                 sigma = 1):
       
        self.beta = beta
         
         self.deltaC = deltaC 
         self.b = b
         self.c = c 
         self.gammaC = gammaC
         self.g = g
         self.tauC = tauC
         self.eta = eta
         self.wageC = wageC
         self.VC = VC    
         self.UC = UC    
         self.fc = fc   
         self.sigma = sigma 
        
def compute_derived_values(self):

 self.EmpC=fc/(fc+deltaC)  #steady state level of employment C
 self.Uc=deltaC/(fc+deltaC)  #steady state level of unemployment C
 self.EmpCgrid = linspace(0.9*self.EmpC,1.1*self.EmpC,1000).T #The grid, an N x 1 vector, is defined around the steady state.
 self.EmpCmatrix = repmat(self.EmpCgrid,1,1000)       # An, N x N, matrix with kgrid as columns, N times.
 self.EmpCmatrixprime = self.EmpCmatrix.T            #The same but transposed.
 self.OmegaC=[self.EmpC,self.Uc]
 self.OmegaCgrid=[self.EmpCgrid,self.Ucgrid] 
 self.OmegaCmatrix=repmat(self.OmegaCgrid,1,1000) 
 self.OmegaCmatrixprime=self.OmegaCmatrix.T

    
    
@jit
def _update_bellman(beta, deltaC, b, fc, wagesC, OmegaC, V, V_new, U, U_new):
  




for OmegaC_idx, OmegaC in enumerate(self.OmegaCgrid):
        # w_idx indexes the vector of possible wages
    

V_new[OmegaC_idx] = wageC + beta*((1 - deltaC)*(np.maximum(V[OmegaC_idx])) + deltaC*(np.maximum(U[OmegaC_idx]))) 
   
U_new[OmegaC_idx] = b + beta*(((fc)*np.maximum(V[OmegaC_idx])) + (1-fc)*(np.maximum(U[OmegaC_idx])))
    
return U_new

def solve_mymodel(mcm, tol=1e-5, max_iter=2000):
    """

Iterates to convergence on the Bellman equations

Parameters
----------
mcm : an instance of McCallModel
tol : float
    error tolerance
max_iter : int
    the maximum number of iterations
"""

V = np.ones(len(mcm.w_vec))  # Initial guess of V
V_new = np.empty_like(V)     # To store updates to V
U = 1                        # Initial guess of U
i = 0
error = tol + 1

while error > tol and i < max_iter:
    U_new = _update_bellman(mcm.beta, mcm.deltaC, mcm.b, 
            mcm.fc, mcm.wagesC, mcm.OmegaC, V, V_new, U)
    error_1 = np.max(np.abs(V_new - V))
    error_2 = np.abs(U_new - U)
    error = max(error_1, error_2)
    V[:] = V_new
    U = U_new
    i += 1

return V, U

But it is giving me this error:

  File "<ipython-input-4-5f43475caa45>", line 76
    for     OmegaC_idx, OmegaC in enumerate(self.OmegaCgrid):
      ^
IndentationError: expected an indented block

Could anyone help me figure out how to correct this error.

Thanks.

Hi Nayha,

It looks like you’ve forgotten to indent the code block for the function _update_bellman.

All the code that makes up a function definition should be indented. For example,

def print_foo():
    print("foo")
    print("foo again")

The code block in this case is the two print statements.

Regards, John.

Author: Nayha M

Cheers thank you.
That problem is solved but now I have another problem:

                     self.tauC,self.eta, self.wageC, self.VC, self.UC, self.fc, self.sigma = tauC, eta, wageC, VC, UC, fc, sigma```

def compute_derived_values(self):
        
    
                        self.EmpC=fc/(fc+deltaC),  #steady state level of employment C
                        self.Uc=deltaC/(fc+deltaC),  #steady state level of unemployment C
                        self.EmpCgrid = np.linspace(0.9*self.EmpC,1.1*self.EmpC,1000).T, #The grid, an N x 1 vector, is defined around the steady state.
                        self.EmpCmatrix = repmat(self.EmpCgrid,1,1000),       # An, N x N, matrix with kgrid as columns, N times.
                        self.EmpCmatrixprime = self.EmpCmatrix.T,           #The same but transposed.
                        self.Ucgrid = np.linspace(0.9*Uc,1.1*Uc,1000).T, #The grid, an N x 1 vector, is defined around the steady state.
                        self.Ucmatrix = repmat(Ucgrid,1,1000),       # An, N x N, matrix with kgrid as columns, N times.
    
    OmegaC=self.EmpC,self.Uc
    OmegaCgrid=EmpCgrid,self.Ucgrid 
    OmegaCmatrix=repmat(OmegaCgrid,1,1000), 
    OmegaCmatrixprime=OmegaCmatrix.T
 
@jit
def update_bellman(beta, deltaC, b, fc, wagesC, OmegaC, V, V_new, U, U_new):





            for OmegaC_idx, OmegaC in enumerate(OmegaCgrid):


for OmegaC_idx, OmegaC in enumerate(OmegaCgrid):
                    # w_idx indexes the vector of possible wages

                V_new[OmegaC_idx] = wageC + beta*((1 - deltaC)*(np.maximum(V[OmegaC_idx])) + deltaC*(np.maximum(U[OmegaC_idx]))) 

            
            U_new[OmegaC_idx] = 0.4 + beta*(((fc)*np.maximum(V[OmegaC_idx])) + (1-fc)*(np.maximum(U[OmegaC_idx])))

            return U_new
        
def solve_mymodel(mm, tol=1e-5, max_iter=2000):
        """
        Iterates to convergence on the Bellman equations 

        Parameters
        ----------
        mm : an instance of MyModel
        tol : float
            error tolerance
        max_iter : int
            the maximum number of iterations
        """

        V = np.ones(len(mm.OmegaCgrid))  # Initial guess of V
        V_new = np.empty_like(V)     # To store updates to V
        U = 1                        # Initial guess of U
        i = 0
        error = tol + 1

        while error > tol and i < max_iter:
            U_new = _update_bellman(mm.beta, mm.deltaC, mm.b, 
                    mm.fc, mm.wagesC, mm.OmegaCgrid, V, V_new, U)
            error_1 = np.max(np.abs(V_new - V))
            error_2 = np.abs(U_new - U)
            error = max(error_1, error_2)
            V[:] = V_new
            U = U_new
            i += 1
            
        return V, U

>Generate plots of value of employment and unemployment in the McCall model.

```python
import numpy as np
import matplotlib.pyplot as plt

mm = MyModel(OmegaCgrid)

fig, ax = plt.subplots()

ax.plot(mm.OmegaCgrid, V, 'b-', lw=2, deltaC=0.05, label='$V$')
ax.plot(mm.OmegaCgrid, [U] *len(mm.OmegaCgrid) , 'g-', lw=2, deltaC=0.05, label='$U$')
ax.legend(loc='upper left')
ax.grid()

plt.show()

But it says

NameError                                 Traceback (most recent call last)
<ipython-input-22-5c7451c725c0> in <module>()
    120 import matplotlib.pyplot as plt
    121 
--> 122 mm = MyModel(OmegaCgrid)
    123 
    124 fig, ax = plt.subplots()

NameError: name 'MyModel' is not defined

I have run the codes in bits first and it works fine but when I try to plot my results, it gives me several errors like these.

Thank you.

Best,
Nayha

Hi Nayha,

Have your read Part 1 of these lectures:

I guess it takes time but I think it will help you solve some of these simple problems.

Regards, John.