def setUp(self):
        # Create portfolio choice consumer type
        self.pcct = cpm.PortfolioConsumerType()
        self.pcct.cycles = 0

        # Solve the model under the given parameters

        self.pcct.solve()
Exemple #2
0
    def test_joint_dist(self):
        # Create portfolio choice consumer type
        self.joint_dist = cpm.PortfolioConsumerType()
        self.joint_dist.IndepDstnBool = False
        self.joint_dist.solve_one_period = make_one_period_oo_solver(
            cpm.ConsPortfolioJointDistSolver)

        # Solve model under given parameters
        self.joint_dist.solve()
Exemple #3
0
    def test_discrete(self):
        # Make example type of agent who can only choose risky share along discrete grid
        init_discrete_share = cpm.init_portfolio.copy()
        # PortfolioConsumerType requires vFuncBool to be True when DiscreteShareBool is True
        init_discrete_share["DiscreteShareBool"] = True
        init_discrete_share["vFuncBool"] = True

        # Create portfolio choice consumer type
        self.discrete = cpm.PortfolioConsumerType(**init_discrete_share)

        # Solve model under given parameters
        self.discrete.solve()
Exemple #4
0
    def test_sticky(self):
        # Make another example type, but this one can only update their risky portfolio
        # share in any particular period with 15% probability.
        init_sticky_share = cpm.init_portfolio.copy()
        init_sticky_share["AdjustPrb"] = 0.15

        # Create portfolio choice consumer type
        self.sticky = cpm.PortfolioConsumerType(**init_sticky_share)

        # Solve the model under the given parameters

        self.sticky.solve()
    def setUp(self):

        # Parameters from Mehra and Prescott (1985):
        Avg = 1.08  # equity premium
        Std = 0.20  # standard deviation of rate-of-return shocks

        RiskyDstnFunc = cpm.RiskyDstnFactory(
            RiskyAvg=Avg, RiskyStd=Std)  # Generates nodes for integration
        RiskyDrawFunc = cpm.LogNormalRiskyDstnDraw(
            RiskyAvg=Avg, RiskyStd=Std
        )  # Function to generate draws from a lognormal distribution

        init_portfolio = copy.copy(
            param.init_idiosyncratic_shocks
        )  # Default parameter values for inf horiz model - including labor income with transitory and permanent shocks
        init_portfolio['approxRiskyDstn'] = RiskyDstnFunc
        init_portfolio['drawRiskyFunc'] = RiskyDrawFunc
        init_portfolio[
            'RiskyCount'] = 2  # Number of points in the approximation; 2 points is minimum
        init_portfolio[
            'RiskyShareCount'] = 25  # How many discrete points to allow for portfolio share
        init_portfolio[
            'Rfree'] = 1.0  # Riskfree return factor (interest rate is zero)
        init_portfolio['CRRA'] = 6.0  # Relative risk aversion

        # Uninteresting technical parameters:
        init_portfolio['aXtraMax'] = 100
        init_portfolio['aXtraCount'] = 50
        init_portfolio[
            'BoroCnstArt'] = 0.0  # important for theoretical reasons
        init_portfolio[
            'DiscFac'] = 0.92  # Make them impatient even wrt a riskfree return of 1.08

        # Create portfolio choice consumer type
        self.pcct = cpm.PortfolioConsumerType(**init_portfolio)

        # %% {"code_folding": []}
        # Solve the model under the given parameters

        self.pcct.solve()
Exemple #6
0
    def test_discrete_and_joint(self):
        # Make example type of agent who can only choose risky share along
        # discrete grid and income dist is correlated with risky dist
        init_discrete_and_joint_share = cpm.init_portfolio.copy()
        # PortfolioConsumerType requires vFuncBool to be True when DiscreteShareBool is True
        init_discrete_and_joint_share["DiscreteShareBool"] = True
        init_discrete_and_joint_share["vFuncBool"] = True

        # Create portfolio choice consumer type
        self.discrete_and_joint = cpm.PortfolioConsumerType(
            **init_discrete_and_joint_share)
        self.discrete_and_joint.IndepDstnBool = False
        self.discrete_and_joint.solve_one_period = make_one_period_oo_solver(
            cpm.ConsPortfolioJointDistSolver)

        # Solve model under given parameters
        self.discrete_and_joint.solve()
Exemple #7
0
if __name__ == '__main__':
    # Running as a script
    my_file_path = os.path.abspath("../")
else:
    # Running from do_ALL
    my_file_path = os.path.dirname(os.path.abspath("do_ALL.py"))
    my_file_path = os.path.join(my_file_path, "Code/Python/")

FigPath = os.path.join(my_file_path, "Figures/")

# %% Calibration and solution
sys.path.append(my_file_path)
# Loading the parameters from the ../Code/Calibration/params.py script
from Calibration.params import dict_portfolio, time_params

agent = cpm.PortfolioConsumerType(**dict_portfolio)
agent.solve()

# %% Run simulation and store results in a data frame

# Number of agents and periods in the simulation.
agent.AgentCount = 50  # Number of instances of the class to be simulated.
# Since agents can die, they are replaced by a new agent whenever they do.

# Number of periods to be simulated
agent.T_sim = agent.T_cycle * 50

# Set up the variables we want to keep track of.
agent.track_vars = [
    'aNrmNow', 'cNrmNow', 'pLvlNow', 't_age', 'ShareNow', 'mNrmNow'
]
FigPath = os.path.join(my_file_path,"Figures/")

