示例#1
0
#print dta2

aa = np.genfromtxt(r'gspc_table.csv', skip_header=0, delimiter=',', names=True)

cl = aa['Close']
ret = np.diff(np.log(cl))[-2000:] * 1000.

ggmod = Garch(ret - ret.mean())  #hgjr4[:nobs])#-hgjr4.mean()) #errgjr4)
ggmod.nar = 1
ggmod.nma = 1
ggmod._start_params = np.array([-0.1, 0.1, 0.1, 0.1])
ggres = ggmod.fit(start_params=np.array([-0.1, 0.1, 0.1, 0.0]),
                  maxiter=1000,
                  method='bfgs')
print('ggres.params', ggres.params)
garchplot(ggmod.errorsest, ggmod.h, title='Garch estimated')

use_rpy = False
if use_rpy:
    from rpy import r
    r.library('fGarch')
    f = r.formula('~garch(1, 1)')
    fit = r.garchFit(f, data=ret - ret.mean(), include_mean=False)
    f = r.formula('~arma(1,1) + ~garch(1, 1)')
    fit = r.garchFit(f, data=ret)

ggmod0 = Garch0(ret - ret.mean())  #hgjr4[:nobs])#-hgjr4.mean()) #errgjr4)
ggmod0.nar = 1
ggmod.nma = 1
start_params = np.array([-0.1, 0.1, ret.var()])
ggmod0._start_params = start_params  #np.array([-0.6, 0.1, 0.2, 0.0])
示例#2
0
    print('seed', seed)
    np.random.seed(seed)
    ar1 = -0.9
    err, h = generate_garch(nobs, [1.0, ar1], [1.0, 0.50], mu=0.0, scale=0.1)
    #    plt.figure()
    #    plt.subplot(211)
    #    plt.plot(err)
    #    plt.subplot(212)
    #    plt.plot(h)
    #    plt.figure()
    #    plt.subplot(211)
    #    plt.plot(err[-400:])
    #    plt.subplot(212)
    #    plt.plot(h[-400:])
    #plt.show()
    garchplot(err, h)
    garchplot(err[-400:], h[-400:])

    np.random.seed(seed)
    errgjr, hgjr, etax = generate_gjrgarch(nobs, [1.0, ar1],
                                           [[1, 0], [0.5, 0]],
                                           mu=0.0,
                                           scale=0.1)
    garchplot(errgjr[:nobs], hgjr[:nobs],
              'GJR-GARCH(1,1) Simulation - symmetric')
    garchplot(errgjr[-400:nobs], hgjr[-400:nobs],
              'GJR-GARCH(1,1) Simulation - symmetric')

    np.random.seed(seed)
    errgjr2, hgjr2, etax = generate_gjrgarch(nobs, [1.0, ar1],
                                             [[1, 0], [0.1, 0.9]],
示例#3
0
#print dta2

aa=np.genfromtxt(r'gspc_table.csv', skip_header=0, delimiter=',', names=True)

cl = aa['Close']
ret = np.diff(np.log(cl))[-2000:]*1000.

ggmod = Garch(ret - ret.mean())#hgjr4[:nobs])#-hgjr4.mean()) #errgjr4)
ggmod.nar = 1
ggmod.nma = 1
ggmod._start_params = np.array([-0.1, 0.1, 0.1, 0.1])
ggres = ggmod.fit(start_params=np.array([-0.1, 0.1, 0.1, 0.0]),
                  maxiter=1000,method='bfgs')
print('ggres.params', ggres.params)
garchplot(ggmod.errorsest, ggmod.h, title='Garch estimated')

use_rpy = False
if use_rpy:
    from rpy import r
    r.library('fGarch')
    f = r.formula('~garch(1, 1)')
    fit = r.garchFit(f, data = ret - ret.mean(), include_mean=False)
    f = r.formula('~arma(1,1) + ~garch(1, 1)')
    fit = r.garchFit(f, data = ret)


ggmod0 = Garch0(ret - ret.mean())#hgjr4[:nobs])#-hgjr4.mean()) #errgjr4)
ggmod0.nar = 1
ggmod.nma = 1
start_params = np.array([-0.1, 0.1, ret.var()])
示例#4
0
    print('seed', seed)
    np.random.seed(seed)
    ar1 = -0.9
    err,h = generate_garch(nobs, [1.0, ar1], [1.0,  0.50], mu=0.0,scale=0.1)
#    plt.figure()
#    plt.subplot(211)
#    plt.plot(err)
#    plt.subplot(212)
#    plt.plot(h)
#    plt.figure()
#    plt.subplot(211)
#    plt.plot(err[-400:])
#    plt.subplot(212)
#    plt.plot(h[-400:])
    #plt.show()
    garchplot(err, h)
    garchplot(err[-400:], h[-400:])


    np.random.seed(seed)
    errgjr,hgjr, etax = generate_gjrgarch(nobs, [1.0, ar1],
                                [[1,0],[0.5,0]], mu=0.0,scale=0.1)
    garchplot(errgjr[:nobs], hgjr[:nobs], 'GJR-GARCH(1,1) Simulation - symmetric')
    garchplot(errgjr[-400:nobs], hgjr[-400:nobs], 'GJR-GARCH(1,1) Simulation - symmetric')

    np.random.seed(seed)
    errgjr2,hgjr2, etax = generate_gjrgarch(nobs, [1.0, ar1],
                                [[1,0],[0.1,0.9]], mu=0.0,scale=0.1)
    garchplot(errgjr2[:nobs], hgjr2[:nobs], 'GJR-GARCH(1,1) Simulation')
    garchplot(errgjr2[-400:nobs], hgjr2[-400:nobs], 'GJR-GARCH(1,1) Simulation')