# Problem 1. > setwd("C:/teaching/bs41202") > library(fSeries) > source("ohlc_R.txt") > source("reverse.txt") > source("syz_R.txt") > da=read.table("d-catohlc0107.txt",header=T) > m1=reverse(da) > cat=m1$series > op=cat[,4] > hi=cat[,5] > lo=cat[,6] > cl=cat[,7] > ra=op[2:1593]/op[1:1592] > idx=c(1:1592)[ra<0.6] > idx [1] 1137 > ra[1137] [1] 0.5072757 > sp=c(idx,0.5) > mm=ohlc(op,hi,lo,cl,nsplit=1,split=sp) > names(mm) [1] "s0sq" "s1sq" "s2sq" "s3sq" "s5sq" "s6sq" > basicStats(sqrt(mm$s0sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.000000 Maximum 10.020000 1. Quartile 0.290000 3. Quartile 1.170000 Mean 0.824548 Median 0.640000 Sum 1312.680000 SE Mean 0.018455 LCL Mean 0.788349 UCL Mean 0.860747 Variance 0.542227 Stdev 0.736360 Skewness 2.563602 Kurtosis 17.890003 > basicStats(sqrt(mm$s1sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.000000 Maximum 7.095886 1. Quartile 0.443059 3. Quartile 1.489218 Mean 1.065322 Median 0.843305 Sum 1695.991936 SE Mean 0.021046 LCL Mean 1.024040 UCL Mean 1.106603 Variance 0.705170 Stdev 0.839744 Skewness 1.683421 Kurtosis 4.538825 > basicStats(sqrt(mm$s2sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.270262 Maximum 2.762682 1. Quartile 0.600583 3. Quartile 1.045015 Mean 0.862561 Median 0.780758 Sum 1373.197109 SE Mean 0.009150 LCL Mean 0.844614 UCL Mean 0.880508 Variance 0.133279 Stdev 0.365074 Skewness 1.378160 Kurtosis 2.762985 > basicStats(sqrt(mm$s3sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.472960 Maximum 4.984749 1. Quartile 1.071635 3. Quartile 1.832196 Mean 1.526625 Median 1.387349 Sum 2430.386311 SE Mean 0.016284 LCL Mean 1.494683 UCL Mean 1.558566 Variance 0.422174 Stdev 0.649749 Skewness 1.453185 Kurtosis 3.141615 > basicStats(sqrt(mm$s5sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.267769 Maximum 2.918949 1. Quartile 0.629909 3. Quartile 1.041071 Mean 0.868393 Median 0.799305 Sum 1382.482206 SE Mean 0.008599 LCL Mean 0.851528 UCL Mean 0.885259 Variance 0.117707 Stdev 0.343084 Skewness 1.226268 Kurtosis 2.166977 > basicStats(sqrt(mm$s6sq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.482675 Maximum 5.278869 1. Quartile 1.138382 3. Quartile 1.882684 Mean 1.576747 Median 1.449367 Sum 2510.180579 SE Mean 0.015681 LCL Mean 1.545989 UCL Mean 1.607505 Variance 0.391474 Stdev 0.625679 Skewness 1.271950 Kurtosis 2.320136 > Problem 2 > m2=syz(op,hi,lo,cl,nsplit=1,split=sp) > names(m2) [1] "syzsq" > basicStats(sqrt(m2$syzsq)) round.ans..digits...6. nobs 1592.000000 NAs 0.000000 Minimum 0.000000 Maximum 0.032119 1. Quartile 0.015083 3. Quartile 0.020866 Mean 0.018150 Median 0.017795 Sum 28.894791 SE Mean 0.000141 LCL Mean 0.017873 UCL Mean 0.018427 Variance 0.000032 Stdev 0.005643 Skewness -0.663081 Kurtosis 2.545673 Problem 3 > da=read.table("m-ge2606.txt") > dim(da) [1] 972 2 > y=da[4:936,2] > gein=cbind(da[3:935,2],da[2:934,2],da[1:933,2]) > mge=nnet(gein,y,size=3,linout=T,skip=T) > yp=da[937:972,2] > xp=cbind(da[936:971,2],da[935:970,2],da[934:969,2]) > pre=predict(mge,xp) > ssfe=sum((yp-pre)^2)/36 > ssfe [1] 0.001202994 > var(yp) [1] 0.001201248 Problem 4 > yd=ifelse(y>0,1,0) > ypd=ifelse(yp>0,1,0) > mged=nnet(gein,yd,size=3,linout=F,skip=T) # weights: 19 ... stopped after 100 iterations > summary(mged) a 3-3-1 network with 19 weights options were - skip-layer connections b->h1 i1->h1 i2->h1 i3->h1 -13.47 49.16 17.05 -3.82 b->h2 i1->h2 i2->h2 i3->h2 2.07 3.21 -0.74 1.14 b->h3 i1->h3 i2->h3 i3->h3 -6.90 24.43 -11.99 9.88 b->o h1->o h2->o h3->o i1->o i2->o i3->o 2.36 25.67 -2.39 -8.80 2.24 -2.14 0.52 > yfit=predict(mged,xp) > plot(yfit,type='l') > yfitd=ifelse(yfit>0.55,1,0) > ypd [1] 1 0 0 0 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 1 1 > yfitd [,1] [1,] 1 [2,] 0 [3,] 0 [4,] 1 [5,] 1 [6,] 1 [7,] 1 [8,] 0 [9,] 0 [10,] 1 [11,] 0 [12,] 1 [13,] 1 [14,] 0 [15,] 1 [16,] 1 [17,] 0 [18,] 1 [19,] 0 [20,] 1 [21,] 0 [22,] 1 [23,] 1 [24,] 1 [25,] 0 [26,] 0 [27,] 1 [28,] 1 [29,] 0 [30,] 1 [31,] 1 [32,] 1 [33,] 1 [34,] 1 [35,] 0 [36,] 1 > icnt=0 > for(i in 1:36){ + if(yfitd[i,1]==ypd[i]){ + icnt=icnt+1 + } + } > icnt [1] 21 > Problem 5 > source("garchoxfit_R.txt") > ge=da[,2] > acf(ge) > pacf(ge) > ma=garchOxFit(formula.mean=~arma(3,0),formula.var=~gjr(1,1),series=ge,cond.dist="t") ******************** ** SPECIFICATIONS ** ******************** Dependent variable : X Mean Equation : ARMA (3, 0) model. No regressor in the mean Variance Equation : GJR (1, 1) model. No regressor in the variance The distribution is a Student distribution, with 9.64988 degrees of freedom. Strong convergence using numerical derivatives Log-likelihood = 1238.32 Maximum Likelihood Estimation (Std.Errors based on Second derivatives) Coefficient Std.Error t-value t-prob Cst(M) 0.012190 0.0018643 6.539 0.0000 AR(1) 0.001009 0.033814 0.02983 0.9762 AR(2) -0.039538 0.033682 -1.174 0.2407 AR(3) -0.048821 0.032947 -1.482 0.1387 Cst(V) 0.0002275.0142e-005 4.523 0.0000 ARCH(Alpha1) 0.079251 0.023622 3.355 0.0008 GARCH(Beta1) 0.850560 0.021544 39.48 0.0000 GJR(Gamma1) 0.060378 0.035075 1.721 0.0855 Student(DF) 9.649881 2.7376 3.525 0.0004 No. Observations : 972 No. Parameters : 9 Mean (Y) : 0.01229 Variance (Y) : 0.00600 Skewness (Y) : 0.60501 Kurtosis (Y) : 9.45033 Log Likelihood : 1238.322 > ge=ge*100 > ma=garchOxFit(formula.mean=~arma(0,0),formula.var=~gjr(1,1),series=ge,cond.dist="t") ******************** ** SPECIFICATIONS ** ******************** Dependent variable : X Mean Equation : ARMA (0, 0) model. No regressor in the mean Variance Equation : GJR (1, 1) model. No regressor in the variance The distribution is a Student distribution, with 9.69495 degrees of freedom. Strong convergence using numerical derivatives Log-likelihood = -3241.28 Maximum Likelihood Estimation (Std.Errors based on Second derivatives) Coefficient Std.Error t-value t-prob Cst(M) 1.196024 0.20261 5.903 0.0000 Cst(V) 2.322104 0.85365 2.720 0.0066 ARCH(Alpha1) 0.073797 0.022705 3.250 0.0012 GARCH(Beta1) 0.851598 0.028915 29.45 0.0000 GJR(Gamma1) 0.066666 0.038194 1.745 0.0812 Student(DF) 9.694954 2.7800 3.487 0.0005 No. Observations : 972 No. Parameters : 6 Mean (Y) : 1.22913 Variance (Y) : 59.96874 Skewness (Y) : 0.60501 Kurtosis (Y) : 9.45033 Log Likelihood : -3241.281 *************** ** FORECASTS ** *************** Number of Forecasts: 15 Horizon Mean Variance 1 1.196 23.05 2 1.196 24.42 3 1.196 25.73 4 1.196 26.99 5 1.196 28.2 *********** ** TESTS ** *********** Statistic t-Test P-Value Skewness 0.12607 1.6070 0.10804 Excess Kurtosis 0.88056 5.6182 1.9294e-008 Jarque-Bera 33.978 .NaN 4.1862e-008 --------------- Information Criterium (to be minimized) Akaike 6.681648 Shibata 6.681572 Schwarz 6.711767 Hannan-Quinn 6.693111 --------------- Q-Statistics on Standardized Residuals Q( 10) = 8.49336 [0.5807619] Q( 15) = 13.5308 [0.5613712] Q( 20) = 15.2315 [0.7630094] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] -------------- Q-Statistics on Squared Standardized Residuals --> P-values adjusted by 2 degree(s) of freedom Q( 10) = 10.8032 [0.2131014] Q( 15) = 19.6960 [0.1030548] Q( 20) = 26.4931 [0.0890060] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] EGARCH model > source("garchoxfit_R_w.txt") > da=read.table("m-ge2606.txt") > ge=da[,2] > m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~egarch(1,1),series=ge) ******************** ** SPECIFICATIONS ** ******************** Dependent variable : X Mean Equation : ARMA (0, 0) model. No regressor in the mean Variance Equation : EGARCH (1, 1) model. No regressor in the variance The distribution is a Gauss distribution. Strong convergence using numerical derivatives Log-likelihood = 1209.23 Maximum Likelihood Estimation (Std.Errors based on Second derivatives) Coefficient Std.Error t-value t-prob Cst(M) 0.012319 0.0023229 5.303 0.0000 Cst(V) -5.290466 0.13563 -39.01 0.0000 ARCH(Alpha1) 2.006326 2.1364 0.9391 0.3479 GARCH(Beta1) 0.939566 0.017354 54.14 0.0000 EGARCH(Theta1) -0.014276 0.014714 -0.9702 0.3322 EGARCH(Theta2) 0.093538 0.067818 1.379 0.1681 No. Observations : 972 No. Parameters : 6 Mean (Y) : 0.01229 Variance (Y) : 0.00600 Skewness (Y) : 0.60501 Kurtosis (Y) : 9.45033 Log Likelihood : 1209.234 *************** ** FORECASTS ** *************** Number of Forecasts: 15 Horizon Mean Variance 1 0.01232 0.001953 2 0.01232 0.002124 3 0.01232 0.002238 *********** ** TESTS ** *********** Statistic t-Test P-Value Skewness 0.12285 1.5660 0.11734 Excess Kurtosis 0.86980 5.5496 2.8640e-008 Jarque-Bera 33.085 .NaN 6.5411e-008 --------------- Information Criterium (to be minimized) Akaike -2.475790 Shibata -2.475866 Schwarz -2.445671 Hannan-Quinn -2.464327 --------------- Q-Statistics on Standardized Residuals Q( 10) = 10.2053 [0.4226707] Q( 15) = 15.0269 [0.4494778] Q( 20) = 16.8875 [0.6602663] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] -------------- Q-Statistics on Squared Standardized Residuals --> P-values adjusted by 2 degree(s) of freedom Q( 10) = 8.99147 [0.3430160] Q( 15) = 26.5179 [0.0144734] Q( 20) = 34.6936 [0.0103274] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] -------------- S-Plus output **** > m1=garch(ge~1,~egarch(1,1),leverage=T) Convergence R-Square = 0.0001707261 Function value convergence. > summary(m1) Call: garch(formula.mean = ge ~ 1, formula.var = ~ egarch(1, 1), leverage = T) Mean Equation: ge ~ 1 Conditional Variance Equation: ~ egarch(1, 1) Conditional Distribution: gaussian -------------------------------------------------------------- Estimated Coefficients: -------------------------------------------------------------- Value Std.Error t value Pr(>|t|) C 0.0128 0.002015 6.351 3.287e-010 A -0.3133 0.066290 -4.726 2.628e-006 ARCH(1) 0.2012 0.031292 6.430 2.000e-010 GARCH(1) 0.9710 0.009582 101.336 0.000e+000 LEV(1) -0.1562 0.079047 -1.975 4.850e-002 -------------------------------------------------------------- AIC(5) = -2439.021 BIC(5) = -2414.625 Normality Test: -------------------------------------------------------------- Jarque-Bera P-value Shapiro-Wilk P-value 33.02 6.772e-008 0.984 0.1274 Ljung-Box test for standardized residuals: -------------------------------------------------------------- Statistic P-value Chi^2-d.f. 13.28 0.349 12 Ljung-Box test for squared standardized residuals: -------------------------------------------------------------- Statistic P-value Chi^2-d.f. 19.26 0.08238 12 Lagrange multiplier test: -------------------------------------------------------------- Lag 1 Lag 2 Lag 3 Lag 4 Lag 5 Lag 6 Lag 7 Lag 8 Lag 9 Lag 10 Lag 11 0.7413 1.349 -1.191 -1.044 -0.6462 0.1198 1.72 1.756 -0.2616 2.205 0.9401 Lag 12 C 0.06957 -0.9987 TR^2 P-value F-stat P-value 18.51 0.1011 1.716 0.156 >