tpemartin / 112-2-programming-for-economic-modeling

MIT License
0 stars 1 forks source link

Exercise AR(1) process #12

Open tpemartin opened 1 year ago

12Maggie11 commented 1 year ago
import numpy as np
class AR1:
    def __init__(self, rho, X0):
        self.rho = rho
        self.X0 = X0
        self.X = X0
        self.simulated_dynamics = [[X0]]
    def simulate_onePeriod(self):
        """
        return a tuple of (Xnext)
        and track the dynamics in self.simulate_dynamics
        """ 
        sigma = 0.2
        epsilon = np.random.normal(0, sigma, 1)

        Xnext = self.rho*self.X+epsilon

        # keep track of the dynamics
        self.simulated_dynamics.append([Xnext])

        # set the market time to the next period
        self.X = Xnext
        return (Xnext)

ar1=AR1(rho=0.9, X0=0.1)
ar1.simulate_onePeriod()

# simulate the dynamics for 10 periods
for t in range(10):
    _ = ar1.simulate_onePeriod()

ar1.simulated_dynamics