-
Notifications
You must be signed in to change notification settings - Fork 57
/
exampleanalyticsdoc.js
1864 lines (1621 loc) · 63.3 KB
/
exampleanalyticsdoc.js
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
require('qminer').la.Vector.prototype.print = function () { };require('qminer').la.SparseVector.prototype.print = function () { };require('qminer').la.SparseMatrix.prototype.print = function () { };require('qminer').la.Matrix.prototype.print = function () { };describe('example tests for the analyticsdoc.js file', function () {
describe("Analytics module, number 1", function () {
it("should make test number 1", function () {
// import module, load dataset, create model, evaluate model
});
});
describe("SV, number 2", function () {
it("should make test number 2", function () {
// import modules
var la = require('qminer').la;
var analytics = require('qminer').analytics;
// CLASSIFICATION WITH SVC
// set up fake train and test data
// four training examples with number of features = 2
var featureMatrix = new la.Matrix({ rows: 2, cols: 4, random: true });
// classification targets for four examples
var targets = new la.Vector([-1, -1, 1, 1]);
// set up the classification model
var SVC = new analytics.SVC({ verbose: false });
// train classifier
SVC.fit(featureMatrix, targets);
// set up a fake test vector
var test = new la.Vector([1.1, -0.5]);
// predict the target value
var prediction = SVC.predict(test);
});
});
describe("Returns the SVC parameters, number 3", function () {
it("should make test number 3", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new SVC model with json
var SVC = new analytics.SVC({ c: 5, j: 10, batchSize: 2000, maxIterations: 12000, maxTime: 2, minDiff: 1e-10, verbose: true });
// get the parameters of the SVC model
// returns { algorithm: 'SGD' c: 5, j: 10, batchSize: 2000, maxIterations: 12000, maxTime: 2, minDiff: 1e-10, verbose: true }
var json = SVC.getParams();
});
});
describe("Sets the SVC parameters, number 4", function () {
it("should make test number 4", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a default SVC model
var SVC = new analytics.SVC();
// change the parameters of the SVC with the json { j: 5, maxIterations: 12000, minDIff: 1e-10 }
SVC.setParams({ j: 5, maxIterations: 12000, minDiff: 1e-10 }); // returns self
});
});
describe("Gets the vector of coefficients of the linear model, number 5", function () {
it("should make test number 5", function () {
// import the analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVC object
var SVC = new analytics.SVC();
// create the matrix containing the input features and the input vector for each matrix.
var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 1, -1, -1]);
// fit the model
SVC.fit(matrix, vec);
// get the weights
var weights = SVC.weights; // returns the coefficients of the normal vector of the hyperplane gained from the model: [1, 1]
});
});
describe("Saves model to output file stream, number 6", function () {
it("should make test number 6", function () {
// import the analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new SVC object
var SVC = new analytics.SVC();
// create the matrix containing the input features and the input vector for each matrix column.
var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// fit the model
SVC.fit(matrix, vec);
// create output stream
var fout = fs.openWrite('svc_example.bin');
// save SVC object (model and parameters) to output stream and close it
SVC.save(fout);
fout.close();
// create input stream
var fin = fs.openRead('svc_example.bin');
// create a SVC object that loads the model and parameters from input stream
var SVC2 = new analytics.SVC(fin);
});
});
describe("Sends vector through the model and returns the distance to the decision boundery, number 7", function () {
it("should make test number 7", function () {
// import the analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVC object
var SVC = new analytics.SVC();
// create the matrix containing the input features and the input vector for each matrix
var matrix = new la.Matrix([[1, 0], [0, -1]]);
var vec = new la.Vector([1, -1]);
// fit the model
SVC.fit(matrix, vec);
// create the vector you want to get the distance from the model
var vec2 = new la.Vector([2, 3]);
// use the decisionFunction to get the distance of vec2 from the model
var distance = SVC.decisionFunction(vec2); // returns something close to 5
});
});
describe("Sends vector through the model and returns the prediction as a real number, number 8", function () {
it("should make test number 8", function () {
// import the analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVC object
var SVC = new analytics.SVC();
// create the matrix containing the input features and the input vector for each matrix
var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 1, -1, -1]);
// fit the model
SVC.fit(matrix, vec);
// create a vector you want to predict
var vec2 = new la.Vector([3, 5]);
// predict the vector
var prediction = SVC.predict(vec2); // returns 1
});
});
describe("Fits a SVM classification model, given column examples in a matrix and vector of targets, number 9", function () {
it("should make test number 9", function () {
// import the analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVC object
var SVC = new analytics.SVC();
// create the matrix containing the input features and the input vector for each matrix.
var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 1, -1, -1]);
// fit the model
SVC.fit(matrix, vec); // creates a model, where the hyperplane has the normal semi-equal to [1, 1]
});
});
describe("SV, number 10", function () {
it("should make test number 10", function () {
// import module
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// REGRESSION WITH SVR
// Set up fake train and test data.
// Four training examples with, number of features = 2
var featureMatrix = new la.Matrix({ rows: 2, cols: 4, random: true });
// Regression targets for four examples
var targets = new la.Vector([1.1, -2, 3, 4.2]);
// Set up the regression model
var SVR = new analytics.SVR({ verbose: false });
// Train regression
SVR.fit(featureMatrix, targets);
// Set up a fake test vector
var test = new la.Vector([1.1, -0.8]);
// Predict the target value
var prediction = SVR.predict(test);
});
});
describe("Returns the SVR parameters, number 11", function () {
it("should make test number 11", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new SVR object
var SVR = new analytics.SVR({ c: 10, eps: 1e-10, maxTime: 12000, verbose: true });
// get the parameters of SVR
var params = SVR.getParams();
});
});
describe("Sets the SVR parameters, number 12", function () {
it("should make test number 12", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new SVR object
var SVR = new analytics.SVR();
// set the parameters of the SVR object
SVR.setParams({ c: 10, maxTime: 12000 });
});
});
describe("Saves model to output file stream, number 13", function () {
it("should make test number 13", function () {
// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new SVR object
var SVR = new analytics.SVR({ c: 10 });
// create a matrix and vector for the model
var matrix = new la.Matrix([[1, -1], [1, 1]]);
var vector = new la.Vector([1, 1]);
// create the model by fitting the values
SVR.fit(matrix, vector);
// save the model in a binary file
var fout = fs.openWrite('svr_example.bin');
SVR.save(fout);
fout.close();
// construct a SVR model by loading from the binary file
var fin = fs.openRead('svr_example.bin');
var SVR2 = new analytics.SVR()
});
});
describe("Sends vector through the model and returns the scalar product as a real number, number 14", function () {
it("should make test number 14", function () {
// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVR object
var SVR = new analytics.SVR({ c: 10 });
// create a matrix and vector for the model
var matrix = new la.Matrix([[1, -1], [1, 1]]);
var vector = new la.Vector([1, 1]);
// create the model by fitting the values
SVR.fit(matrix, vector);
// get the distance between the model and the given vector
var vec2 = new la.Vector([-5, 1]);
var distance = SVR.decisionFunction(vec2);
});
});
describe("Sends vector through the model and returns the prediction as a real number, number 15", function () {
it("should make test number 15", function () {
// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVR object
var SVR = new analytics.SVR({ c: 10 });
// create a matrix and vector for the model
var matrix = new la.Matrix([[1, -1], [1, 1]]);
var vector = new la.Vector([1, 1]);
// create the model by fitting the values
SVR.fit(matrix, vector);
// predict the value of the given vector
var vec2 = new la.Vector([-5, 1]);
var prediction = SVR.predict(vec2);
});
});
describe("fits an SVM regression model, given column examples in a matrix and vector of target, number 16", function () {
it("should make test number 16", function () {
// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new SVR object
var SVR = new analytics.SVR({ c: 10 });
// create a matrix and vector for the model
var matrix = new la.Matrix([[1, -1], [1, 1]]);
var vector = new la.Vector([1, 1]);
// create the model by fitting the values
SVR.fit(matrix, vector);
});
});
describe("Ridge regression. Minimizes: ||A' x - b||^2 + ||gamma x||^, number 17", function () {
it("should make test number 17", function () {
// import modules
la = require('qminer').la;
analytics = require('qminer').analytics;
// create a new model with gamma = 1.0
var regmod = new analytics.RidgeReg({ gamma: 1.0 });
// generate a random feature matrix
var A = la.randn(10,100);
// generate a random model
var w = la.randn(10);
// generate noise
var n = la.randn(100).multiply(0.01);
// generate responses (model'*data + noise)
var b = A.transpose().multiply(w).plus(n);
// fit model
regmod.fit(A, b);
// compare
// true model
w.print();
// trained model');
regmod.weights.print();
// cosine between the true and the estimated model should be close to 1 if the fit succeeded
var cos = regmod.weights.cosine(w);
});
});
describe("Gets the parameters, number 18", function () {
it("should make test number 18", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg({ gamma: 5 });
// get the parameters
// returns a json object { gamma: 5 }
var param = regmod.getParams();
});
});
describe("Set the parameters, number 19", function () {
it("should make test number 19", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg({ gamma: 5 });
// set the parameters of the object
var param = regmod.setParams({ gamma: 10 });
});
});
describe("Fits a column matrix of feature vectors X onto the response variable y, number 20", function () {
it("should make test number 20", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg();
// create the test matrix and vector
var X = new la.Matrix([[1, 2], [1, -1]]);
var y = new la.Vector([3, 3]);
// fit the model with X and y
// the weights of the model are 2, 1
regmod.fit(X, y);
});
});
describe("Returns the expected response for the provided feature vector, number 21", function () {
it("should make test number 21", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg();
// create the test matrix and vector
var X = new la.Matrix([[1, 2], [1, -1]]);
var y = new la.Vector([3, 3]);
// fit the model with X and y
regmod.fit(X, y);
// create a new vector for the prediction
var vec = new la.Vector([3, 4]);
// create the prediction
// returns the value 10
var prediction = regmod.decisionFunction(vec);
});
});
describe("Returns the expected response for the provided feature vector, number 22", function () {
it("should make test number 22", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg();
// create the test matrix and vector
var X = new la.Matrix([[1, 2], [1, -1]]);
var y = new la.Vector([3, 3]);
// fit the model with X and y
regmod.fit(X, y);
// create a new vector for the prediction
var vec = new la.Vector([3, 4]);
// create the prediction
// returns the value 10
var prediction = regmod.predict(vec);
});
});
describe("Saves the model into the output stream, number 23", function () {
it("should make test number 23", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new Ridge Regression object
var regmod = new analytics.RidgeReg();
// create the test matrix and vector
var X = new la.Matrix([[1, 2], [1, -1]]);
var y = new la.Vector([3, 3]);
// fit the model with X and y
regmod.fit(X, y);
// create an output stream object and save the model
var fout = fs.openWrite('regmod_example.bin');
regmod.save(fout);
fout.close();
// create a new Ridge Regression model by loading the model
var fin = fs.openRead('regmod_example.bin');
var regmod2 = new analytics.RidgeReg(fin);
});
});
describe("Sigmoid function (y = 1/[1 + exp[, number 24", function () {
it("should make test number 24", function () {
// import modules
la = require('qminer').la;
analytics = require('qminer').analytics;
// create a new model
var sigmoid = new analytics.Sigmoid();
// generate a random predictions
var x = new la.Vector([0.5, 2.3, -0.1, 0.5, -7.3, 1.2]);
// generate a random labels
var y = new la.Vector([1, 1, -1, 1, -1, -1]);
// fit model
sigmoid.fit(x, y);
// get predictions
var pred1 = sigmoid.predict(1.2);
var pred2 = sigmoid.predict(-1.2);
});
});
describe("Get the parameters. It doesn't do anything, it's only for consistency for constructing pipeline, number 25", function () {
it("should make test number 25", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// get the parameters
// returns an empty Json object
var param = s.getParams();
});
});
describe("Sets the parameters. It doesn't do anything, it's only for consistency for constructing pipeline, number 26", function () {
it("should make test number 26", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// set the parameters
// doesn't change the model
s.setParams({});
});
});
describe("Gets the model, number 27", function () {
it("should make test number 27", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// get the model parameters
// returns a Json object { A: 0, B: 0 }
var model = s.getModel();
});
});
describe("Fits a column matrix of feature vectors X onto the response variable y, number 28", function () {
it("should make test number 28", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// create the predicted values and the binary labels
var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
var y = new la.Vector([-1, -1, -1, 1, 1, 1]);
// fit the model
// changes the internal A and B values of the model
// (these values can be obtained with the getModel method)
s.fit(X, y);
});
});
describe("Returns the expected response for the provided feature vector, number 29", function () {
it("should make test number 29", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// create the predicted values and the binary labels
var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
var y = new la.Vector([-1, -1, -1, 1, 1, 1]);
// fit the model
s.fit(X, y);
// predict the probability of the value 0 on this model
// returns 0.5
var prediction = s.decisionFunction(0.5);
});
});
describe("Returns the expected response for the provided feature vector, number 30", function () {
it("should make test number 30", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// create the predicted values and the binary labels
var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
var y = new la.Vector([-1, -1, -1, 1, 1, 1]);
// fit the model
s.fit(X, y);
// predict the probability of the value 0 on this model
// returns 0.5
var prediction = s.predict(0.5);
});
});
describe("Saves the model into the output stream, number 31", function () {
it("should make test number 31", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create the Sigmoid model
var s = new analytics.Sigmoid();
// create the predicted values and the binary labels
var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
var y = new la.Vector([-1, -1, -1, 1, 1, 1]);
// fit the model
s.fit(X, y);
// create an output stream object and save the model
var fout = fs.openWrite('sigmoid_example.bin');
s.save(fout);
fout.close();
// create a new Sigmoid model by loading the model
var fin = fs.openRead('sigmoid_example.bin');
var s2 = new analytics.Sigmoid(fin);
});
});
describe("Nearest Neighbour Anomaly Detection, number 32", function () {
it("should make test number 32", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
// create a sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// predict if the vector is an anomaly or not
var prediction = neighbor.predict(vector);
});
});
describe("Sets parameters, number 33", function () {
it("should make test number 33", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// set it's parameters to rate: 0.1
neighbor.setParams({ rate: 0.1 });
});
});
describe("Returns parameters, number 34", function () {
it("should make test number 34", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// get the parameters of the object
// returns a json object { rate: 0.05 }
var params = neighbor.getParams();
});
});
describe("Save model to provided output stream, number 35", function () {
it("should make test number 35", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create an output stream object and save the model
var fout = fs.openWrite('neighbor_example.bin');
neighbor.save(fout);
fout.close();
// create a new Nearest Neighbor Anomaly model by loading the model
var fin = fs.openRead('neighbor_example.bin');
var neighbor2 = new analytics.NearestNeighborAD(fin);
});
});
describe("Returns the model, number 36", function () {
it("should make test number 36", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
// get the model of the object
// returns a json object { rate: 0.1, window: 0 }
var model = neighbor.getModel();
});
});
describe("Adds a new point to the known points and recomputes the threshold, number 37", function () {
it("should make test number 37", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 2], [1, 5]]);
// update the model with the vector
neighbor.partialFit(vector);
});
});
describe("Analyzes the nearest neighbor distances and computes the detector threshold based on the rate parameter, number 38", function () {
it("should make test number 38", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
});
});
describe("Compares the point to the known points and returns distance to the nearest one, number 39", function () {
it("should make test number 39", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// get the distance of the vector from the model
var prediction = neighbor.decisionFunction(vector); // returns 1
});
});
describe("Compares the point to the known points and returns 1 if it's too far away (based on the precomputed threshold), number 40", function () {
it("should make test number 40", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// check if the vector is an anomaly
var prediction = neighbor.predict(vector); // returns 1
});
});
describe("Returns a JSON object that encodes the ID of the nearest neighbor and the features that contributed to the distanc, number 41", function () {
it("should make test number 41", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({rate:0.05, windowSize:3});
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix and provide a vector record IDs
neighbor.fit(matrix, new la.IntVector([3541,1112,4244]));
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// check if the vector is an anomaly
var explanation = neighbor.explain(vector); // returns an explanation
});
});
describe("Recursive Linear Regressio, number 42", function () {
it("should make test number 42", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the recursive linear regression model holder
var linreg = new analytics.RecLinReg({ dim: 10, regFact: 1.0, forgetFact: 1.0 });
});
});
describe("Creates a partial fit of the input, number 43", function () {
it("should make test number 43", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 3.0 });
// create a new dense vector
var vec = new la.Vector([1, 2, 3]);
// fit the model with the vector
linreg.partialFit(vec, 6);
});
});
describe("Creates a fit of the input, number 44", function () {
it("should make test number 44", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 2.0 });
// create a new dense matrix and target vector
var mat = new la.Matrix([[1, 2, 3], [3, 4, 5]]);
var vec = new la.Vector([3, 5, -1]);
// fit the model with the matrix
linreg.fit(mat, vec);
});
});
describe("Puts the vector through the model and returns the prediction as a real number, number 45", function () {
it("should make test number 45", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 2.0, recFact: 1e-10 });
// create a new dense matrix and target vector
var mat = new la.Matrix([[1, 2], [1, -1]]);
var vec = new la.Vector([3, 3]);
// fit the model with the matrix
linreg.fit(mat, vec);
// create the vector to be predicted
var pred = new la.Vector([1, 1]);
// predict the value of the vector
var prediction = linreg.predict(pred); // returns something close to 3.0
});
});
describe("Sets the parameters of the model, number 46", function () {
it("should make test number 46", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 10 });
// set the parameters of the model
linreg.setParams({ dim: 3, recFact: 1e2, forgetFact: 0.5 });
});
});
describe("Returns the parameters, number 47", function () {
it("should make test number 47", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a new Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 10 });
// get the parameters of the model
var params = linreg.getParams(); // returns { dim: 10, recFact: 1.0, forgetFact: 1.0 }
});
});
describe("Save model to provided output stream, number 48", function () {
it("should make test number 48", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create the Recursive Linear Regression model
var linreg = new analytics.RecLinReg({ dim: 2.0, recFact: 1e-10 });
// create a new dense matrix and target vector
var mat = new la.Matrix([[1, 2], [1, -1]]);
var vec = new la.Vector([3, 3]);
// fit the model with the matrix
linreg.fit(mat, vec);
// create an output stream object and save the model
var fout = fs.openWrite('linreg_example.bin');
linreg.save(fout);
fout.close();
// create a new Nearest Neighbor Anomaly model by loading the model
var fin = fs.openRead('linreg_example.bin');
var linreg2 = new analytics.RecLinReg(fin);
});
});
describe("Logistic regression model. Uses Newtons method to compute the weights, number 49", function () {
it("should make test number 49", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the Logistic Regression model
var logreg = new analytics.LogReg({ lambda: 2 });
});
});
describe("Gets the parameters, number 50", function () {
it("should make test number 50", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create the Logistic Regression model
var logreg = new analytics.LogReg({ lambda: 10 });
// get the parameters of the model
var param = logreg.getParams(); // returns { lambda: 10, intercept: false }
});
});
describe("Set the parameters, number 51", function () {
it("should make test number 51", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a logistic regression model
var logreg = new analytics.LogReg({ lambda: 10 });
// set the parameters of the model
logreg.setParams({ lambda: 1 });
});
});
describe("Fits a column matrix of feature vectors X onto the response variable y, number 52", function () {
it("should make test number 52", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas is used, fit the model
if (require('qminer').flags.blas) {
logreg.fit(mat, vec);
}
});
});
describe("Returns the expected response for the provided feature vector, number 53", function () {
it("should make test number 53", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas is used
if (require('qminer').flags.blas) {
// fit the model
logreg.fit(mat, vec);
// create the vector for the prediction
var test = new la.Vector([1, 1]);
// get the prediction
var prediction = logreg.predict(test);
};
});
});
describe("Saves the model into the output stream, number 54", function () {
it("should make test number 54", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas is used, fit the model
if (require('qminer').flags.blas) {
logreg.fit(mat, vec);
};
// create an output stream object and save the model
var fout = fs.openWrite('logreg_example.bin');
logreg.save(fout);
fout.close();
// create input stream
var fin = fs.openRead('logreg_example.bin');
// create a Logistic Regression object that loads the model and parameters from input stream
var logreg2 = new analytics.LogReg(fin);
});
});
describe("Proportional Hazards Model with a constant hazard function, number 55", function () {
it("should make test number 55", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a Proportional Hazard model
var hazard = new analytics.PropHazards();
});
});
describe("Gets the parameters of the model, number 56", function () {
it("should make test number 56", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a Proportional Hazard model
var hazard = new analytics.PropHazards({ lambda: 5 });
// get the parameters of the model
var param = hazard.getParams();
});
});
describe("Sets the parameters of the model, number 57", function () {
it("should make test number 57", function () {
// import analytics module
var analytics = require('qminer').analytics;
// create a Proportional Hazard model
var hazard = new analytics.PropHazards({ lambda: 5 });
// set the parameters of the model
hazard.setParams({ lambda: 10 });
});
});
describe("Fits a column matrix of feature vectors X onto the response variable y, number 58", function () {
it("should make test number 58", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Proportional Hazards model
var hazards = new analytics.PropHazards();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas used, fit the model
if (require('qminer').flags.blas) {
hazards.fit(mat, vec);
};
});
});
describe("Returns the expected response for the provided feature vector, number 59", function () {
it("should make test number 59", function () {
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the Proportional Hazards model
var hazards = new analytics.PropHazards();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 1], [1, -1]]);