我有一个用于GARCH建模的示例python代码,它创建了一个随机生成数字的数据。我想用我自己的包含列向量的csv文件替换数字,但这不起作用。
import numpy as np
from scipy import optimize
r=np.array([0.945532630498276,
0.614772790142383,
0.834417758890680,
0.862344782601800,
0.555858715401929,
0.641058419842652,
0.720118656981704,
0.643948007732270,
0.138790608092353,
0.279264178231250,
0.993836948076485,
0.531967023876420,
0.964455754192395,
0.873171802181126,
0.937828816793698])
print r
f=open('main_ax.csv')
b=f.read()
r=np.array(b)
print r
def GARCH11_logL(param, r):
omega, alpha, beta = param
n = len(r)
s = np.ones(n)
for i in range(3,n):
s[i] = omega + alpha*r[i-1]**2 + beta*(s[i-1]) # GARCH(1,1) model
logL = -( (-np.log(s) - r**2/s).sum() ) # calculate the sum
return logL
R = optimize.fmin(GARCH11_logL,np.array([.1,.1,.1]),args=(r,),full_output=1)
print R
print
print("omega = %.6f\nbeta = %.6f\nalpha = %.6f\n") % (R[0][0],R[0][2],R[0][1])
此程序抛出以下错误:
Traceback (most recent call last):
File "C:\Users\Gaurav\Desktop\Ram\garch_model.py", line 35, in <module>
R = optimize.fmin(GARCH11_logL,np.array([.1,.1,.1]),args=(r,),full_output=1)
File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 377, in fmin
res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 435, in _minimize_neldermead
fsim[0] = func(x0)
File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 285, in function_wrapper
return function(*(wrapper_args + args))
File "C:\Users\Gaurav\Desktop\Ram\garch_model.py", line 32, in GARCH11_logL
s[i] = omega + alpha*r[i-1]**2 + beta*(s[i-1]) # GARCH(1,1) model
TypeError: unsupported operand type(s) for ** or pow(): 'numpy.string_' and 'int'
任何修复此代码的帮助都会非常有用。
答案 0 :(得分:0)
行
pd.Series([pd.Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London'),
pd.Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels')])
使用单个元素创建一个数组,该数组是一个字符串。您尚未将文件内容转换为数字数组。
您可以通过调用numpy.loadtxt
来替换该代码:
f=open('main_ax.csv')
b=f.read()
r=np.array(b)