<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import numpy as np <EOL> from numpy . testing import assert_almost_equal <EOL> import matplotlib . pyplot as plt <EOL> import numdifftools as ndt <EOL> import statsmodels . api as sm <EOL> from statsmodels . sandbox import tsa <EOL> from statsmodels . sandbox . tsa . garch import * <EOL> nobs = <NUM_LIT:1000> <EOL> examples = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if '<STR_LIT>' in examples : <EOL> err , h = generate_kindofgarch ( nobs , [ <NUM_LIT:1.0> , - <NUM_LIT> ] , [ <NUM_LIT:1.0> , <NUM_LIT:0.1> ] , mu = <NUM_LIT:0.5> ) <EOL> plt . figure ( ) <EOL> plt . subplot ( <NUM_LIT> ) <EOL> plt . plot ( err ) <EOL> plt . subplot ( <NUM_LIT> ) <EOL> plt . plot ( h ) <EOL> seed = <NUM_LIT> <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , seed ) <EOL> np . random . seed ( seed ) <EOL> ar1 = - <NUM_LIT> <EOL> err , h = generate_garch ( nobs , [ <NUM_LIT:1.0> , ar1 ] , [ <NUM_LIT:1.0> , <NUM_LIT> ] , mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> ) <EOL> garchplot ( err , h ) <EOL> garchplot ( err [ - <NUM_LIT> : ] , h [ - <NUM_LIT> : ] ) <EOL> np . random . seed ( seed ) <EOL> errgjr , hgjr , etax = generate_gjrgarch ( nobs , [ <NUM_LIT:1.0> , ar1 ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0.5> , <NUM_LIT:0> ] ] , mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> ) <EOL> garchplot ( errgjr [ : nobs ] , hgjr [ : nobs ] , '<STR_LIT>' ) <EOL> garchplot ( errgjr [ - <NUM_LIT> : nobs ] , hgjr [ - <NUM_LIT> : nobs ] , '<STR_LIT>' ) <EOL> np . random . seed ( seed ) <EOL> errgjr2 , hgjr2 , etax = generate_gjrgarch ( nobs , [ <NUM_LIT:1.0> , ar1 ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> ) <EOL> garchplot ( errgjr2 [ : nobs ] , hgjr2 [ : nobs ] , '<STR_LIT>' ) <EOL> garchplot ( errgjr2 [ - <NUM_LIT> : nobs ] , hgjr2 [ - <NUM_LIT> : nobs ] , '<STR_LIT>' ) <EOL> np . random . seed ( seed ) <EOL> errgjr3 , hgjr3 , etax3 = generate_gjrgarch ( nobs , [ <NUM_LIT:1.0> , ar1 ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> ) <EOL> garchplot ( errgjr3 [ : nobs ] , hgjr3 [ : nobs ] , '<STR_LIT>' ) <EOL> garchplot ( errgjr3 [ - <NUM_LIT> : nobs ] , hgjr3 [ - <NUM_LIT> : nobs ] , '<STR_LIT>' ) <EOL> np . random . seed ( seed ) <EOL> errgjr4 , hgjr4 , etax4 = generate_gjrgarch ( nobs , [ <NUM_LIT:1.0> , ar1 ] , <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> ] ] , <EOL> mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> ) <EOL> garchplot ( errgjr4 [ : nobs ] , hgjr4 [ : nobs ] , '<STR_LIT>' ) <EOL> garchplot ( errgjr4 [ - <NUM_LIT> : nobs ] , hgjr4 [ - <NUM_LIT> : nobs ] , '<STR_LIT>' ) <EOL> varinno = np . zeros ( <NUM_LIT:100> ) <EOL> varinno [ <NUM_LIT:0> ] = <NUM_LIT:1.> <EOL> errgjr5 , hgjr5 , etax5 = generate_gjrgarch ( <NUM_LIT:100> , [ <NUM_LIT:1.0> , - <NUM_LIT:0.> ] , <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> mu = <NUM_LIT:0.0> , scale = <NUM_LIT:0.1> , varinnovation = varinno ) <EOL> garchplot ( errgjr5 [ : <NUM_LIT:20> ] , hgjr5 [ : <NUM_LIT:20> ] , '<STR_LIT>' ) <EOL> seed = np . random . randint ( <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , seed ) <EOL> x = np . arange ( <NUM_LIT:20> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:2> ) <EOL> x3 = np . column_stack ( ( np . ones ( ( x . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) , x ) ) <EOL> y , inp = miso_lfilter ( [ <NUM_LIT:1.> , <NUM_LIT:0> ] , np . array ( [ [ - <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ] , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0> ] ] ) , x3 ) <EOL> nobs = <NUM_LIT:1000> <EOL> warmup = <NUM_LIT:1000> <EOL> np . random . seed ( seed ) <EOL> ar = [ <NUM_LIT:1.0> , - <NUM_LIT> ] <EOL> ma = [ [ <NUM_LIT:1.> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0.0> ] ] <EOL> errgjr4 , hgjr4 , etax4 = generate_gjrgarch ( warmup + nobs , ar , ma , <EOL> mu = <NUM_LIT> , scale = <NUM_LIT> ) <EOL> errgjr4 , hgjr4 , etax4 = errgjr4 [ warmup : ] , hgjr4 [ warmup : ] , etax4 [ warmup : ] <EOL> garchplot ( errgjr4 [ : nobs ] , hgjr4 [ : nobs ] , '<STR_LIT>' ) <EOL> ggmod = Garch ( errgjr4 - errgjr4 . mean ( ) ) <EOL> ggmod . nar = <NUM_LIT:1> <EOL> ggmod . nma = <NUM_LIT:1> <EOL> ggmod . _start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.0> ] ) <EOL> ggres = ggmod . fit ( start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.0> ] ) , maxiter = <NUM_LIT:1000> ) <EOL> print ( '<STR_LIT>' , ggres . params ) <EOL> garchplot ( ggmod . errorsest , ggmod . h , title = '<STR_LIT>' ) <EOL> ggmod0 = Garch0 ( errgjr4 - errgjr4 . mean ( ) ) <EOL> ggmod0 . nar = <NUM_LIT:1> <EOL> ggmod . nma = <NUM_LIT:1> <EOL> start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] ) <EOL> ggmod0 . _start_params = start_params <EOL> ggres0 = ggmod0 . fit ( start_params = start_params , maxiter = <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , ggres0 . params ) <EOL> ggmod0 = Garch0 ( errgjr4 - errgjr4 . mean ( ) ) <EOL> ggmod0 . nar = <NUM_LIT:1> <EOL> ggmod . nma = <NUM_LIT:1> <EOL> start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] ) <EOL> ggmod0 . _start_params = start_params <EOL> ggres0 = ggmod0 . fit ( start_params = start_params , method = '<STR_LIT>' , maxiter = <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , ggres0 . params ) <EOL> g11res = optimize . fmin ( lambda params : - loglike_GARCH11 ( params , errgjr4 - errgjr4 . mean ( ) ) [ <NUM_LIT:0> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> print ( g11res ) <EOL> llf = loglike_GARCH11 ( g11res , errgjr4 - errgjr4 . mean ( ) ) <EOL> print ( llf [ <NUM_LIT:0> ] ) <EOL> if '<STR_LIT>' in examples : <EOL> from rpy import r <EOL> r . library ( '<STR_LIT>' ) <EOL> f = r . formula ( '<STR_LIT>' ) <EOL> fit = r . garchFit ( f , data = errgjr4 - errgjr4 . mean ( ) , include_mean = False ) <EOL> if '<STR_LIT>' in examples : <EOL> from rpy import r <EOL> f = r . formula ( '<STR_LIT>' ) <EOL> x = r . garchSim ( n = <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , tsa . acf ( np . power ( x , <NUM_LIT:2> ) ) [ : <NUM_LIT:15> ] ) <EOL> arma3 = Arma ( np . power ( x , <NUM_LIT:2> ) ) <EOL> arma3res = arma3 . fit ( start_params = [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.5> ] , maxiter = <NUM_LIT> ) <EOL> print ( arma3res . params ) <EOL> arma3b = Arma ( np . power ( x , <NUM_LIT:2> ) ) <EOL> arma3bres = arma3b . fit ( start_params = [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.5> ] , maxiter = <NUM_LIT> , method = '<STR_LIT>' ) <EOL> print ( arma3bres . params ) <EOL> xr = r . garchSim ( n = <NUM_LIT:100> ) <EOL> x = np . asarray ( xr ) <EOL> ggmod = Garch ( x - x . mean ( ) ) <EOL> ggmod . nar = <NUM_LIT:1> <EOL> ggmod . nma = <NUM_LIT:1> <EOL> ggmod . _start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.0> ] ) <EOL> ggres = ggmod . fit ( start_params = np . array ( [ - <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.0> ] ) , maxiter = <NUM_LIT:1000> ) <EOL> print ( '<STR_LIT>' , ggres . params ) <EOL> g11res = optimize . fmin ( lambda params : - loglike_GARCH11 ( params , x - x . mean ( ) ) [ <NUM_LIT:0> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> print ( g11res ) <EOL> llf = loglike_GARCH11 ( g11res , x - x . mean ( ) ) <EOL> print ( llf [ <NUM_LIT:0> ] ) <EOL> garchplot ( ggmod . errorsest , ggmod . h , title = '<STR_LIT>' ) <EOL> fit = r . garchFit ( f , data = x - x . mean ( ) , include_mean = False , trace = False ) <EOL> print ( r . summary ( fit ) ) <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> erro , ho , etaxo = generate_gjrgarch ( <NUM_LIT:20> , ar , ma , mu = <NUM_LIT> , scale = <NUM_LIT> , <EOL> varinnovation = np . ones ( <NUM_LIT:20> ) ) <EOL> if '<STR_LIT>' in examples : <EOL> import tabular as tb <EOL> import scikits . timeseries as ts <EOL> a = tb . loadSV ( r'<STR_LIT>' ) <EOL> s = ts . time_series ( a [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ : : - <NUM_LIT:1> ] , <EOL> dates = ts . date_array ( a [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ : : - <NUM_LIT:1> ] , freq = "<STR_LIT:D>" ) ) <EOL> sp500 = a [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ : : - <NUM_LIT:1> ] <EOL> sp500r = np . diff ( np . log ( sp500 ) ) </s>
