Esempio n. 1
0
    def setUp(self):
        """
        Prepare to compare the models by initializing and solving them
        """
        # Set up and solve infinite type

        # Define a test dictionary that should have the same solution in the
        # perfect foresight and idiosyncratic shocks models.
        test_dictionary = deepcopy(init_idiosyncratic_shocks)
        test_dictionary["LivPrb"] = [1.0]
        test_dictionary["DiscFac"] = 0.955
        test_dictionary["PermGroFac"] = [1.0]
        test_dictionary["PermShkStd"] = [0.0]
        test_dictionary["TranShkStd"] = [0.0]
        test_dictionary["UnempPrb"] = 0.0
        test_dictionary["T_cycle"] = 1
        test_dictionary["T_retire"] = 0
        test_dictionary["BoroCnstArt"] = None

        InfiniteType = IndShockConsumerType(**test_dictionary)
        InfiniteType.cycles = 0

        InfiniteType.update_income_process()
        InfiniteType.solve()
        InfiniteType.unpack("cFunc")

        # Make and solve a perfect foresight consumer type with the same parameters
        PerfectForesightType = PerfForesightConsumerType(**test_dictionary)
        PerfectForesightType.cycles = 0

        PerfectForesightType.solve()
        PerfectForesightType.unpack("cFunc")

        self.InfiniteType = InfiniteType
        self.PerfectForesightType = PerfectForesightType
    def __init__(self):

        PFexample = PerfForesightConsumerType(
        )  # set up a consumer type and use default parameteres
        PFexample.cycles = 0  # Make this type have an infinite horizon
        PFexample.DiscFac = 0.05
        PFexample.PermGroFac = [0.7]

        PFexample.solve()  # solve the consumer's problem
        PFexample.unpackcFunc()  # unpack the consumption function

        self.cFunc = PFexample.solution[0].cFunc
        self.a0 = self.cFunc(0)
        self.a1 = self.cFunc(1) - self.cFunc(0)
    def __init__(self, Rfree=1.001, CRRA=2):

        PFaux = PerfForesightConsumerType(
        )  # set up a consumer type and use default parameteres
        PFaux.cycles = 0  # Make this type have an infinite horizon
        PFaux.DiscFac = 1 / Rfree
        PFaux.Rfree = Rfree
        PFaux.LivPrb = [1.0]
        PFaux.PermGroFac = [1.0]
        PFaux.CRRA = CRRA
        PFaux.solve()  # solve the consumer's problem
        PFaux.unpackcFunc()  # unpack the consumption function

        self.cFunc = PFaux.solution[0].cFunc
Esempio n. 4
0
    def setUp(self):
        """
        Prepare to compare the models by initializing and solving them
        """
        # Set up and solve infinite type
        import HARK.ConsumptionSaving.ConsumerParameters as Params

        # Define a test dictionary that should have the same solution in the
        # perfect foresight and idiosyncratic shocks models.
        test_dictionary = deepcopy(Params.init_idiosyncratic_shocks)
        test_dictionary['LivPrb'] = [1.]
        test_dictionary['DiscFac'] = 0.955
        test_dictionary['PermGroFac'] = [1.]
        test_dictionary['PermShkStd'] = [0.]
        test_dictionary['TranShkStd'] = [0.]
        test_dictionary['UnempPrb'] = 0.
        test_dictionary['T_cycle'] = 1
        test_dictionary['T_retire'] = 0
        test_dictionary['BoroCnstArt'] = None

        InfiniteType = IndShockConsumerType(**test_dictionary)
        InfiniteType.cycles = 0

        InfiniteType.updateIncomeProcess()
        InfiniteType.solve()
        InfiniteType.timeFwd()
        InfiniteType.unpackcFunc()

        # Make and solve a perfect foresight consumer type with the same parameters
        PerfectForesightType = PerfForesightConsumerType(**test_dictionary)
        PerfectForesightType.cycles = 0

        PerfectForesightType.solve()
        PerfectForesightType.unpackcFunc()
        PerfectForesightType.timeFwd()

        self.InfiniteType = InfiniteType
        self.PerfectForesightType = PerfectForesightType
