代码在索引中使用了错误
import numpy as np
def generate_y(phi,sigma,y_,T,epsilon):
生成数学:
y_t
跟随:数学:
y_t = \phi_0+\phi_1y_{t-1}+\phi_2 y_{t-2} + \sigma \epsilon_t
输入 ----------- * phi(列表或数组):[phi_0,phi_1,phi_2]
- sigma(float):: math:
\sigma
y_(列表或数组):: math:
[y_{-1},y_{-2}]
T(int):要模拟的句点数
- epsilon(长度T + 1 numpy数组):>:math:
[\epsilon_0,\epsilon_1,...,\epsilon_T]
Returns
-----------
* y(length T+1 numpy array) : :math:`[y_0,y_1,...,y_T]`
ret = np.ones(T+1)
ret[0] = phi[0]+(phi[1]*y_[0])+(phi[2]*y_[1])+(sigma*epsilon[0])
ret[1] = phi[0]+(phi[1]*ret[0])+(phi[2]*y_[0])+(sigma*epsilon[1])
for i in range(2,T+1):
ret[i] = phi[0]+(phi[1]*ret[i-1])+(phi[2]*ret[i-2])+(sigma*epsilon[i])
return ret
def generate_N_y(phi,sigma,y_,N,T):
'''r
Generates N realizations of y^i_t following
:math:`y^i_t = \phi_0+\phi_1y^i_{t-1}+\phi_2 y^i_{t-2} + \sigma \epsilon^i_t`
Inputs
-----------
* phi (list or array) : [phi_0,phi_1,phi_2]
* sigma (float) : :math:`\sigma`
* y_ (list or array) : :math:`[y_{-1},y_{-2}]`
* N (int) : number of simulations
* T (int) : number of periods to simulate
Returns
-----------
* y(Nx(T+1) numpy array) : y[i,t] is the realization of y at time t for simulation i
'''
Tlist = []
for i in range(0,N+1):
epsilon = np.ones(T+1)
for j in range (0,T+1):
epsilon[j] = np.random.randn()
temp = generate_y(phi, sigma, y_, T, epsilon)
Tlist.append(temp)
ret = np.vstack(Tlist)
return ret
def generate_AC(phi,sigma):
r'''
Create A,C for the process
:math:`x_{t} = A x_{t-1} + C\epsilon_{t}`
Inputs
-----------
* phi (list or array) : [phi_0,phi_1,phi_2]
* sigma (float) : :math:`\sigma`
Returns
-----------
* A ( :math:`(3\times 3)` array)
* C (length 3 array)
'''
A = np.array([[phi[1],phi[2],phi[0]],[1,0,0],[0,0,1]])
C = np.array([1,0,0])
return A,C
def generate_x(A,C,x_,T,epsilon):
r'''
Generates :math:`x_t` following
:math:`x_{t} = A x_{t-1} + C\epsilon_{t}`
Inputs
-----------
* A (:math:`m\times m` array)
* C (length m array)
* x_ (length m array) : :math:`x_{-1}`
* T (int) : number of periods to simulate
* epsilon (length T+1 numpy array) : :math:`[\epsilon_0,\epsilon_1,...,\epsilon_T]`
Returns
-----------
* x(length :math:`m\times(T+1) numpy array) : x[:,t] is the array :math:`x_t`
'''
stack = [1]
stack[0] = A.dot(x_)+(C*epsilon[0])
for i in range(1,T+1):
stack.append(A.dot(stack[i-1])+(C*epsilon[i]))
ret = np.vstack(stack)
ret = ret.T
return ret
def generate_xhat(A,xhat_,T):
r'''
Generates :math:`\hat x_t` following
:math:`\hat x_{t} = A \hat x_{t-1}`
Inputs
-----------
* A (:math:`m\times m` array)
* xhat_ (length m array) : :math:`\hat x_{-1}`
* T (int) : number of periods to simulate
Returns
-----------
* xhat(length :math:`m\times(T+1) numpy array) : xhat[:,t] is the array
:math:`\hat x_t`
'''
stack = [1]
stack[0]=A.dot(xhat_)
for i in range(1,T+1):
stack.append(A.dot(stack[i-1]))
ret = np.vstack(stack)
ret = ret.T
return ret
中的RAN
import numpy as np
import HW1_1
import matplotlib.pyplot as plt
'''
Parameters
'''
phi_0,phi_1,phi_2 = 1.,0.95,-0.5
sigma = 1
y_ = np.array([0.,0.])
T=50
phi = np.array([phi_0,phi_1,phi_2])
第1部分.b。
y = HW1_1.generate_N_y(phi,sigma,y_,10,T)
plt.figure(1)
for i in range(0,10):
plt.plot(y[i,:])
##############
# Part 1. d. #
##############
x_ = np.hstack([y_,1.])
T=50
epsilon = np.ones(T+1)
for i in range(0,T):
epsilon[i] = np.random.randn()
A,C = HW1_1.generate_AC(phi,sigma)
y = HW1_1.generate_y(phi,sigma,y_,T,epsilon)
x = HW1_1.generate_x(A,C,x_,T,epsilon)
plt.figure(2)
plt.plot(y)
plt.plot(x[0,:])
print(y==x[0,:])
##############
# Part 1. e. #
##############
##############
y = HW1_1.generate_N_y(phi,sigma,y_,500,T)
A,C = HW1_1.generate_AC(phi,sigma)
xhat = HW1_1.generate_xhat(A,x_,T)
ybar = np.mean(y,axis=0)
plt.figure(3)
plt.plot(ybar)
plt.plot(xhat[0,:])
xbar = np.mean(xhat,axis=0)
phi_0,phi_1,phi_2 = 1.,0.95,-0.5
sigma = 1
y_ = np.array([0.,0.])
任何帮助都会很棒!