예제 #1
0
    def main_test(self):

        Markov_vFuncBool_example = MarkovConsumerType(**Markov_Dict)

        TranShkDstn_e = MeanOneLogNormal(
            Markov_vFuncBool_example.TranShkStd[0],
            123).approx(Markov_vFuncBool_example.TranShkCount)
        TranShkDstn_u = DiscreteDistribution(np.ones(1), np.ones(1) * .2)
        PermShkDstn = MeanOneLogNormal(
            Markov_vFuncBool_example.PermShkStd[0],
            123).approx(Markov_vFuncBool_example.PermShkCount)

        #employed Income shock distribution
        employed_IncShkDstn = combine_indep_dstns(PermShkDstn, TranShkDstn_e)

        #unemployed Income shock distribution
        unemployed_IncShkDstn = combine_indep_dstns(PermShkDstn, TranShkDstn_u)

        # Specify list of IncShkDstns for each state
        Markov_vFuncBool_example.IncShkDstn = [[
            employed_IncShkDstn, unemployed_IncShkDstn
        ]]

        #solve the consumer's problem
        Markov_vFuncBool_example.solve()

        self.assertAlmostEqual(
            Markov_vFuncBool_example.solution[0].vFunc[1](0.4),
            -4.127935542867632)
    def update_pref_shock_process(self):
        """
        Make a discrete preference shock structure for each period in the cycle
        for this agent type, storing them as attributes of self for use in the
        solution (and other methods).

        Parameters
        ----------
        none

        Returns
        -------
        none
        """
        PrefShkDstn = []  # discrete distributions of preference shocks
        for t in range(len(self.PrefShkStd)):
            PrefShkStd = self.PrefShkStd[t]
            new_dstn = MeanOneLogNormal(sigma=PrefShkStd,
                                        seed=self.RNG.randint(
                                            0, 2**31 - 1)).approx(
                                                N=self.PrefShkCount,
                                                tail_N=self.PrefShk_tail_N,
                                            )
            PrefShkDstn.append(new_dstn)

        # Store the preference shocks in self (time-varying) and restore time flow
        self.PrefShkDstn = PrefShkDstn
        self.add_to_time_vary("PrefShkDstn")
예제 #3
0
    def updatePrefShockProcess(self):
        '''
        Make a discrete preference shock structure for each period in the cycle
        for this agent type, storing them as attributes of self for use in the
        solution (and other methods).

        Parameters
        ----------
        none

        Returns
        -------
        none
        '''
        PrefShkDstn = [] # discrete distributions of preference shocks
        for t in range(len(self.PrefShkStd)):
            PrefShkStd = self.PrefShkStd[t]
            PrefShkDstn.append(
                MeanOneLogNormal(
                    sigma=PrefShkStd
                ).approx(N=self.PrefShkCount,
                         tail_N=self.PrefShk_tail_N))

        # Store the preference shocks in self (time-varying) and restore time flow
        self.PrefShkDstn = PrefShkDstn
        self.addToTimeVary('PrefShkDstn')
예제 #4
0
    def test_calc_expectation(self):
        dd_0_1_20 = Normal().approx(20)
        dd_1_1_40 = Normal(mu=1).approx(40)
        dd_10_10_100 = Normal(mu=10, sigma=10).approx(100)

        ce1 = calc_expectation(dd_0_1_20)
        ce2 = calc_expectation(dd_1_1_40)
        ce3 = calc_expectation(dd_10_10_100)

        self.assertAlmostEqual(ce1, 0.0)
        self.assertAlmostEqual(ce2, 1.0)
        self.assertAlmostEqual(ce3, 10.0)

        ce4 = calc_expectation(dd_0_1_20, lambda x: 2**x)

        self.assertAlmostEqual(ce4, 1.27153712)

        ce5 = calc_expectation(dd_1_1_40, lambda x: 2 * x)

        self.assertAlmostEqual(ce5, 2.0)

        ce6 = calc_expectation(dd_10_10_100, lambda x, y: 2 * x + y, 20)

        self.assertAlmostEqual(ce6, 40.0)

        ce7 = calc_expectation(dd_0_1_20, lambda x, y: x + y,
                               np.hstack(np.array([0, 1, 2, 3, 4, 5])))

        self.assertAlmostEqual(ce7.flat[3], 3.0)

        PermShkDstn = MeanOneLogNormal().approx(200)
        TranShkDstn = MeanOneLogNormal().approx(200)
        IncShkDstn = combine_indep_dstns(PermShkDstn, TranShkDstn)

        ce8 = calc_expectation(IncShkDstn, lambda X: X[0] + X[1])

        self.assertAlmostEqual(ce8, 2.0)

        ce9 = calc_expectation(
            IncShkDstn,
            lambda X, a, r: r / X[0] * a + X[1],
            np.array([0, 1, 2, 3, 4, 5]),  # an aNrmNow grid?
            1.05,  # an interest rate?
        )

        self.assertAlmostEqual(ce9[3], 9.518015322143837)
예제 #5
0
    def getShocks(self):
        '''
        Gets permanent and transitory income shocks for this period as well as preference shocks.

        Parameters
        ----------
        None

        Returns
        -------
        None
        '''
        IndShockConsumerType.getShocks(self) # Get permanent and transitory income shocks
        PrefShkNow = np.zeros(self.AgentCount) # Initialize shock array
        for t in range(self.T_cycle):
            these = t == self.t_cycle
            N = np.sum(these)
            if N > 0:
                PrefShkNow[these] = self.RNG.permutation(
                    MeanOneLogNormal(
                        sigma=self.PrefShkStd[t]
                    ).approx(N).X)
        self.PrefShkNow = PrefShkNow