Esempio n. 5
0
def plot1(Epsilon, DiscFac, PopGrowth, YearsPerGeneration, Initialk):
    '''Inputs:
        Epsilon: Elasticity of output with respect to capital/labour ratio
        DiscFac: One period discount factor
        YearPerGeneration: No. of years per generation
        PopGrowth: Gross growth rate of population in one period'''
    
    #
    kMax = 0.1
    # Define some parameters
    Beta = DiscFac**YearsPerGeneration
    xi = PopGrowth**YearsPerGeneration
    Q = (1-Epsilon)*(Beta/(1+Beta))/xi
    kBar = Q**(1/(1-Epsilon))
    
    # Create an agent that will solve the consumption problem
    PFagent = PerfForesightConsumerType(**init_perfect_foresight)
    PFagent.cycles = 1 # let the agent live the cycle of periods just once
    PFagent.T_cycle = 2 # Number of periods in the cycle
    PFagent.assign_parameters(PermGroFac = [0.000001]) # Income only in the first period
    PFagent.LivPrb = [1.]
    
    PFagent.DiscFac = Beta
    
    # Hark seems to have trouble with log-utility
    # so pass rho = 1 + something small.
    PFagent.CRRA = 1.001
    
    PFagent.solve()
    
    # Plot the OLG capital accumulation curve and 45 deg line
    plt.figure(figsize=(9,6))
    kt_range = np.linspace(0, kMax, 300)
    
    # Analitical solution plot
    ktp1 = Q*kt_range**Epsilon
    plt.plot(kt_range, ktp1, 'b-', label='Capital accumulation curve')
    plt.plot(kt_range, kt_range, 'k-', label='45 Degree line')
    
    # Plot the path
    kt_ar = Initialk
    ktp1_ar = 0.
    for i in range(3):
        
        # Compute Next Period's capital using HARK
        wage = (1-Epsilon)*kt_ar**Epsilon
        c = PFagent.solution[0].cFunc(wage)
        a = wage - c
        k1 = a/xi
        
        plt.arrow(kt_ar, ktp1_ar, 0., k1-ktp1_ar,
                  length_includes_head=True,
                  lw=0.01,
                  width=0.0005,
                  color='black',
                  edgecolor=None)
        plt.arrow(kt_ar, k1, k1-kt_ar , 0.,
                  length_includes_head=True,
                  lw=0.01,
                  width=0.0005,
                  color='black',
                  edgecolor=None)
        
        # Update arrow
        kt_ar = k1
        ktp1_ar = kt_ar
    
    # Plot kbar and initial k
    plt.plot(kBar, kBar, 'ro', label=r'$\bar{k}$')
    plt.plot(Initialk, 0.0005, 'co', label = '$k_0$')
    
    plt.legend()
    plt.xlim(0 ,kMax)
    plt.ylim(0, kMax)
    plt.xlabel('$k_t$')
    plt.ylabel('$k_{t+1}$')
    plt.show()

    return None
Esempio n. 6
0
    "PermGroFac": [1.01],  # Permanent income growth factor
    "BoroCnstArt": None,  # Artificial borrowing constraint
    "aXtraCount": 200,  # Maximum number of gridpoints in consumption function
    # Parameters that characterize the nature of time
    "T_cycle": 1,  # Number of periods in the cycle for this agent type
    "cycles": 0,  # Number of times the cycle occurs (0 --> infinitely repeated)
}

# %% [markdown]
# ## Solving and examining the solution of the perfect foresight model
#
# With the dictionary we have just defined, we can create an instance of $\texttt{PerfForesightConsumerType}$ by passing the dictionary to the class (as if the class were a function).  This instance can then be solved by invoking its $\texttt{solve}$ method.

