Ejemplo n.º 1
0
    def __init__(self, y_0=100, y_1=100, α=0.8, β=0.9, γ=10, σ=1, g=10):

        self.α, self.β = α, β
        self.y_0, self.y_1, self.g = y_0, y_1, g
        self.γ, self.σ = γ, σ

        # Define intial conditions
        self.μ_0 = [1, y_0, y_1]

        self.ρ1 = α + β
        self.ρ2 = -β

        # Define transition matrix
        self.A = [[1, 0, 0], [γ + g, self.ρ1, self.ρ2], [0, 1, 0]]

        # Define output matrix
        self.G = [
            [γ + g, self.ρ1, self.ρ2],  # this is Y_{t+1}
            [γ, α, 0],  # this is C_{t+1}
            [0, β, -β]
        ]  # this is I_{t+1}

        self.C = np.zeros((3, 1))
        self.C[1] = σ  # stochastic

        # Initialize LSS with parameters from Samuleson model
        LinearStateSpace.__init__(self, self.A, self.C, self.G, mu_0=self.μ_0)
Ejemplo n.º 2
0
    def load_matrices(self, A, C, G):
        self.A = A
        self.C = C
        self.G = G

        self.ar = LinearStateSpace(A, C, G, mu_0=np.ones(4))
        x, y = self.ar.simulate(ts_length=2000)

        y = y.flatten()
        self.__y_max = 1.3 * np.max(y)
        self.__y_min = 1.3 * np.min(y)
Ejemplo n.º 3
0
class Ensemble_plot:
    def __init__(self):
        print("nyan")

    def load_matrices(self, A, C, G):
        self.A = A
        self.C = C
        self.G = G

        self.ar = LinearStateSpace(A, C, G, mu_0=np.ones(4))
        x, y = self.ar.simulate(ts_length=2000)

        y = y.flatten()
        self.__y_max = 1.3 * np.max(y)
        self.__y_min = 1.3 * np.min(y)

    def set_plot(self):
        self.fig, self.ax = plt.subplots(figsize=(8, 5))

        self.ax.set_ylim(self.__y_min, self.__y_max)
        self.ax.set_xlabel("time", fontsize=16)
        self.ax.set_ylabel("y_t", fontsize=16)

    def calc_ensemble_mean(self):

        self.ensemble_mean = np.zeros(T)
        for i in range(I):
            x, y = self.ar.simulate(ts_length=T)
            y = y.flatten()

            self.ax.plot(y, "c~", lw=0.8, alpha=0.5)
            self.ensemble_mean = self.ensemble_mean + y

        self.ensemble_mean = self.ensemble_mean / I
        self.ax.plot(self.ensemble_mean,
                     color="b",
                     lw=2,
                     alpha=0.8,
                     label=r'$\bar y_t$')

    def calc_moment(self):
        print()
Ejemplo n.º 4
0
def likelihood_params(phi1, phi2, psi, data, sigma_v):
    # For the given matrices the moments for the likelihood function are obtained using a Kalman filter
    ss = LinearStateSpace(phi1, phi2, psi, np.ones((1, 1)) * np.sqrt(sigma_v))
    kalman = Kalman(
        ss, [0, 0, 1],
        [[1000, 1000, 1000], [1000, 1000, 1000], [1000, 1000, 1000]])
    mu = []
    sigma = []
    for i in range(len(data)):
        kalman.update(data[i])
        mu.append(psi.dot(kalman.x_hat))
        sigma.append(psi.dot(kalman.Sigma).dot(np.transpose(psi)) + sigma_v**2)

    return mu[-1], sigma[-1]
Ejemplo n.º 5
0
import numpy as np
import matplotlib.pyplot as plt
from quantecon import LinearStateSpace
from scipy.stats import norm
import random

phi_0, phi_1, phi_2 = 1.1, 0.8, -0.8

A = [[1, 0, 0], [phi_0, phi_1, phi_2], [0, 1, 0]]

C = np.ones((3, 1))
G = [0, 1, 0]

ar = LinearStateSpace(A, C, G, mu_0=np.ones(3))
x, y = ar.simulate(ts_length=50)

fig, ax = plt.subplots(figsize=(8, 4.6))

y = y.flatten()
print(y)
ax.plot(y, "-b", lw=2, alpha=0.7)
ax.grid()

ax.set_xlabel("time")
ax.set_ylabel("y")
plt.show()

##calc esemble_mean

esemble_mean = np.zeros(T)
I = 20
phi_1, phi_2, phi_3, phi_4 = 0.5, -0.2, 0, 0.5
sigma = 0.1

A = [[phi_1, phi_2, phi_3, phi_4],
     [1,     0,     0,     0],
     [0,     1,     0,     0],
     [0,     0,     1,     0]]
C = [sigma, 0, 0, 0]
G = [1, 0, 0, 0]

T0 = 10
T1 = 50
T2 = 75
T4 = 100

ar = LinearStateSpace(A, C, G, mu_0=np.ones(4))
ymin, ymax = -0.8, 1.25

fig, ax = plt.subplots(figsize=(8, 5))

ax.grid(alpha=0.4)
ax.set_ylim(ymin, ymax)
ax.set_ylabel(r'$y_t$', fontsize=16)
ax.vlines((T0, T1, T2), -1.5, 1.5)

ax.set_xticks((T0, T1, T2))
ax.set_xticklabels((r"$T$", r"$T'$", r"$T''$"), fontsize=14)

