예제 #1
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()
예제 #2
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
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'))
    x, y = ar.simulate(ts_length=T4)
    y = y.flatten()
    ax.plot(y, color=rcolor, lw=0.8, alpha=0.5)
    ax.plot((T0, T1, T2), (y[T0], y[T1], y[T2],), 'ko', alpha=0.5)

plt.show()
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()
예제 #5
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])