Finance and TimeSeries
Import library(bayesGARCH)
Warning message:package ‘bayesGARCH’ was built under R version 3.5.3
> data(dem2gbp)
> y <- dem2gbp[1:750]
> MCMC <- bayesGARCH(y, control = list(n.chain = 2, l.chain = 200))
chain: 1 iteration: 10 parameters: 0.0407 0.1977 0.6747 97.9185
chain: 1 iteration: 20 parameters: 0.041 0.1566 0.6818 66.2305
chain: 1 iteration: 30 parameters: 0.0439 0.208 0.65 45.4827
chain: 1 iteration: 40 parameters: 0.0461 0.2426 0.6215 58.8274
chain: 1 iteration: 50 parameters: 0.0361 0.2158 0.6922 52.3408
chain: 1 iteration: 60 parameters: 0.0255 0.1801 0.7399 46.8184
chain: 1 iteration: 70 parameters: 0.0344 0.2125 0.7051 34.2654
chain: 1 iteration: 80 parameters: 0.037 0.1893 0.6955 35.4467
chain: 1 iteration: 90 parameters: 0.0299 0.2065 0.6975 33.0581
chain: 1 iteration: 100 parameters: 0.0332 0.2006 0.6877 28.2932
chain: 1 iteration: 110 parameters: 0.0385 0.2189 0.6584 23.1169
chain: 1 iteration: 120 parameters: 0.0377 0.1947 0.6551 18.713
chain: 1 iteration: 130 parameters: 0.0435 0.2348 0.6385 21.2452
chain: 1 iteration: 140 parameters: 0.0556 0.3015 0.5682 16.9942
chain: 1 iteration: 150 parameters: 0.0296 0.2507 0.6771 16.5932
chain: 1 iteration: 160 parameters: 0.0547 0.2298 0.5981 14.338
chain: 1 iteration: 170 parameters: 0.0502 0.2043 0.6283 10.2683
chain: 1 iteration: 180 parameters: 0.0455 0.296 0.6117 10.5093
chain: 1 iteration: 190 parameters: 0.0561 0.267 0.593 8.3902
chain: 1 iteration: 200 parameters: 0.0272 0.2558 0.6836 8.4362
chain: 2 iteration: 10 parameters: 0.0554 0.2672 0.5665 76.2903
chain: 2 iteration: 20 parameters: 0.0632 0.2456 0.5452 75.3237
chain: 2 iteration: 30 parameters: 0.0437 0.1915 0.6723 43.8814
chain: 2 iteration: 40 parameters: 0.0332 0.2959 0.6609 36.4541
chain: 2 iteration: 50 parameters: 0.0625 0.4065 0.4705 23.7808
chain: 2 iteration: 60 parameters: 0.046 0.3761 0.5178 17.951
chain: 2 iteration: 70 parameters: 0.0787 0.349 0.4474 15.7251
chain: 2 iteration: 80 parameters: 0.0671 0.2682 0.5123 20.978
chain: 2 iteration: 90 parameters: 0.0414 0.2661 0.6255 22.1921
chain: 2 iteration: 100 parameters: 0.0494 0.1933 0.6442 20.2299
chain: 2 iteration: 110 parameters: 0.0379 0.2421 0.651 23.7265
chain: 2 iteration: 120 parameters: 0.0337 0.2249 0.6749 16.5062
chain: 2 iteration: 130 parameters: 0.0288 0.2614 0.6707 15.3967
chain: 2 iteration: 140 parameters: 0.041 0.2597 0.5917 9.8975
chain: 2 iteration: 150 parameters: 0.0432 0.2261 0.6611 13.9151
chain: 2 iteration: 160 parameters: 0.031 0.2515 0.6666 12.1141
chain: 2 iteration: 170 parameters: 0.045 0.1672 0.6649 10.9084
chain: 2 iteration: 180 parameters: 0.0364 0.2269 0.7005 8.7603
chain: 2 iteration: 190 parameters: 0.044 0.2549 0.5976 10.3563
chain: 2 iteration: 200 parameters: 0.038 0.3334 0.6159 8.0582
> plot(MCMC)
> smpl <- formSmpl(MCMC, l.bi = 50)
n.chain: 2
l.chain: 200
l.bi: 50
batch.size: 1
smpl size: 300
> summary(smpl)
Iterations = 1:300
Thinning interval = 1
Number of chains = 1
Sample size per chain = 300
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:Mean SD Naive SE
alpha0 0.04308 0.01193 0.000689
alpha1 0.23996 0.04806 0.002774
beta 0.63368 0.06606 0.003814
nu 19.48437 10.38978 0.599854
Time-series SE
alpha0 0.00321
alpha1 0.01294
beta 0.02203
nu 4.53866
2. Quantiles for each variable:
2.5% 25% 50%
alpha0 0.02615 0.03506 0.04117
alpha1 0.16644 0.20486 0.23441
beta 0.46807 0.59946 0.64301
nu 8.44042 11.02220 17.59416
75% 97.5%
alpha0 0.04805 0.07271
alpha1 0.26695 0.34679
beta 0.68334 0.72659
nu 21.86966 50.93082
> smpl <- as.matrix(smpl)
> pairs(smpl)
> MCMC <- bayesGARCH(y, lambda = 100, delta = 500,
+ control = list(n.chain = 2, l.chain = 200))
chain: 1 iteration: 10 parameters: 0.0329 0.1778 0.6998 500.0104
chain: 1 iteration: 20 parameters: 0.0404 0.2281 0.6614 500.0087
chain: 1 iteration: 30 parameters: 0.036 0.1599 0.6875 500.0017
chain: 1 iteration: 40 parameters: 0.0545 0.1614 0.6322 500.0184
chain: 1 iteration: 50 parameters: 0.0547 0.2635 0.6081 500.0196
chain: 1 iteration: 60 parameters: 0.0466 0.2477 0.6326 500.0068
chain: 1 iteration: 70 parameters: 0.0482 0.2155 0.6698 500.0058
chain: 1 iteration: 80 parameters: 0.0592 0.1971 0.6101 500.054
chain: 1 iteration: 90 parameters: 0.0379 0.2115 0.6569 500.0027
chain: 1 iteration: 100 parameters: 0.0343 0.1978 0.6903 500.0092
chain: 1 iteration: 110 parameters: 0.0386 0.264 0.6412 500.0017
chain: 1 iteration: 120 parameters: 0.0514 0.1758 0.6626 500.0272
chain: 1 iteration: 130 parameters: 0.0558 0.2613 0.5978 500.0098
chain: 1 iteration: 140 parameters: 0.0523 0.2633 0.607 500.0194
chain: 1 iteration: 150 parameters: 0.0586 0.2499 0.5598 500.006
chain: 1 iteration: 160 parameters: 0.0658 0.2747 0.5133 500.0178
chain: 1 iteration: 170 parameters: 0.0536 0.2966 0.5608 500.0001
chain: 1 iteration: 180 parameters: 0.0596 0.3226 0.5326 500.0014
chain: 1 iteration: 190 parameters: 0.0568 0.2861 0.5559 500.0148
chain: 1 iteration: 200 parameters: 0.0496 0.2487 0.6088 500.0012
chain: 2 iteration: 10 parameters: 0.0499 0.2512 0.619 500.0129
chain: 2 iteration: 20 parameters: 0.0515 0.2775 0.5862 500.0071
chain: 2 iteration: 30 parameters: 0.0888 0.2709 0.4641 500.0077
chain: 2 iteration: 40 parameters: 0.0398 0.2471 0.6393 500.008
chain: 2 iteration: 50 parameters: 0.0453 0.1967 0.6801 500.012
chain: 2 iteration: 60 parameters: 0.0479 0.1889 0.6263 500.0109
chain: 2 iteration: 70 parameters: 0.0555 0.1947 0.59 500.0165
chain: 2 iteration: 80 parameters: 0.0556 0.1849 0.616 500.0051
chain: 2 iteration: 90 parameters: 0.0629 0.2343 0.5562 500.0044
chain: 2 iteration: 100 parameters: 0.0446 0.2957 0.6098 500.0145
chain: 2 iteration: 110 parameters: 0.0496 0.2282 0.6275 500.009
chain: 2 iteration: 120 parameters: 0.0419 0.23 0.628 500.003
chain: 2 iteration: 130 parameters: 0.0411 0.212 0.6567 500.0022
chain: 2 iteration: 140 parameters: 0.0421 0.2509 0.6064 500.0081
chain: 2 iteration: 150 parameters: 0.0423 0.2523 0.6228 500.0024
chain: 2 iteration: 160 parameters: 0.0473 0.2189 0.651 500.005
chain: 2 iteration: 170 parameters: 0.0345 0.2256 0.6544 500.0052
chain: 2 iteration: 180 parameters: 0.041 0.1765 0.6751 500.019
chain: 2 iteration: 190 parameters: 0.0446 0.1543 0.6967 500.0161
chain: 2 iteration: 200 parameters: 0.0451 0.2114 0.6256 500.0235
> addPriorConditions <- function(psi){psi[2] + psi[3] < 1}
> MCMC <- bayesGARCH(y, lambda = 100, delta = 500,
+ control = list(n.chain = 2, l.chain = 200,
+ addPriorConditions = addPriorConditions))
chain: 1 iteration: 10 parameters: 0.0418 0.1647 0.6938 500.017
chain: 1 iteration: 20 parameters: 0.0373 0.2278 0.6769 500.0137
chain: 1 iteration: 30 parameters: 0.0403 0.1841 0.7026 500.0095
chain: 1 iteration: 40 parameters: 0.0341 0.1974 0.7136 500.0191
chain: 1 iteration: 50 parameters: 0.0437 0.1844 0.6881 500.0034
chain: 1 iteration: 60 parameters: 0.0296 0.2519 0.693 500.0129
chain: 1 iteration: 70 parameters: 0.0419 0.17 0.7143 500.0066
chain: 1 iteration: 80 parameters: 0.0306 0.2095 0.7261 500.0046
chain: 1 iteration: 90 parameters: 0.0303 0.2363 0.6814 500.0001
chain: 1 iteration: 100 parameters: 0.0402 0.1972 0.6739 500.0306
chain: 1 iteration: 110 parameters: 0.0324 0.2223 0.6892 500.0017
chain: 1 iteration: 120 parameters: 0.0375 0.2387 0.657 500.0184
chain: 1 iteration: 130 parameters: 0.0386 0.2122 0.6721 500.0128
chain: 1 iteration: 140 parameters: 0.051 0.2148 0.6066 500.0318
chain: 1 iteration: 150 parameters: 0.0646 0.2342 0.5599 500.004
chain: 1 iteration: 160 parameters: 0.0513 0.2411 0.5915 500.0097
chain: 1 iteration: 170 parameters: 0.0546 0.3589 0.5126 500.0011
chain: 1 iteration: 180 parameters: 0.0901 0.3275 0.4342 500.0031
chain: 1 iteration: 190 parameters: 0.0533 0.2813 0.5867 500.0005
chain: 1 iteration: 200 parameters: 0.0595 0.2697 0.5207 500.0066
chain: 2 iteration: 10 parameters: 0.0498 0.2439 0.6071 500.0366
chain: 2 iteration: 20 parameters: 0.0631 0.2553 0.5587 500.0086
chain: 2 iteration: 30 parameters: 0.0436 0.1827 0.689 500.0087
chain: 2 iteration: 40 parameters: 0.0412 0.1606 0.704 500.0051
chain: 2 iteration: 50 parameters: 0.0409 0.2078 0.663 500.0177
chain: 2 iteration: 60 parameters: 0.059 0.1848 0.6412 500.0098
chain: 2 iteration: 70 parameters: 0.0622 0.23 0.5504 500.0005
chain: 2 iteration: 80 parameters: 0.0583 0.1872 0.625 500.0063
chain: 2 iteration: 90 parameters: 0.0367 0.3141 0.5929 500.008
chain: 2 iteration: 100 parameters: 0.0576 0.2113 0.633 500.0277
chain: 2 iteration: 110 parameters: 0.0505 0.2083 0.6078 500
chain: 2 iteration: 120 parameters: 0.0491 0.1966 0.6778 500.0036
chain: 2 iteration: 130 parameters: 0.0351 0.2449 0.6538 500.0006
chain: 2 iteration: 140 parameters: 0.0483 0.1964 0.634 500.005
chain: 2 iteration: 150 parameters: 0.0398 0.2056 0.6834 500.0157
chain: 2 iteration: 160 parameters: 0.0468 0.1898 0.6829 500.0036
chain: 2 iteration: 170 parameters: 0.0372 0.2515 0.6407 500.0018
chain: 2 iteration: 180 parameters: 0.0312 0.1982 0.7218 500.0054
chain: 2 iteration: 190 parameters: 0.0367 0.1801 0.7112 500.0004
chain: 2 iteration: 200 parameters: 0.0308 0.2253 0.7052 500.004
> install.packages("fGarch")
Installing package into ‘C:/Users/ADMIN/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/fGarch_3042.83.1.zip'
Content type 'application/zip' length 607349 bytes (593 KB)
downloaded 593 KB
package ‘fGarch’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\ADMIN\AppData\Local\Temp\Rtmp6VdpoA\downloaded_packages
> library(fGarch)
Attaching package: ‘fGarch’
The following object is masked _by_ ‘.GlobalEnv’:
dem2gbp
Warning message:
package ‘fGarch’ was built under R version 3.5.3
> x = as.timeSeries(data(LPP2005REC))
> fit = garchFit(LPP40 ~ garch(1, 1), data = 100*x, trace = FALSE)
> volatility = volatility(fit, type = "sigma")
> head(volatility)
[1] 0.2805121 0.2674818 0.2608402
[4] 0.2645372 0.2603407 0.2558201
> class(volatility)
[1] "numeric"
> volatility = volatility(fit, type = "h")
> head(volatility)
[1] 0.07868705 0.07154651 0.06803759
[4] 0.06997992 0.06777728 0.06544394
> class(volatility)
[1] "numeric"
> volatility = slot(fit, "sigma.t")
> head(volatility)
[1] 0.2805121 0.2674818 0.2608402
[4] 0.2645372 0.2603407 0.2558201
> class(volatility)
[1] "numeric"
> volatility = slot(fit, "sigma.t")
> head(volatility)
[1] 0.2805121 0.2674818 0.2608402
[4] 0.2645372 0.2603407 0.2558201
> class(volatility)
[1] "numeric"
> volatility = slot(fit, "h.t")
> head(volatility)
[1] 0.07868705 0.07154651 0.06803759
[4] 0.06997992 0.06777728 0.06544394
> par(mfrow = c(2, 2))
> set.seed(1953)
> r = rsged(n = 1000)
> plot(r, type = "l", main = "sged", col = "steelblue")
> hist(r, n = 25, probability = TRUE, border = "white", col = "steelblue")
> box()
> x = seq(min(r), max(r), length = 201)
> lines(x, dsged(x), lwd = 2)
> plot(sort(r), (1:1000/1000), main = "Probability", col = "steelblue",
+ ylab = "Probability")
> lines(x, psged(x), lwd = 2)
> round(qsged(psged(q = seq(-1, 5, by = 1))), digits = 6)
[1] -1 0 1 2 3 4 5
> plot(sort(r), (1:1000/1000), main = "Probability", col = "steelblue",
+ ylab = "Probability")
> lines(x, psged(x), lwd = 2)
> round(qsged(psged(q = seq(-1, 5, by = 1))), digits = 6)
[1] -1 0 1 2 3 4 5
> set.seed(123)
> fit = garchFit(~ garch(1, 1), data = garchSim(), trace = FALSE)
> fit
Title:
GARCH Modelling
Call:
garchFit(formula = ~garch(1, 1), data = garchSim(), trace = FALSE)
Mean and Variance Equation:
data ~ garch(1, 1)
<environment: 0x0000000012ba1338>
[data = garchSim()]
Conditional Distribution:
norm
Coefficient(s):
mu omega alpha1
-1.5658e-05 3.1101e-06 2.8879e-01
beta1
4.0817e-01
Std. Errors:
based on Hessian
Error Analysis:
Estimate Std. Error t value
mu -1.566e-05 2.637e-04 -0.059
omega 3.110e-06 1.874e-06 1.660
alpha1 2.888e-01 1.808e-01 1.597
beta1 4.082e-01 2.777e-01 1.470
Pr(>|t|)
mu 0.953
omega 0.097 .
alpha1 0.110
beta1 0.142
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
Log Likelihood:
440.3751 normalized: 4.403751
Description:
Sat Apr 20 11:26:49 2019 by user: ADMIN
> predict(fit, n.ahead = 10)
meanForecast meanError
1 -1.565789e-05 0.004533623
2 -1.565789e-05 0.004175561
3 -1.565789e-05 0.003906646
4 -1.565789e-05 0.003707707
5 -1.565789e-05 0.003562490
6 -1.565789e-05 0.003457674
7 -1.565789e-05 0.003382702
8 -1.565789e-05 0.003329451
9 -1.565789e-05 0.003291827
10 -1.565789e-05 0.003265349
standardDeviation
1 0.0045336232 0.004175561
3 0.003906646
4 0.003707707
5 0.003562490
6 0.003457674
7 0.003382702
8 0.003329451
9 0.003291827
10 0.003265349
> predict(fit, n.ahead = 10,mse="uncond")
meanForecast meanError
1 -1.565789e-05 0.00305647
2 -1.565789e-05 0.00305647
3 -1.565789e-05 0.00305647
4 -1.565789e-05 0.00305647
5 -1.565789e-05 0.00305647
6 -1.565789e-05 0.00305647
7 -1.565789e-05 0.00305647
8 -1.565789e-05 0.00305647
9 -1.565789e-05 0.00305647
10 -1.565789e-05 0.00305647
standardDeviation
1 0.004533623
2 0.004175561
3 0.003906646
4 0.003707707
5 0.003562490
6 0.003457674
7 0.003382702
8 0.003329451
9 0.003291827
10 0.003265349
> predict(fit, n.ahead = 10, plot=TRUE, crit_val=2)
meanForecast meanError
1 -1.565789e-05 0.004533623
2 -1.565789e-05 0.004175561
3 -1.565789e-05 0.003906646
4 -1.565789e-05 0.003707707
5 -1.565789e-05 0.003562490
6 -1.565789e-05 0.003457674
7 -1.565789e-05 0.003382702
8 -1.565789e-05 0.003329451
9 -1.565789e-05 0.003291827
10 -1.565789e-05 0.003265349
standardDeviation lowerInterval
1 0.004533623 -0.0090829042 0.004175561 -0.008366780
3 0.003906646 -0.007828950
4 0.003707707 -0.007431071
5 0.003562490 -0.007140637
6 0.003457674 -0.006931006
7 0.003382702 -0.006781061
8 0.003329451 -0.006674559
9 0.003291827 -0.006599312
10 0.003265349 -0.006546355
upperInterval
1 0.009051588
2 0.008335464
3 0.007797635
4 0.007399756
5 0.007109322
6 0.006899691
7 0.006749746
8 0.006643243
9 0.006567996
10 0.006515039
> set.seed(321)
> fit2 = garchFit(~ garch(1, 1), data = garchSim(), trace = FALSE, cond.dist="sged")
> predict(fit2,n.ahead=20,plot=TRUE)
meanForecast meanError
1 -0.0001011762 0.002792102
2 -0.0001011762 0.002697756
3 -0.0001011762 0.002657046
4 -0.0001011762 0.002639724
5 -0.0001011762 0.002632400
6 -0.0001011762 0.002629311
7 -0.0001011762 0.002628010
8 -0.0001011762 0.002627462
9 -0.0001011762 0.002627231
10 -0.0001011762 0.002627134
11 -0.0001011762 0.002627093
12 -0.0001011762 0.002627076
13 -0.0001011762 0.002627069
14 -0.0001011762 0.002627066
15 -0.0001011762 0.002627064
16 -0.0001011762 0.002627064
17 -0.0001011762 0.002627064
18 -0.0001011762 0.002627064
19 -0.0001011762 0.002627064
20 -0.0001011762 0.002627064
StandardDeviation lowerInterval
1 0.002792102 -0.0056720902 0.002697756 -0.005483848
3 0.002657046 -0.005402622
4 0.002639724 -0.005368060
5 0.002632400 -0.005353446
6 0.002629311 -0.005347283
7 0.002628010 -0.005344686
8 0.002627462 -0.005343593
9 0.002627231 -0.005343133
10 0.002627134 -0.005342940
11 0.002627093 -0.005342858
12 0.002627076 -0.005342824
13 0.002627069 -0.005342809
14 0.002627066 -0.005342803
15 0.002627064 -0.005342801
16 0.002627064 -0.005342800
17 0.002627064 -0.005342799
18 0.002627064 -0.005342799
19 0.002627064 -0.005342799
20 0.002627064 -0.005342799
upperInterval
1 0.0051837242 0.005005147
3 0.004928091
4 0.004895303
5 0.004881439
6 0.004875592
7 0.004873130
8 0.004872093
9 0.004871656
10 0.004871472
11 0.004871395
12 0.004871363
13 0.004871349
14 0.004871343
15 0.004871341
16 0.004871340
17 0.004871339
18 0.004871339
19 0.004871339
20 0.004871339
0 Comments