예제 #6
0
 def test_drawMeanOneLognormal(self):
     self.assertEqual(MeanOneLogNormal().draw(1)[0], 3.5397367004222002)
예제 #7
0
    def update_income_process(self):

        self.wage = 1 / (self.SSPmu)  #calculate SS wage
        self.N = (self.mu_u * (self.IncUnemp * self.UnempPrb) + self.G) / (
            self.wage * self.tax_rate
        )  #calculate SS labor supply from Budget Constraint

        PermShkDstn_U = Lognormal(
            np.log(self.mu_u) - (self.L * (self.PermShkStd[0])**2) / 2,
            self.L * self.PermShkStd[0],
            123).approx(self.PermShkCount
                        )  #Permanent Shock Distribution faced when unemployed
        PermShkDstn_E = MeanOneLogNormal(self.PermShkStd[0], 123).approx(
            self.PermShkCount
        )  #Permanent Shock Distribution faced when employed

        pmf_P = np.concatenate(((1 - self.UnempPrb) * PermShkDstn_E.pmf,
                                self.UnempPrb * PermShkDstn_U.pmf))
        X_P = np.concatenate((PermShkDstn_E.X, PermShkDstn_U.X))
        PermShkDstn = [DiscreteDistribution(pmf_P, X_P)]
        self.PermShkDstn = PermShkDstn

        TranShkDstn_E = MeanOneLogNormal(self.TranShkStd[0], 123).approx(
            self.TranShkCount
        )  #Transitory Shock Distribution faced when employed
        TranShkDstn_E.X = (
            TranShkDstn_E.X * (1 - self.tax_rate) * self.wage * self.N
        ) / (
            1 - self.UnempPrb
        )**2  #NEED TO FIX THIS SQUARE TERM #add wage, tax rate and labor supply

        lng = len(TranShkDstn_E.X)
        TranShkDstn_U = DiscreteDistribution(
            np.ones(lng) / lng, self.IncUnemp *
            np.ones(lng))  #Transitory Shock Distribution faced when unemployed

        IncShkDstn_E = combine_indep_dstns(
            PermShkDstn_E,
            TranShkDstn_E)  # Income Distribution faced when Employed
        IncShkDstn_U = combine_indep_dstns(
            PermShkDstn_U,
            TranShkDstn_U)  # Income Distribution faced when Unemployed

        #Combine Outcomes of both distributions
        X_0 = np.concatenate((IncShkDstn_E.X[0], IncShkDstn_U.X[0]))
        X_1 = np.concatenate((IncShkDstn_E.X[1], IncShkDstn_U.X[1]))
        X_I = [X_0, X_1]  #discrete distribution takes in a list of arrays

        #Combine pmf Arrays
        pmf_I = np.concatenate(((1 - self.UnempPrb) * IncShkDstn_E.pmf,
                                self.UnempPrb * IncShkDstn_U.pmf))

        IncShkDstn = [DiscreteDistribution(pmf_I, X_I)]
        self.IncShkDstnN = IncShkDstn

        self.IncShkDstn = IncShkDstn
        self.add_to_time_vary('IncShkDstn')

        PermShkDstn_Uw = Lognormal(
            np.log(self.mu_u) - (self.L * (self.PermShkStd[0])**2) / 2,
            self.L * self.PermShkStd[0],
            123).approx(self.PermShkCount
                        )  #Permanent Shock Distribution faced when unemployed
        PermShkDstn_Ew = MeanOneLogNormal(self.PermShkStd[0], 123).approx(
            self.PermShkCount
        )  #Permanent Shock Distribution faced when employed

        TranShkDstn_Ew = MeanOneLogNormal(self.TranShkStd[0], 123).approx(
            self.TranShkCount
        )  #Transitory Shock Distribution faced when employed
        TranShkDstn_Ew.X = (
            TranShkDstn_Ew.X * (1 - self.tax_rate) *
            (self.wage + self.dx) * self.N) / (
                1 - self.UnempPrb)**2  #add wage, tax rate and labor supply

        lng = len(TranShkDstn_Ew.X)
        TranShkDstn_Uw = DiscreteDistribution(
            np.ones(lng) / lng, self.IncUnemp *
            np.ones(lng))  #Transitory Shock Distribution faced when unemployed

        IncShkDstn_Ew = combine_indep_dstns(
            PermShkDstn_Ew,
            TranShkDstn_Ew)  # Income Distribution faced when Employed
        IncShkDstn_Uw = combine_indep_dstns(
            PermShkDstn_Uw,
            TranShkDstn_Uw)  # Income Distribution faced when Unemployed

        #Combine Outcomes of both distributions
        X_0 = np.concatenate((IncShkDstn_Ew.X[0], IncShkDstn_Uw.X[0]))
        X_1 = np.concatenate((IncShkDstn_Ew.X[1], IncShkDstn_Uw.X[1]))
        X_I = [X_0, X_1]  #discrete distribution takes in a list of arrays

        #Combine pmf Arrays
        pmf_I = np.concatenate(((1 - self.UnempPrb) * IncShkDstn_Ew.pmf,
                                self.UnempPrb * IncShkDstn_Uw.pmf))

        IncShkDstnW = [DiscreteDistribution(pmf_I, X_I)]

        self.IncShkDstnW = IncShkDstnW
        self.add_to_time_vary('IncShkDstnW')