# %%
PFexample = PerfForesightConsumerType(**PerfForesightDict)
PFexample.cycles = 0
PFexample.solve()

# %% [markdown]
# The $\texttt{solve}$ method fills in the instance's attribute $\texttt{solution}$ as a time-varying list of solutions to each period of the consumer's problem.  In this case, $\texttt{solution}$ will be a list with exactly one instance of the class $\texttt{ConsumerSolution}$, representing the solution to the infinite horizon model we specified.

# %%
print(PFexample.solution)

# %% [markdown]
# Each element of $\texttt{solution}$ has a few attributes. To see all of them, we can use the \texttt{vars} built in function:
#
# the consumption functions reside in the attribute $\texttt{cFunc}$ of each element of $\texttt{ConsumerType.solution}$.  This method creates a (time varying) attribute $\texttt{cFunc}$ that contains a list of consumption functions.

# %%
print(vars(PFexample.solution[0]))
    PerfForesightConsumerType, IndShockConsumerType, KinkedRconsumerType,
    init_lifecycle, init_cyclical)
from HARK.utilities import plotFuncsDer, plotFuncs
from time import time

# %%
mystr = lambda number: "{:.4f}".format(number)

# %%
do_simulation = True

# %%
# Make and solve an example perfect foresight consumer
PFexample = PerfForesightConsumerType()
# Make this type have an infinite horizon
PFexample.cycles = 0

# %%
start_time = time()
PFexample.solve()
end_time = time()
print("Solving a perfect foresight consumer took " +
      mystr(end_time - start_time) + " seconds.")
PFexample.unpackcFunc()
PFexample.timeFwd()

# %%
# Plot the perfect foresight consumption function
print("Perfect foresight consumption function:")
mMin = PFexample.solution[0].mNrmMin
plotFuncs(PFexample.cFunc[0], mMin, mMin + 10)
Esempio n. 8
0
def FisherPlot(Y1, Y2, B1, R, c1Max, c2Max):

    # Basic setup of perfect foresight consumer

    # We first create an instance of the class
    # PerfForesightConsumerType, with its standard parameters.
    PFexample = PerfForesightConsumerType()

    PFexample.cycles = 1  # let the agent live the cycle of periods just once
    PFexample.T_cycle = 2  # Number of periods in the cycle
    PFexample.PermGroFac = [1.]  # No automatic growth in income across periods
    PFexample.LivPrb = [1.]  # No chance of dying before the second period
    PFexample.aNrmInitStd = 0.
    PFexample.AgentCount = 1
    CRRA = PFexample.CRRA
    Beta = PFexample.DiscFac

    # Set interest rate and bank balances from input.
    PFexample.Rfree = R
    PFexample.aNrmInitMean = B1

    # Solve the model: this generates the optimal consumption function.

    # Try-except blocks "try" to execute the code in the try block. If an
    # error occurs, the except block is executed and the application does
    # not halt
    try:
        PFexample.solve()
    except:
        print(
            'Those parameter values violate a condition required for solution!'
        )

    # Create the figure
    c1Min = 0.
    c2Min = 0.
    plt.figure(figsize=(8, 8))
    plt.xlabel('Period 1 Consumption $C_1$')
    plt.ylabel('Period 2 Consumption $C_2$')
    plt.ylim([c2Min, c2Max])
    plt.xlim([c1Min, c1Max])

    # Plot the budget constraint
    C1_bc = np.linspace(c1Min, B1 + Y1 + Y2 / R, 10, endpoint=True)
    C2_bc = (Y1 + B1 - C1_bc) * R + Y2
    plt.plot(C1_bc, C2_bc, 'k-', label='Budget Constraint')

    # Plot the optimal consumption bundle
    C1 = PFexample.solution[0].cFunc(B1 + Y1 + Y2 / R)
    C2 = PFexample.solution[1].cFunc((Y1 + B1 - C1) * R + Y2)
    plt.plot(C1, C2, 'ro', label='Optimal Consumption')

    # Plot the indifference curve
    V = C1**(1 - CRRA) / (1 - CRRA) + Beta * C2**(1 - CRRA) / (
        1 - CRRA)  # Get max utility
    C1_V = np.linspace(((1 - CRRA) * V)**(1 / (1 - CRRA)) + 0.5, c1Max, 1000)
    C2_V = (((1 - CRRA) * V - C1_V**(1 - CRRA)) / Beta)**(1 / (1 - CRRA))
    plt.plot(C1_V, C2_V, 'b-', label='Indiferrence Curve')

    # Add a legend and display the plot
    plt.legend()
    plt.show()

    return None
