Index Error with python code and numpy matrix using spyder -


code used error in indexing

import numpy np  def generate_y(phi,sigma,y_,t,epsilon): 

generates math:y_t following

:math:y_t = \phi_0+\phi_1y_{t-1}+\phi_2 y_{t-2} + \sigma \epsilon_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}]

  • t (int) : number of periods simulate

  • epsilon (length t+1 numpy array) : >: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])  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 simulate    returns  -----------  * y(nx(t+1) numpy array) : y[i,t] realization of y @ time t simulation  '''  tlist = [] in range(0,n+1):     epsilon = np.ones(t+1) 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 process   :math:`x_{t} = x_{t-1} + c\epsilon_{t}`   inputs  -----------  * phi (list or array) : [phi_0,phi_1,phi_2]   * sigma (float) : :math:`\sigma`    returns  -----------  * ( :math:`(3\times 3)` array)  * c (length 3 array)  '''   = 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} = x_{t-1} + c\epsilon_{t}`    inputs  -----------  * (:math:`m\times m` array)   * c (length m array)   * x_ (length m array) : :math:`x_{-1}`   * t (int) : number of periods 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] array :math:`x_t`  '''  stack = [1]   stack[0] = a.dot(x_)+(c*epsilon[0])  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} = \hat x_{t-1}`    inputs  -----------  * (:math:`m\times m` array)   * xhat_ (length m array) : :math:`\hat x_{-1}`   * t (int) : number of periods simulate    returns  -----------  * xhat(length :math:`m\times(t+1) numpy array) : xhat[:,t] array   :math:`\hat x_t`  '''   stack = [1]  stack[0]=a.dot(xhat_)  in range(1,t+1):   stack.append(a.dot(stack[i-1]))  ret = np.vstack(stack)  ret = ret.t  return ret 

ran in

import numpy np import hw1_1 import matplotlib.pyplot 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]) 

part 1. b.

y = hw1_1.generate_n_y(phi,sigma,y_,10,t)  plt.figure(1) in range(0,10):  plt.plot(y[i,:])    ############## # part 1. d. # ############## x_ = np.hstack([y_,1.]) t=50 epsilon = np.ones(t+1) 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.]) 

any awesome!


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -