How to use the quantecon.MarkovChain function in quantecon

To help you get started, we’ve selected a few quantecon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github QuantEcon / QuantEcon.lectures.code / opt_tax_recur / sequential_allocation.py View on Github external
def __init__(self, model):

        # Initialize from model object attributes
        self.β, self.π, self.G = model.β, model.π, model.G
        self.mc, self.Θ = MarkovChain(self.π), model.Θ
        self.S = len(model.π)  # Number of states
        self.model = model

        # Find the first best allocation
        self.find_first_best()
github QuantEcon / QuantEcon.lectures.code / lake_model / lake_agent_dynamics.py View on Github external
from quantecon import MarkovChain

lm = LakeModel(d=0, b=0)
T = 5000  # Simulation length

α, λ = lm.α, lm.λ

P = [[1 - λ,        λ],
     [    α,    1 - α]]

mc = MarkovChain(P)

xbar = lm.rate_steady_state()

fig, axes = plt.subplots(2, 1, figsize=(10, 8))
s_path = mc.simulate(T, init=1)
s_bar_e = s_path.cumsum() / range(1, T+1)
s_bar_u = 1 - s_bar_e

to_plot = [s_bar_u, s_bar_e]
titles = ['Percent of time unemployed', 'Percent of time employed']

for i, plot in enumerate(to_plot):
    axes[i].plot(plot, lw=2, alpha=0.5)
    axes[i].hlines(xbar[i], 0, T, 'r', '--')
    axes[i].set_title(titles[i])
    axes[i].grid()
github QuantEcon / QuantEcon.lectures.code / harrison_kreps / harrison_kreps_code.py View on Github external
print(eaa)


ebb = np.linalg.matrix_power(qb, 100)


print("100th power of qb")
print(ebb)

import quantecon as qe


qa = np.array([[1./2, 1./2], [2./3, 1./3]])
qb = np.array([[2./3, 1./3], [1./4, 3./4]])

mcA = qe.MarkovChain(qa)
mcB = qe.MarkovChain(qb)

ppa = mcA.stationary_distributions
ppb = mcB.stationary_distributions

print("stationary distribution of P_a")

print(ppa)



mcB = qe.MarkovChain(qb)

ppb = mcB.stationary_distributions

print("stationary distribution of P_b")
github QuantEcon / QuantEcon.lectures.code / amss / lucas_stokey.py View on Github external
def __init__(self,Para,mugrid):
        '''
        Initializes the class from the calibration Para
        '''
        self.beta = Para.beta
        self.Pi = Para.Pi
        self.mc = MarkovChain(self.Pi)
        self.G = Para.G
        self.S = len(Para.Pi) # number of states
        self.Theta = Para.Theta
        self.Para = Para
        self.mugrid = mugrid
        
        #now find the first best allocation
        self.solve_time1_bellman()
        self.T.time_0 = True #Bellman equation now solves time 0 problem
github QuantEcon / QuantEcon.lectures.code / harrison_kreps / harrison_kreps_code.py View on Github external
ebb = np.linalg.matrix_power(qb, 100)


print("100th power of qb")
print(ebb)

import quantecon as qe


qa = np.array([[1./2, 1./2], [2./3, 1./3]])
qb = np.array([[2./3, 1./3], [1./4, 3./4]])

mcA = qe.MarkovChain(qa)
mcB = qe.MarkovChain(qb)

ppa = mcA.stationary_distributions
ppb = mcB.stationary_distributions

print("stationary distribution of P_a")

print(ppa)



mcB = qe.MarkovChain(qb)

ppb = mcB.stationary_distributions

print("stationary distribution of P_b")
github QuantEcon / QuantEcon.lectures.code / opt_tax_recur / lucas_stokey.py View on Github external
def __init__(self,Para,mugrid):
        '''
        Initializes the class from the calibration Para
        '''
        self.beta = Para.beta
        self.Pi = Para.Pi
        self.mc = MarkovChain(self.Pi)
        self.G = Para.G
        self.S = len(Para.Pi) # number of states
        self.Theta = Para.Theta
        self.Para = Para
        self.mugrid = mugrid

        #now find the first best allocation
        self.solve_time1_bellman()
        self.T.time_0 = True #Bellman equation now solves time 0 problem
github QuantEcon / QuantEcon.lectures.code / finite_markov / mc_convergence_plot.py View on Github external
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xticks((0.25, 0.5, 0.75))
ax.set_yticks((0.25, 0.5, 0.75))
ax.set_zticks((0.25, 0.5, 0.75))

x_vals, y_vals, z_vals = [], [], []
for t in range(20):
    x_vals.append(psi[0])
    y_vals.append(psi[1])
    z_vals.append(psi[2])
    psi = np.dot(psi, P)

ax.scatter(x_vals, y_vals, z_vals, c='r', s=60)

mc = qe.MarkovChain(P)
psi_star = mc.stationary_distributions[0]
ax.scatter(psi_star[0], psi_star[1], psi_star[2], c='k', s=60)

plt.show()
github QuantEcon / QuantEcon.lectures.code / smoothing / smoothing_actions.py View on Github external
def consumption_incomplete(cp, N_simul=150):
    """
    Computes endogenous values for the incomplete market case.

    Parameters
    ----------

    cp : instance of ConsumptionProblem
    N_simul : int

    """

    β, P, y, b0 = cp.β, cp.P, cp.y, cp.b0  # Unpack
    # For the simulation define a quantecon MC class
    mc = qe.MarkovChain(P)

    # Useful variables
    y = np.asarray(y).reshape(2, 1)
    v = np.linalg.inv(np.eye(2) - β * P) @ y

    # Simulat state path
    s_path = mc.simulate(N_simul, init=0)

    # Store consumption and debt path
    b_path, c_path = np.ones(N_simul + 1), np.ones(N_simul)
    b_path[0] = b0

    # Optimal decisions from (12) and (13)
    db = ((1 - β) * v - y) / β

    for i, s in enumerate(s_path):