# %% Calibration and solution

# Import parameters from external file
sys.path.append(os.path.realpath('../')) 
# Loading the parameters from the ../Code/Calibration/params.py script
from Calibration.params import dict_portfolio, time_params, det_income, Mu, Rfree, Std, norm_factor

# Create new dictionary
merton_dict = copy(dict_portfolio)

# Adjust certain parameters to align with Merton-Samuleson
# Log normal returns (Overwriting Noramal returns defined in params)
mu = Mu + Rfree
RiskyDstnFunc = cpm.RiskyDstnFactory(RiskyAvg=mu, RiskyStd=Std) # Generates nodes for integration
RiskyDrawFunc = cpm.LogNormalRiskyDstnDraw(RiskyAvg=mu, RiskyStd=Std) # Generates draws from the "true" distribution

# Make agent inifitely lived. Following parameter examples from ConsumptionSaving Notebook
merton_dict['approxRiskyDstn'] = RiskyDstnFunc
merton_dict['drawRiskyFunc'] = RiskyDrawFunc

agent = cpm.PortfolioConsumerType(**merton_dict)
agent.solve()

# %%

aMin = 0   # Minimum ratio of assets to income to plot
aMax = 1e5  # Maximum ratio of assets to income to plot
aPts = 1000 # Number of points to plot 
# In the discrete portfolio shares case, the user also must
# input a function that *draws* from the distribution in drawRiskyFunc
#
# Other assumptions: 
#   * distributions are time constant
#   * probability of being allowed to reoptimize is time constant
#      * If p < 1, you must specify the PortfolioSet discretely
#       

# %% {"code_folding": []}
# Set up the model
# Parameters from Mehra and Prescott (1985):
Avg = 1.08 # equity premium 
Std = 0.20 # standard deviation of rate-of-return shocks 

RiskyDstnFunc = cpm.RiskyDstnFactory(RiskyAvg=Avg, RiskyStd=Std)       # Generates nodes for integration
RiskyDrawFunc = cpm.LogNormalRiskyDstnDraw(RiskyAvg=Avg, RiskyStd=Std) # Generates draws from a lognormal distribution

init_portfolio = copy.copy(param.init_idiosyncratic_shocks) # Default parameter values for inf horiz model
init_portfolio['approxRiskyDstn'] = RiskyDstnFunc
init_portfolio['drawRiskyFunc']   = RiskyDrawFunc
init_portfolio['RiskyCount']      = 2   # Number of points in the approximation; 2 points is minimum
init_portfolio['RiskyShareCount'] = 25  # How many discrete points to allow in the share approximation
init_portfolio['Rfree']           = 1.0 # Riskfree return factor is 1 (interest rate is zero)
init_portfolio['CRRA']            = 6.0 # Relative risk aversion

# Uninteresting technical parameters:
init_portfolio['aXtraMax']        = 100 
init_portfolio['aXtraCount']      = 50
init_portfolio['BoroCnstArt']     = 0.0 # important for theoretical reasons
# init_portfolio['vFuncBool'] = True # We do not need value function for purposes here
Exemple #10
0
# Do away with probability of death
mpc_dict['LivPrb'] = [1] * dict_portfolio['T_cycle']

# Risky returns
mu = 0.05
std = 0.1

# Construct the distribution approximation for integration
RiskyDstnFunc = lambda count: approxLognormal(count, mu=mu, sigma=std)
# Contruct function for drawing returns
RiskyDrawFunc = lambda seed: drawLognormal(1, mu=mu, sigma=std, seed=seed)

mpc_dict['approxRiskyDstn'] = RiskyDstnFunc
mpc_dict['drawRiskyFunc'] = RiskyDrawFunc

agent = cpm.PortfolioConsumerType(**mpc_dict)
agent.cylces = 0
agent.solve()

# %% Compute the theoretical MPC (for when there is no labor income)

# First extract some parameter values that will be used
crra = agent.CRRA
sigma_r = std
goth_r = mu + sigma_r**2 / 2
beta = agent.DiscFac

# Compute E[Return factor ^(1-rho)]
E_R_exp = np.exp((1 - crra) * goth_r - crra * (1 - crra) * sigma_r**2 / 2)

# And the theoretical MPC
# No income shocks
pf_dict['PermShkStd'] = [0] * t_cycle
pf_dict['TranShkStd'] = [0] * t_cycle

# Make agent live for sure until the terminal period
pf_dict['T_retire'] = t_cycle
pf_dict['LivPrb'] = [1] * t_cycle

# Shut down income growth
pf_dict['PermGroFac'] = [1] * t_cycle

# Decrease grid for speed
pf_dict['aXtraCount'] = 100

# %% Create both agents
port_agent = cpm.PortfolioConsumerType(**pf_dict)
port_agent.solve()

pf_agent = cis.PerfForesightConsumerType(**pf_dict)
pf_agent.solve()

# %% Construct the analytical solution

rho = pf_dict['CRRA']
R = pf_dict['Rfree']
Beta = pf_dict['DiscFac']
T = pf_dict['T_cycle'] + 1

thorn_r = (R * Beta)**(1 / rho) / R

ht = lambda t: (1 - (1 / R)**(T - t + 1)) / (1 - 1 / R) - 1