sample = []
for i in range(80):
    rcolor = random.choice(('c', 'g', 'b'))
Ejemplo n.º 7
0
import matplotlib.pyplot as plt
from scipy.stats import norm
from quantecon import LinearStateSpace

phi_1, phi_2, phi_3, phi_4 = 0.5, -0.2, 0, 0.5
sigma = 0.1

A = [[phi_1, phi_2, phi_3, phi_4],
     [1,     0,     0,     0],
     [0,     1,     0,     0],
     [0,     0,     1,     0]]
C = [[sigma], [0], [0], [0]]
G = [1, 0, 0, 0]

T = 30
ar = LinearStateSpace(A, C, G)

ymin, ymax = -0.8, 1.25

fig, ax = plt.subplots(figsize=(8, 4))

ax.set_xlim(ymin, ymax)
ax.set_xlabel(r'$y_t$', fontsize=16)

x, y = ar.replicate(T=T, num_reps=100000)
mu_x, mu_y, Sigma_x, Sigma_y = ar.stationary_distributions()
f_y = norm(loc=float(mu_y), scale=float(np.sqrt(Sigma_y)))

y = y.flatten()
ax.hist(y, bins=50, normed=True, alpha=0.4)
Ejemplo n.º 8
0
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from quantecon import LinearStateSpace

phi_1, phi_2, phi_3, phi_4 = 0.5, -0.2, 0, 0.5
sigma = 0.1

A = [[phi_1, phi_2, phi_3, phi_4], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
C = [sigma, 0, 0, 0]
G = [1, 0, 0, 0]

T = 30
ar = LinearStateSpace(A, C, G)

ymin, ymax = -0.8, 1.25

fig, ax = plt.subplots(figsize=(8, 4))

ax.set_xlim(ymin, ymax)
ax.set_xlabel(r'$y_t$', fontsize=16)

x, y = ar.replicate(T=T, num_reps=100000)
mu_x, mu_y, Sigma_x, Sigma_y = ar.stationary_distributions()
f_y = norm(loc=float(mu_y), scale=float(np.sqrt(Sigma_y)))

y = y.flatten()
ax.hist(y, bins=50, normed=True, alpha=0.4)

ygrid = np.linspace(ymin, ymax, 150)
ax.plot(ygrid, f_y.pdf(ygrid), 'k-', lw=2, alpha=0.8, label='true density')
g = 10
n = 100

A = [[1,        0,      0],
     [γ + g,   ρ1,     ρ2],
     [0,        1,      0]]

G = [[γ + g, ρ1,   ρ2],         # this is Y_{t+1}
     [γ,      α,    0],         # this is C_{t+1}
     [0,      β,   -β]]         # this is I_{t+1}

μ_0 = [1, 100, 100]
C = np.zeros((3,1))
C[1] = σ # stochastic

sam_t = LinearStateSpace(A, C, G, mu_0=μ_0)

x, y = sam_t.simulate(ts_length=n)

fig, axes = plt.subplots(3, 1, sharex=True, figsize=(12, 8))
titles = ['Output ($Y_t$)', 'Consumption ($C_t$)', 'Investment ($I_t$)']
colors = ['darkblue', 'red', 'purple']
for ax, series, title, color in zip(axes, y, titles, colors):
    ax.plot(series, color=color)
    ax.set(title=title, xlim=(0, n))
    ax.grid()

axes[-1].set_xlabel('Iteration')

plt.show()
n = 100

A = [[1, 0, 0], [γ + g, ρ1, ρ2], [0, 1, 0]]

G = [
    [γ + g, ρ1, ρ2],  # this is Y_{t+1}
    [γ, α, 0],  # this is C_{t+1}
    [0, β, -β]
]  # this is I_{t+1}

μ_0 = [1, 100, 100]
C = np.zeros((3, 1))
# stochastic
C[1] = σ

sam_t = LinearStateSpace(A, C, G, mu_0=μ_0)

x, y = sam_t.simulate(ts_length=n)

fig, axes = plt.subplots(3, 1, sharex=True, figsize=(12, 8))
titles = ['Output ($Y_t$)', 'Consumption ($C_t$)', 'Investment ($I_t$)']
colors = ['darkblue', 'red', 'purple']
for ax, series, title, color in zip(axes, y, titles, colors):
    ax.plot(series, color=color)
    ax.set(title=title, xlim=(0, n))
    ax.grid()

axes[-1].set_xlabel('Iteration')

plt.show()
Ejemplo n.º 11
0
import numpy as np
import matplotlib.pyplot as plt
from quantecon import Kalman
from quantecon import LinearStateSpace
from scipy.stats import norm

theta = 10
A, C, G, H = 1, 0, 1, 1
#mu_0は  x0 の初期分布の平均パラメータ
ss = LinearStateSpace(A, C, G, H, mu_0=theta)

x_hat_0, Sigma_0 = 8, 1
kalman = Kalman(ss, x_hat_0, Sigma_0)

N = 5
x, y = ss.simulate(N)
y = y.flatten()
print(y)

#
fig, ax = plt.subplots(figsize=(10, 8))
xgrid = np.linspace(theta - 5, theta + 2, 200)

for i in range(N):

    m, v = [float(z) for z in (kalman.x_hat, kalman.Sigma)]
    print(m)
    ax.plot(xgrid,
            norm.pdf(xgrid, loc=m, scale=np.sqrt(v)),
            label=r't={}'.format(i))
    kalman.update(y[i])