Esempio n. 9
0
def FisherPlot2(Y1, Y2, B1, RHi, RLo, c1Max, c2Max):

    # Basic setup of perfect foresight consumer
    PFexample = PerfForesightConsumerType(
    )  # set up a consumer type and use default parameteres
    PFexample.cycles = 1  # let the agent live the cycle of periods just once
    PFexample.T_cycle = 2  # Number of periods in the cycle
    PFexample.PermGroFac = [1.]  # No automatic growth in income across periods
    PFexample.LivPrb = [1.]  # No chance of dying before the second period
    PFexample.aNrmInitStd = 0.
    PFexample.AgentCount = 1
    CRRA = 2.
    Beta = PFexample.DiscFac

    # Set the parameters we enter
    PFexample.aNrmInitMean = B1

    # Create two models, one for RfreeHigh and one for RfreeLow
    PFexampleRHi = deepcopy(PFexample)
    PFexampleRHi.Rfree = RHi
    PFexampleRLo = deepcopy(PFexample)
    PFexampleRLo.Rfree = RLo

    c1Min = 0.
    c2Min = 0.

    # Solve the model for RfreeHigh
    try:
        PFexampleRHi.solve()
        PFexampleRLo.solve()
    except:
        print(
            'Those parameter values violate a condition required for solution!'
        )

    # Plot the chart
    plt.figure(figsize=(8, 8))
    plt.xlabel('Period 1 Consumption $C_1$')
    plt.ylabel('Period 2 Consumption $C_2$')
    plt.ylim([c2Min, c2Max])
    plt.xlim([c1Min, c1Max])

    # Plot the budget constraints
    C1_bc_RLo = np.linspace(c1Min, B1 + Y1 + Y2 / RLo, 10, endpoint=True)
    C2_bc_RLo = (Y1 + B1 - C1_bc_RLo) * RLo + Y2
    plt.plot(C1_bc_RLo, C2_bc_RLo, 'k-', label='Budget Constraint R Low')

    C1_bc_RHi = np.linspace(c1Min, B1 + Y1 + Y2 / RHi, 10, endpoint=True)
    C2_bc_RHi = (Y1 + B1 - C1_bc_RHi) * RHi + Y2
    plt.plot(C1_bc_RHi, C2_bc_RHi, 'k--', label='Budget Constraint R High')

    # The optimal consumption bundles
    C1_opt_RLo = PFexampleRLo.solution[0].cFunc(B1 + Y1 + Y2 / RLo)
    C2_opt_RLo = PFexampleRLo.solution[1].cFunc((Y1 + B1 - C1_opt_RLo) * RLo +
                                                Y2)

    C1_opt_RHi = PFexampleRHi.solution[0].cFunc(B1 + Y1 + Y2 / RHi)
    C2_opt_RHi = PFexampleRHi.solution[1].cFunc((Y1 + B1 - C1_opt_RHi) * RHi +
                                                Y2)

    # Plot the indifference curves
    V_RLo = C1_opt_RLo**(1 - CRRA) / (1 - CRRA) + Beta * C2_opt_RLo**(
        1 - CRRA) / (1 - CRRA)  # Get max utility
    C1_V_RLo = np.linspace(((1 - CRRA) * V_RLo)**(1 / (1 - CRRA)) + 0.5, c1Max,
                           1000)
    C2_V_RLo = (((1 - CRRA) * V_RLo - C1_V_RLo**(1 - CRRA)) /
                Beta)**(1 / (1 - CRRA))
    plt.plot(C1_V_RLo, C2_V_RLo, 'b-', label='Indiferrence Curve R Low')

    V_RHi = C1_opt_RHi**(1 - CRRA) / (1 - CRRA) + Beta * C2_opt_RHi**(
        1 - CRRA) / (1 - CRRA)  # Get max utility
    C1_V_RHi = np.linspace(((1 - CRRA) * V_RHi)**(1 / (1 - CRRA)) + 0.5, c1Max,
                           1000)
    C2_V_RHi = (((1 - CRRA) * V_RHi - C1_V_RHi**(1 - CRRA)) /
                Beta)**(1 / (1 - CRRA))
    plt.plot(C1_V_RHi, C2_V_RHi, 'b--', label='Indiferrence Curve R High')

    # The substitution effect
    C1_Subs = (V_RHi * (1 - CRRA) /
               (1 + Beta * (RLo * Beta)**((1 - CRRA) / CRRA)))**(1 /
                                                                 (1 - CRRA))
    C2_Subs = C1_Subs * (RLo * Beta)**(1 / CRRA)

    # The Human wealth effect
    Y2HW = Y2 * RLo / RHi
    C1HW = Y2HW / (RLo + (RLo)**(1 / CRRA))
    C2HW = C1HW * (RLo * Beta)**(1 / CRRA)

    C1_bc_HW = np.linspace(c1Min, B1 + Y1 + Y2HW / RLo, 10, endpoint=True)
    C2_bc_HW = (Y1 + B1 - C1_bc_HW) * RLo + Y2HW
    plt.plot(C1_bc_HW, C2_bc_HW, 'k:')

    VHW = (C1HW**(1 - CRRA)) / (1 - CRRA) + (Beta * C2HW**
                                             (1 - CRRA)) / (1 - CRRA)
    C1_V_HW = np.linspace(((1 - CRRA) * VHW)**(1 / (1 - CRRA)) + 0.5, c1Max,
                          1000)
    C2_V_HW = (((1 - CRRA) * VHW - C1_V_HW**(1 - CRRA)) / Beta)**(1 /
                                                                  (1 - CRRA))
    plt.plot(C1_V_HW, C2_V_HW, 'b:')

    # Plot the points of interest
    plt.plot(C1_opt_RLo,
             C2_opt_RLo,
             'ro',
             label='A: Optimal Consumption R Low')
    plt.plot(C1_Subs,
             C2_Subs,
             'go',
             label='B: Income effect DB \n    Substitution effect BC')
    plt.plot(C1_opt_RHi,
             C2_opt_RHi,
             'mo',
             label='C: Optimal Consumption R High')
    plt.plot(C1HW, C2HW, 'co', label='D: HW effect AD')

    plt.legend()
    plt.show()

    return None
from HARK.ConsumptionSaving.ConsIndShockModel import PerfForesightConsumerType # Import the consumer type
import HARK.ConsumptionSaving.ConsumerParameters as Params # Import default parameters

# Now extract the default values of the parameters of interest

CRRA       = Params.CRRA 
Rfree      = Params.Rfree 
DiscFac    = Params.DiscFac
PermGroFac = Params.PermGroFac
rfree      = Rfree-1

# %%
# Now create a perfect foresight consumer example, 
PFagent = PerfForesightConsumerType(**Params.init_perfect_foresight)
PFagent.cycles = 0 # We need the consumer to be infinitely lived
PFagent.LivPrb = [1.0] # Suppress the possibility of dying

# Solve the agent's problem
PFagent.solve()

# %%
# Plot the consumption function 

# Remember, after invoking .solve(), the consumption function is stored as PFagent.solution[0].cFunc

# Set out some range of market resources that we want to plot consumption for

mMin = 0
mMax = 20
numPoints = 100