forked from Apress/numerical-methods-using-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter15.java
More file actions
1210 lines (1056 loc) · 49.1 KB
/
Chapter15.java
File metadata and controls
1210 lines (1056 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) NM LTD.
* https://nm.dev/
*
* THIS SOFTWARE IS LICENSED, NOT SOLD.
*
* YOU MAY USE THIS SOFTWARE ONLY AS DESCRIBED IN THE LICENSE.
* IF YOU ARE NOT AWARE OF AND/OR DO NOT AGREE TO THE TERMS OF THE LICENSE,
* DO NOT USE THIS SOFTWARE.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITH NO WARRANTY WHATSOEVER,
* EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
* ANY WARRANTIES OF ACCURACY, ACCESSIBILITY, COMPLETENESS,
* FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, NON-INFRINGEMENT,
* TITLE AND USEFULNESS.
*
* IN NO EVENT AND UNDER NO LEGAL THEORY,
* WHETHER IN ACTION, CONTRACT, NEGLIGENCE, TORT, OR OTHERWISE,
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIMS, DAMAGES OR OTHER LIABILITIES,
* ARISING AS A RESULT OF USING OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.nm.nmj;
import dev.nm.algebra.linear.matrix.doubles.Matrix;
import dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix;
import dev.nm.algebra.linear.matrix.doubles.operation.Inverse;
import dev.nm.algebra.linear.matrix.doubles.operation.MatrixFactory;
import static dev.nm.algebra.linear.matrix.doubles.operation.MatrixFactory.cbind;
import dev.nm.algebra.linear.vector.doubles.Vector;
import dev.nm.algebra.linear.vector.doubles.dense.DenseVector;
import dev.nm.number.DoubleUtils;
import dev.nm.stat.cointegration.CointegrationMLE;
import dev.nm.stat.cointegration.JohansenAsymptoticDistribution.Test;
import dev.nm.stat.cointegration.JohansenTest;
import dev.nm.stat.descriptive.covariance.SampleCovariance;
import dev.nm.stat.descriptive.moment.Mean;
import dev.nm.stat.random.rng.RNGUtils;
import dev.nm.stat.random.rng.univariate.RandomNumberGenerator;
import dev.nm.stat.random.rng.univariate.normal.StandardNormalRNG;
import dev.nm.stat.test.timeseries.adf.AugmentedDickeyFuller;
import dev.nm.stat.test.timeseries.adf.TrendType;
import dev.nm.stat.test.timeseries.portmanteau.LjungBox;
import dev.nm.stat.timeseries.datastructure.multivariate.realtime.inttime.MultivariateIntTimeTimeSeries;
import dev.nm.stat.timeseries.datastructure.multivariate.realtime.inttime.MultivariateSimpleTimeSeries;
import dev.nm.stat.timeseries.datastructure.univariate.realtime.inttime.SimpleTimeSeries;
import dev.nm.stat.timeseries.linear.multivariate.MultivariateAutoCovarianceFunction;
import dev.nm.stat.timeseries.linear.multivariate.arima.VARIMAModel;
import dev.nm.stat.timeseries.linear.multivariate.arima.VARIMASim;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.MultivariateForecastOneStep;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VARFit;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VARMAAutoCovariance;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VARMAModel;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VARModel;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VARXModel;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VECMTransitory;
import dev.nm.stat.timeseries.linear.multivariate.stationaryprocess.arma.VMAModel;
import dev.nm.stat.timeseries.linear.univariate.arima.ARIMAForecast;
import dev.nm.stat.timeseries.linear.univariate.arima.ARIMAModel;
import dev.nm.stat.timeseries.linear.univariate.arima.ARIMASim;
import dev.nm.stat.timeseries.linear.univariate.arima.AutoARIMAFit;
import dev.nm.stat.timeseries.linear.univariate.sample.SampleAutoCorrelation;
import dev.nm.stat.timeseries.linear.univariate.sample.SampleAutoCovariance;
import dev.nm.stat.timeseries.linear.univariate.sample.SamplePartialAutoCorrelation;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.AdditiveModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.MADecomposition;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.MultiplicativeModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.ARMAForecast;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.ARMAForecastOneStep;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.ARMAModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.ARModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.AutoCorrelation;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.AutoCovariance;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.ConditionalSumOfSquares;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.LinearRepresentation;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.arma.MAModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.armagarch.ARMAGARCHFit;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.garch.GARCHFit;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.garch.GARCHModel;
import dev.nm.stat.timeseries.linear.univariate.stationaryprocess.garch.GARCHSim;
import java.io.IOException;
import static java.lang.Math.log;
import java.util.Arrays;
/**
* Numerical Methods Using Java: For Data Science, Analysis, and Engineering
*
* @author haksunli
* @see
* https://www.amazon.com/Numerical-Methods-Using-Java-Engineering/dp/1484267966
* https://nm.dev/
*/
public class Chapter15 {
public static void main(String[] args) throws Exception {
System.out.println("Chapter 15 demos");
Chapter15 chapter15 = new Chapter15();
chapter15.univariate_ts();
chapter15.sp500_daily();
chapter15.sp500_monthly();
chapter15.decomposition();
chapter15.ar();
chapter15.ma();
chapter15.arma11();
chapter15.arma23();
chapter15.arma_forecast();
chapter15.goodness_of_fit();
chapter15.adf();
chapter15.garch_models();
chapter15.mts();
chapter15.spx_aapl();
chapter15.VARIMA();
chapter15.VAR();
chapter15.VARMA_forecast();
chapter15.VARMA_autocovariance();
chapter15.varim_simulation();
chapter15.cointegration();
chapter15.vecm();
}
public void vecm() {
System.out.println("convert VAR to VECM");
// define a VAR(2) model
VARXModel var = new VARXModel(
new Matrix[]{
new DenseMatrix(
new double[][]{
{-0.210, 0.167},
{0.512, 0.220}
}),
new DenseMatrix(
new double[][]{
{0.743, 0.746},
{-0.405, 0.572}
})
},
null);
// construct a VECM fron a VAR
VECMTransitory vecm = new VECMTransitory(var);
System.out.println("dimension = " + vecm.dimension());
System.out.println("PI, the impact matrix = ");
System.out.println(vecm.pi());
System.out.println("GAMMA = ");
System.out.println(vecm.gamma(1));
}
public void cointegration() throws Exception {
System.out.println("cointegration between SPX and AAPL");
// read the monthly S&P 500 data from a csv file
double[][] spx_arr
= DoubleUtils.readCSV2d(
this.getClass().getClassLoader().getResourceAsStream("sp500_monthly.csv"),
true,
true
);
// convert the csv file into a matrix for manipulation
Matrix spx_M1 = new DenseMatrix(spx_arr);
// remove the data before 2008
Matrix spx_M2 = MatrixFactory.subMatrix(spx_M1, 325, spx_M1.nRows(), 1, spx_M1.nCols());
// extract the column of adjusted close prices
Vector spx_v = spx_M2.getColumn(5); // adjusted closes
// read the monthly AAPL data from a csv file
double[][] appl_arr
= DoubleUtils.readCSV2d(
this.getClass().getClassLoader().getResourceAsStream("AAPL_monthly.csv"),
true,
true
);
// convert the csv file into a matrix for manipulation
Matrix aapl_M1 = new DenseMatrix(appl_arr);
// remove the data before 2008
Matrix aapl_M2 = MatrixFactory.subMatrix(aapl_M1, 325, aapl_M1.nRows(), 1, aapl_M1.nCols());
// extract the column of adjusted close prices
Vector aapl_v = aapl_M2.getColumn(5); // adjusted closes
// combine SPX and AAPL to form a bivariate time series
MultivariateSimpleTimeSeries mts
= new MultivariateSimpleTimeSeries(cbind(spx_v, aapl_v));
// System.out.println("(spx, aapl) prices: \n" + mts);
// run cointegration on all combinations of available test and trend types
for (Test test : Test.values()) {
for (dev.nm.stat.cointegration.JohansenAsymptoticDistribution.TrendType trend
: dev.nm.stat.cointegration.JohansenAsymptoticDistribution.TrendType.values()) {
CointegrationMLE coint = new CointegrationMLE(mts, true);
JohansenTest johansen
= new JohansenTest(test, trend, coint.getEigenvalues().size());
System.out.println("alpha:");
System.out.println(coint.alpha());
System.out.println("beta:");
System.out.println(coint.beta());
System.out.println("Johansen test: "
+ test.toString()
+ "\t" + trend.toString()
+ "\t eigenvalues: " + coint.getEigenvalues()
+ "\t statistics: " + johansen.getStats(coint)
);
}
}
// run ADF test to check if the cointegrated series is indeed stationary
double[] betas = new double[]{-12.673296, -6.995392};
for (double beta : betas) {
System.out.printf("testing for beta = %f%n", beta);
Vector ci = spx_v.add(aapl_v.scaled(beta));
AugmentedDickeyFuller adf
= new AugmentedDickeyFuller(
ci.toArray(),
TrendType.CONSTANT, // constant drift term
4, // the lag order
null
);
System.out.println("H0: " + adf.getNullHypothesis());
System.out.println("H1: " + adf.getAlternativeHypothesis());
System.out.printf("the p-value for the test = %f%n", adf.pValue());
System.out.printf("the statistics for the test = %f%n", adf.statistics());
}
}
public void varim_simulation() {
// number of random numbers to generate
int T = 100000;
// the mean
Vector MU = new DenseVector(new double[]{1., 1.});
// the AR coefficients
Matrix[] PHI = new Matrix[]{
new DenseMatrix(
new double[][]{
{0.5, 0.5},
{0., 0.5}
})};
// the MA coefficients
Matrix[] THETA = new Matrix[]{PHI[0]};
// the white noise covariance structure
Matrix SIGMA
= new DenseMatrix(
new double[][]{
{1, 0.2},
{0.2, 1}
});
// construct a VARMA model
VARMAModel VARMA = new VARMAModel(MU, PHI, THETA, SIGMA);
// construct a random number generator from a VARMA model
VARIMASim SIM = new VARIMASim(VARMA);
SIM.seed(1234567890L);
// produce the random vectors
Vector[] data = new Vector[T];
for (int i = 0; i < T; ++i) {
data[i] = new DenseVector(SIM.nextVector());
}
// statistics about the simulation
System.out.printf("each vector size = %d%n", data[0].size());
System.out.printf("sample size = %d%n", data.length);
// compute the theoretical mean of the data
double[] theoretical_mean = new Inverse(new DenseMatrix(2, 2).ONE().minus(VARMA.AR(1))).multiply(new DenseVector(MU)).toArray();
System.out.println("theoretical mean =");
System.out.println(Arrays.toString(theoretical_mean));
// cast the random data in matrix form for easy manipulation
Matrix dataM = MatrixFactory.rbind(data);
// compute the sample mean of the data
double sample_mean1 = new Mean(dataM.getColumn(1).toArray()).value();
double sample_mean2 = new Mean(dataM.getColumn(2).toArray()).value();
System.out.printf("sample mean of the first variable = %f%n", sample_mean1);
System.out.printf("sample mean of the second variable = %f%n", sample_mean2);
// compute the theoretical covariance of the data
Matrix cov_theoretical = new DenseMatrix(
new double[][]{
{811. / 135., 101. / 45.},
{101. / 45., 7. / 3.}
});
System.out.println("theoretical covariance =");
System.out.println(cov_theoretical);
// compute the sample covariance of the data
SampleCovariance sample_cov = new SampleCovariance(dataM);
System.out.println("sample covariance =");
System.out.println(sample_cov);
}
public void VARMA_autocovariance() {
System.out.println("autocovariance function for a causal VARMA");
// the AR coefficients
Matrix[] AR = new Matrix[1];
AR[0] = new DenseMatrix(
new double[][]{
{0.5, 0.5},
{0, 0.5}});
// the MA coefficients
Matrix[] MA = new Matrix[1];
MA[0] = AR[0].t();
// SIGMA
Matrix SIGMA
= new DenseMatrix(
new double[][]{
{1, 0.2},
{0.2, 1}
});
// define a VARIMA(1,1) model
VARMAModel varma11 = new VARMAModel(AR, MA, SIGMA);
// comptue the autocovariance function for a VARIMA process up to a certian number of lags
VARMAAutoCovariance GAMMA
= new VARMAAutoCovariance(
varma11,
10 // number of lags
);
// print out the autocovariance function
for (int i = 0; i <= 5; ++i) {
System.out.printf("GAMMA(%d) = %n", i);
System.out.println(GAMMA.evaluate(1));
System.out.println();
}
}
/**
* P. J. Brockwell and R. A. Davis, "Example 5.2.1. Chapter 5. Multivariate
* Time Series," in <i>Time Series: Theory and Methods</i>, Springer, 2006.
*/
public void VARMA_forecast() {
System.out.println("forecast using the innovation algorithm");
// MA(1) model parameters
final double theta = -0.9;
final double sigma = 1;
// a multivariate time series (although the dimension is just 1)
MultivariateIntTimeTimeSeries Xt
= new MultivariateSimpleTimeSeries(
new double[][]{
{-2.58},
{1.62},
{-0.96},
{2.62},
{-1.36}
});
// the autocovariance function
MultivariateAutoCovarianceFunction K = new MultivariateAutoCovarianceFunction() {
@Override
public Matrix evaluate(double x1, double x2) {
int i = (int) x1;
int j = (int) x2;
double k = 0;
if (i == j) {
k = sigma * sigma;
k *= 1 + theta * theta;
}
if (Math.abs(j - i) == 1) {
k = theta;
k *= sigma * sigma;
}
// γ = 0 otherwise
DenseMatrix result = new DenseMatrix(new double[][]{{k}});
return result;
}
};
// run the innovation algorithm
MultivariateForecastOneStep forecast
= new MultivariateForecastOneStep(Xt, K);
// making forecasts
for (int i = 0; i <= 5; ++i) {
System.out.println(Arrays.toString(forecast.xHat(i).toArray()));
}
}
public void VAR() {
System.out.println("VAR models");
// construct a VAR(2) model
Vector MU = new DenseVector(new double[]{1., 2.});
Matrix[] PHI = new Matrix[]{
new DenseMatrix(new double[][]{
{0.2, 0.3},
{0., 0.4}}),
new DenseMatrix(new double[][]{
{0.1, 0.2},
{0.3, 0.1}})
};
VARModel model0 = new VARModel(MU, PHI);
// construct a RNG from the model
VARIMASim sim = new VARIMASim(model0);
sim.seed(1234567891L);
// generate a random multivariate time series
final int N = 5000;
double[][] ts = new double[N][];
for (int i = 0; i < N; ++i) {
ts[i] = sim.nextVector();
}
MultivariateIntTimeTimeSeries mts
= new MultivariateSimpleTimeSeries(ts);
// fit the data to a VAR(2) model
VARModel model1 = new VARFit(mts, 2);
System.out.println("μ = ");
System.out.println(model1.mu());
System.out.println("ϕ_1 =");
System.out.println(model1.AR(1));
System.out.println("ϕ_2 =");
System.out.println(model1.AR(2));
System.out.println("sigma =");
System.out.println(model1.sigma());
}
public void VARIMA() {
System.out.println("VAR, VMA, VARMA, VARIMA and all those");
// construct a VAR(1) model
Matrix[] PHI = new Matrix[1];
PHI[0] = new DenseMatrix(
new double[][]{
{0.7, 0.12},
{0.31, 0.6}
});
VARModel var1 = new VARModel(PHI);
System.out.println("unconditional mean = " + var1.unconditionalMean());
// construct a VAR(1) model
Matrix[] THETA = new Matrix[1];
THETA[0] = new DenseMatrix(
new double[][]{
{0.5, 0.16},
{-0.7, 0.28}
});
VMAModel vma1 = new VMAModel(THETA);
System.out.println("unconditional mean = " + vma1.unconditionalMean());
// construct a VARIMA(1,1) model
Matrix PHI1 = new DenseMatrix(
new double[][]{
{0.7, 0},
{0, 0.6}
});
Matrix THETA1 = new DenseMatrix(
new double[][]{
{0.5, 0.6},
{-0.7, 0.8}
});
VARMAModel varma11 = new VARMAModel(
new Matrix[]{PHI1},
new Matrix[]{THETA1}
);
System.out.println("unconditional mean = " + varma11.unconditionalMean());
// the AR coefficients
Matrix[] PHI_1 = new Matrix[4];
PHI_1[0] = new DenseMatrix(new DenseVector(new double[]{0.3}));
PHI_1[1] = new DenseMatrix(new DenseVector(new double[]{-0.2}));
PHI_1[2] = new DenseMatrix(new DenseVector(new double[]{0.05}));
PHI_1[3] = new DenseMatrix(new DenseVector(new double[]{0.04}));
// the MA coefficients
Matrix[] THETA_1 = new Matrix[2];
THETA_1[0] = new DenseMatrix(new DenseVector(new double[]{0.2}));
THETA_1[1] = new DenseMatrix(new DenseVector(new double[]{0.5}));
// the order of integration
int d = 1;
// construct a VARIMA(1,1,1) model
VARIMAModel varima111 = new VARIMAModel(PHI_1, d, THETA_1);
}
public void spx_aapl() throws IOException {
System.out.println("SPX and APPL");
// read the monthly S&P 500 data from a csv file
double[][] spx_arr
= DoubleUtils.readCSV2d(
this.getClass().getClassLoader().getResourceAsStream("sp500_monthly.csv"),
true,
true
);
// convert the csv file into a matrix for manipulation
Matrix spx_M = new DenseMatrix(spx_arr);
// extract the column of adjusted close prices
Vector spx_v = spx_M.getColumn(5); // adjusted closes
// read the monthly AAPL data from a csv file
double[][] appl_arr
= DoubleUtils.readCSV2d(
this.getClass().getClassLoader().getResourceAsStream("AAPL_monthly.csv"),
true,
true
);
// convert the csv file into a matrix for manipulation
Matrix aapl_M = new DenseMatrix(appl_arr);
// extract the column of adjusted close prices
Vector aapl_v = aapl_M.getColumn(5); // adjusted closes
// combine SPX and AAPL to form a bivariate time series
MultivariateSimpleTimeSeries mts
= new MultivariateSimpleTimeSeries(cbind(spx_v, aapl_v));
System.out.println("(spx, aapl) prices: \n" + mts);
// compute the bivariate time series of log returns
Matrix p1 = mts.drop(1).toMatrix();
Matrix p = mts.toMatrix();
Matrix log_returns_M = p1.ZERO(); // allocate space for the new matrix
for (int i = 1; i <= p1.nRows(); i++) { // matrix and vector index starts from 1
double r = log(p1.get(i, 1)) - log(p.get(i, 1));
log_returns_M.set(i, 1, r);
r = log(p1.get(i, 2)) - log(p.get(i, 2));
log_returns_M.set(i, 2, r);
}
// convert a matrix to a multivariate time series
MultivariateSimpleTimeSeries log_returns
= new MultivariateSimpleTimeSeries(log_returns_M);
System.out.println("(spx, aapl) log_returns: \n" + log_returns);
// fit the log returns to an VAR(1) model
VARFit fit = new VARFit(log_returns, 1);
System.out.println("the estimated phi_1 for var(1) is");
System.out.println(fit.AR(1));
VARMAModel log_returns2 = fit.getDemeanedModel(); // demeaned version
// predict the future values using the innovation algorithm
MultivariateAutoCovarianceFunction K
= new VARMAAutoCovariance(log_returns2, log_returns.size());
MultivariateForecastOneStep forecast
= new MultivariateForecastOneStep(log_returns, K);
for (int i = 0; i <= 5; ++i) {
System.out.println(Arrays.toString(forecast.xHat(i).toArray()));
}
}
public void mts() {
System.out.println("multivariate time series");
// construct a bivariate time series
MultivariateSimpleTimeSeries X_T0
= new MultivariateSimpleTimeSeries(
new double[][]{
{-1.875, 1.693},
{-2.518, -0.03},
{-3.002, -1.057},
{-2.454, -1.038},
{-1.119, -1.086},
{-0.72, -0.455},
{-2.738, 0.962},
{-2.565, 1.992},
{-4.603, 2.434},
{-2.689, 2.118}
});
System.out.println("X_T0: " + X_T0);
// first difference of the bivariate time series
MultivariateSimpleTimeSeries X_T1 = X_T0.diff(1);
System.out.println("diff(1): " + X_T1);
// first order lagged time series
MultivariateSimpleTimeSeries X_T2 = X_T0.lag(1);
System.out.println("lag(1): " + X_T2);
// drop the first two items
MultivariateSimpleTimeSeries X_T3 = X_T0.drop(1);
System.out.println("drop(1): " + X_T3);
}
public void garch_models() {
System.out.println("ARCH and GARCH models");
// σ_t^2 = 0.2 + 0.212 * ε_(t-1)^2
GARCHModel arch1
= new GARCHModel(
0.2, // constant
new double[]{0.212}, // ARCH terms
new double[]{} // no GARCH terms
);
System.out.println(arch1);
System.out.printf("conditional variance = %f%n", arch1.var());
// σ_t^2= 0.2+0.212ε_(t-1)^2+0.106σ_(t-1)^2
GARCHModel garch11
= new GARCHModel(
0.2, // constant
new double[]{0.212}, // ARCH terms
new double[]{0.106} // GARCH terms
);
System.out.println(garch11);
System.out.printf("conditional variance = %f%n", garch11.var());
// simulate the GARCH process
GARCHSim sim = new GARCHSim(garch11);
sim.seed(1234567890L);
double[] series = RNGUtils.nextN(sim, 200);
System.out.println(Arrays.toString(series));
}
/**
*
* x<-c(0.2,0.3,-0.1,0.4,-0.5,0.6,0.1,0.2) adf.test(c(x,x),alternative =
* c("stationary"),4)
*
* The p-value from R is obtained form the interpolation
* (0.9-0.1)/(3.24-1.14)=(x-0.1)/(3.24-2.642)
* "x=0.8/(3.24-1.14)*(3.24-2.642)+0.1=0.3278"
*
* The values -3.24 and -1.14 are from table 4.2c in A. Banerjee, J. J.
* Dolado, J. W. Galbraith, and D. F. Hendry (1993): Cointegration, Error
* Correction, and the Econometric Analysis of Non-Stationary Data, Oxford
* University Press, Oxford.
*/
public void adf() {
System.out.println("unit root test using the augmented Dickey-Fuller test");
double[] sample = new double[]{0.2, 0.3, -0.1, 0.4, -0.5, 0.6, 0.1, 0.2, 0.2, 0.3, -0.1, 0.4, -0.5, 0.6, 0.1, 0.2};
AugmentedDickeyFuller adf
= new AugmentedDickeyFuller(
sample,
TrendType.CONSTANT, // constant drift term
4, // the lag order
null
);
System.out.println("H0: " + adf.getNullHypothesis());
System.out.println("H1: " + adf.getAlternativeHypothesis());
System.out.printf("the p-value for the test = %f%n", adf.pValue());
System.out.printf("the statistics for the test = %f%n", adf.statistics());
}
public void goodness_of_fit() {
System.out.println("check the goodness of fit using the Ljung-Box test");
// define an ARMA(1, 1)
ARMAModel arma11 = new ARMAModel(
new double[]{0.2}, // the AR coefficients
new double[]{1.1} // the MA coefficients
);
// create a random number generator from the ARMA model
ARIMASim sim = new ARIMASim(arma11);
sim.seed(1234567890L);
// generate a random time series
final int T = 50; // length of the time series
double[] x = new double[T];
for (int i = 0; i < T; ++i) {
// call the RNG to generate random numbers according to the specification
x[i] = sim.nextDouble();
}
// fit an ARMA(1,1) model
ConditionalSumOfSquares css
= new ConditionalSumOfSquares(x, 1, 0, 1);
ARMAModel arma11_css = css.getARMAModel();
System.out.printf("The fitted ARMA(1,1) model is %s%n", arma11_css);
System.out.printf("The fitted ARMA(1,1) model, demeanded, is %s%n", arma11_css.getDemeanedModel());
System.out.printf("AIC = %f%n", css.AIC());
// compute the fitted values using the fitted model
ARMAForecastOneStep forecaset = new ARMAForecastOneStep(x, arma11_css);
double[] x_hat = new double[T];
double[] residuals = new double[T];
for (int i = 0; i < T; ++i) {
// the fitted value
x_hat[i] = forecaset.xHat(i);
// the residuals
residuals[i] = x[i] - x_hat[i];
}
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(x_hat));
System.out.println(Arrays.toString(residuals));
// run the goodness-of-fit test
LjungBox test = new LjungBox(
residuals,
4, // check up to the 4-th lag
2 // number of parameters
);
System.out.printf("The test statistic = %f%n", test.statistics());
System.out.printf("The p-value = %f%n", test.pValue());
}
public void arma_forecast() {
System.out.println("making forecasts for an ARMA model");
// define an ARMA(1, 1)
ARMAModel arma11 = new ARMAModel(
new double[]{0.2}, // the AR coefficients
new double[]{1.1} // the MA coefficients
);
// create a random number generator from the ARMA model
ARIMASim sim = new ARIMASim(arma11);
sim.seed(1234567890L);
// generate a random time series
final int T = 50; // length of the time series
double[] x = new double[T];
for (int i = 0; i < T; ++i) {
// call the RNG to generate random numbers according to the specification
x[i] = sim.nextDouble();
}
// compute the one-step conditional expectations for the model
ARMAForecastOneStep forecaset = new ARMAForecastOneStep(x, arma11);
double[] x_hat = new double[T];
double[] residuals = new double[T];
for (int i = 0; i < T; ++i) {
// the one-step conditional expectations
x_hat[i] = forecaset.xHat(i);
// the errors
residuals[i] = x[i] - x_hat[i];
}
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(x_hat));
System.out.println(Arrays.toString(residuals));
}
public void arma23() throws IOException {
System.out.println("ARMA(2,3) model");
// build an ARMA(2, 3)
ARMAModel arma23 = new ARMAModel(
new double[]{0.6, -0.23}, // the AR coefficients
new double[]{0.1, 0.2, 0.4} // the MA coefficients
);
// compute the linear representation
LinearRepresentation ma = new LinearRepresentation(arma23);
for (int i = 1; i <= 20; i++) {
System.out.printf("the coefficients of the linear representation at lag %d = %f%n", i, ma.AR(i));
}
// compute the autocovariance function for the model
AutoCovariance acvf = new AutoCovariance(arma23);
for (int i = 1; i < 10; i++) {
System.out.printf("the acvf of the ARMA(2,3) model at lag%d: %f%n", i, acvf.evaluate(i));
}
// compute the autocorrelation function for the model
AutoCorrelation acf = new AutoCorrelation(arma23, 10);
for (int i = 0; i < 10; i++) {
System.out.printf("the acf of the ARMA(2,3) model at lag%d: %f%n", i, acf.evaluate(i));
}
}
public void arma11() throws IOException {
System.out.println("ARMA(1,1) model");
// build an ARMA(1, 1)
ARMAModel arma11 = new ARMAModel(
new double[]{0.2}, // the AR coefficients
new double[]{1.1} // the MA coefficients
);
// compute the linear representation
LinearRepresentation ma = new LinearRepresentation(arma11);
for (int i = 1; i <= 20; i++) {
System.out.printf("the coefficients of the linear representation at lag %d = %f%n", i, ma.AR(i));
}
// compute the autocovariance function for the model
AutoCovariance acvf = new AutoCovariance(arma11);
for (int i = 1; i < 10; i++) {
System.out.printf("the acvf of the ARMA(1,1) model at lag%d: %f%n", i, acvf.evaluate(i));
}
// compute the autocorrelation function for the model
AutoCorrelation acf = new AutoCorrelation(arma11, 10);
for (int i = 0; i < 10; i++) {
System.out.printf("the acf of the ARMA(1,1) model at lag%d: %f%n", i, acf.evaluate(i));
}
}
public void ma() throws IOException {
System.out.println("MA models");
// define an MA(1) model
MAModel ma1 = new MAModel(
new double[]{0.8} // theta_1 = 0.8
);
final int nLags = 10;
// compute the autocovariance function for the model
AutoCovariance acvf1 = new AutoCovariance(ma1);
for (int i = 0; i < nLags; i++) {
System.out.printf("the acvf of the MA(1) model at lag%d = %f%n", i, acvf1.evaluate(i));
}
// compute the autocorrelation function for the model
AutoCorrelation acf1 = new AutoCorrelation(ma1, nLags);
for (int i = 0; i < nLags; i++) {
System.out.printf("the acf of the MA(1) model at lag%d = %f%n", i, acf1.evaluate(i));
}
// define an MA(2) model
MAModel ma2 = new MAModel(
new double[]{-0.2, 0.01} // the moving-average coefficients
);
AutoCovariance acvf2 = new AutoCovariance(ma2);
for (int i = 1; i < 10; i++) {
System.out.printf("the acvf of the MA(2) model at lag%d: %f%n", i, acvf2.evaluate(i));
}
AutoCorrelation acf2 = new AutoCorrelation(ma2, 10);
for (int i = 0; i < 10; i++) {
System.out.printf("the acf of the MA(2) model at lag%d: %f%n", i, acf2.evaluate(i));
}
// define an MA(2) model
MAModel ma3 = new MAModel(
0.1, // the mean
new double[]{-0.5, 0.01}, // the moving-average coefficients
10. // the standard deviation
);
// create a random number generator from the MA model
ARIMASim sim = new ARIMASim(ma3);
sim.seed(1234567890L);
// generate a random time series
final int T = 500; // length of the time series
double[] x = new double[T];
for (int i = 0; i < T; ++i) {
// call the RNG to generate random numbers according to the specification
x[i] = sim.nextDouble();
}
// determine the cutoff lag using autocorrelation
SampleAutoCorrelation acf3_hat
= new SampleAutoCorrelation(new SimpleTimeSeries(x));
for (int i = 0; i < 5; i++) {
System.out.printf("the empirical acf of a time series at lag%d: %f%n", i, acf3_hat.evaluate(i));
}
// fit an MA(2) model using the data
ConditionalSumOfSquares fit
= new ConditionalSumOfSquares(
x,
0,
0,
2); // the MA order
System.out.printf("theta: %s%n", Arrays.toString(fit.getARMAModel().theta()));
System.out.printf("var of error term: %s%n", fit.var());
// make forecast
ARMAForecast forecast_ma3
= new ARMAForecast(new SimpleTimeSeries(x), ma3);
System.out.println("The forecasts for the MA(2) model are");
for (int j = 0; j < 10; ++j) {
System.out.println(forecast_ma3.next());
}
}
public void ar() throws IOException {
System.out.println("AR models");
// define an AR(1) model
ARModel ar1 = new ARModel(
new double[]{0.6} // phi_1 = 0.6
);
final int nLags = 10;
// compute the autocovariance function for the model
AutoCovariance acvf1 = new AutoCovariance(ar1);
for (int i = 0; i < nLags; i++) {
System.out.printf("the acvf of the AR(1) model at lag%d = %f%n", i, acvf1.evaluate(i));
}
// compute the autocorrelation function for the model
AutoCorrelation acf1 = new AutoCorrelation(ar1, nLags);
for (int i = 0; i < nLags; i++) {
System.out.printf("the acf of the AR(1) model at lag%d = %f%n", i, acf1.evaluate(i));
}
// define an AR(2) model
ARModel ar2 = new ARModel(
new double[]{1.2, -0.35}
);
// compute the autocovariance function for the model
AutoCovariance acvf2 = new AutoCovariance(ar2);
for (int i = 1; i < nLags; i++) {
System.out.printf("the acvf of the AR(2) model at lag%d = %f%n", i, acvf2.evaluate(i));
}
// compute the autocorrelation function for the model
AutoCorrelation acf2 = new AutoCorrelation(ar2, 10);
for (int i = 0; i < nLags; i++) {
System.out.printf("the acf of the AR(2) model at lag%d = %f%n", i, acf2.evaluate(i));
}
// create a random number generator from the AR model
ARIMASim sim = new ARIMASim(ar2);
sim.seed(1234567890L);
// generate a random time series
final int T = 500; // length of the time series
double[] x = new double[T];
for (int i = 0; i < T; ++i) {
// call the RNG to generate random numbers according to the specification
x[i] = sim.nextDouble();
}
// determine the cutoff lag using partial autocorrelation
SamplePartialAutoCorrelation acf2_hat
= new SamplePartialAutoCorrelation(new SimpleTimeSeries(x));
for (int i = 1; i < 5; i++) {
System.out.printf("the empirical pacf of a time series at lag%d: %f%n", i, acf2_hat.evaluate(i));
}
// fit an AR(2) model using the data
ConditionalSumOfSquares fit
= new ConditionalSumOfSquares(
x,
2, // the AR order
0,
0);
System.out.printf("phi: %s%n", Arrays.toString(fit.getARMAModel().phi()));
System.out.printf("var of error term: %s%n", fit.var());
// make forecast
ARMAForecast forecast_ar2
= new ARMAForecast(new SimpleTimeSeries(x), ar2);
System.out.println("The forecasts for the AR(2) model are");
for (int j = 0; j < 10; ++j) {
System.out.println(forecast_ar2.next());
}
}
public void decomposition() throws IOException {
System.out.println("classical time series decomposition");
RandomNumberGenerator rng = new StandardNormalRNG();
// construct an additive model
AdditiveModel additive_model = new AdditiveModel(
// the trend
new double[]{
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
},
// the seasonal component
new double[]{
0, 1
},
// the source of randomness
rng
);
System.out.println(additive_model);
// construct a multiplicative model
MultiplicativeModel multiplicative_model = new MultiplicativeModel(
// the trend
new double[]{
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
},
// the seasonal component
new double[]{
-1, 1
},
// the source of randomness
rng
);
System.out.println(multiplicative_model);
}
public void sp500_monthly() throws IOException {
System.out.println("univariate time series of S&P 500 monthly close");
// read the monthly S&P 500 data from a csv file
double[][] spx_arr
= DoubleUtils.readCSV2d(
this.getClass().getClassLoader().getResourceAsStream("sp500_monthly.csv"),
true,
true
);
// convert the csv file into a matrix for manipulation
Matrix spx_M = new DenseMatrix(spx_arr);
// extract the column of adjusted close prices
Vector spx_v = spx_M.getColumn(5); // adjusted closes
// construct a univariate time series from the data