-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathNumPy_random.tests.cs
More file actions
1841 lines (1552 loc) · 64.2 KB
/
NumPy_random.tests.cs
File metadata and controls
1841 lines (1552 loc) · 64.2 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) 2019 by the SciSharp Team
// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Numpy.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Python.Runtime;
using Assert = NUnit.Framework.Assert;
namespace Numpy.UnitTest
{
[TestClass]
public class NumPy_randomTest : BaseTestCase
{
[TestMethod]
public void randTest()
{
// >>> np.random.rand(3,2)
// array([[ 0.14022471, 0.96360618], #random
// [ 0.37601032, 0.25528411], #random
// [ 0.49313049, 0.94909878]]) #random
//
np.random.seed(0);
var given= np.random.rand(1,2);
var expected = "array([[0.5488135 , 0.71518937]])";
Assert.AreEqual(expected, given.repr);
np.random.seed(0);
float x = np.random.rand();
Assert.AreEqual(0.5488135039273248f, x);
}
[TestMethod]
public void randnTest()
{
// >>> np.random.randn()
// 2.1923875335537315 #random
//
np.random.seed(0);
var given = np.random.randn();
Assert.AreEqual(1.76405239f, given);
// Two-by-four array of samples from N(3, 6.25):
// >>> 2.5 * np.random.randn(2, 4) + 3
// array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
// [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random
//
np.random.seed(0);
var a = 2.5 * np.random.randn(1, 2) + 3;
var expected= "array([[7.41013086, 4.00039302]])";
Assert.AreEqual(expected, a.repr);
}
[TestMethod]
public void randintTest()
{
// >>> np.random.randint(2, size=10)
// array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
// >>> np.random.randint(1, size=10)
// array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
//
var given = np.random.randint(2, size: new int[] { 10 });
Assert.LessOrEqual( (int)np.sum(given), 10);
given = np.random.randint(1, size: new int[] { 10 });
var expected =
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])";
Assert.AreEqual(expected, given.repr);
// Generate a 2 x 4 array of ints between 0 and 4, inclusive:
// >>> np.random.randint(5, size=(2, 4))
// array([[4, 0, 2, 1],
// [3, 2, 2, 0]])
//
given= np.random.randint(5, size:new int[]{2, 4});
Assert.LessOrEqual((int)given.max(), 4);
Assert.GreaterOrEqual((int)given.min(), 0);
}
[TestMethod]
public void random_integersTest()
{
// >>> np.random.random_integers(5)
// 4
// >>> type(np.random.random_integers(5))
// <type 'int'>
// >>> np.random.random_integers(5, size=(3,2))
// array([[5, 4],
// [3, 3],
// [4, 5]])
//
#if TODO
var given= np.random.random_integers(5);
var expected=
"4";
Assert.AreEqual(expected, given.repr);
given= type(np.random.random_integers(5));
expected=
"<type 'int'>";
Assert.AreEqual(expected, given.repr);
given= np.random.random_integers(5, size=(3,2));
expected=
"array([[5, 4],\n" +
" [3, 3],\n" +
" [4, 5]])";
Assert.AreEqual(expected, given.repr);
#endif
// Choose five random numbers from the set of five evenly-spaced
// numbers between 0 and 2.5, inclusive (i.e., from the set
// ):
// >>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4.
// array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ])
//
#if TODO
given= 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4.;
expected=
"array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ])";
Assert.AreEqual(expected, given.repr);
#endif
// Roll two six sided dice 1000 times and sum the results:
// >>> d1 = np.random.random_integers(1, 6, 1000)
// >>> d2 = np.random.random_integers(1, 6, 1000)
// >>> dsums = d1 + d2
//
#if TODO
given= d1 = np.random.random_integers(1, 6, 1000);
given= d2 = np.random.random_integers(1, 6, 1000);
given= dsums = d1 + d2;
#endif
// Display results as a histogram:
// >>> import matplotlib.pyplot as plt
// >>> count, bins, ignored = plt.hist(dsums, 11, density=True)
// >>> plt.show()
//
#if TODO
given= import matplotlib.pyplot as plt;
given= count, bins, ignored = plt.hist(dsums, 11, density=True);
given= plt.show();
#endif
}
[TestMethod]
public void random_sampleTest()
{
// >>> np.random.random_sample()
// 0.47108547995356098
// >>> type(np.random.random_sample())
// <type 'float'>
// >>> np.random.random_sample((5,))
// array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
//
#if TODO
var given= np.random.random_sample();
var expected=
"0.47108547995356098";
Assert.AreEqual(expected, given.repr);
given= type(np.random.random_sample());
expected=
"<type 'float'>";
Assert.AreEqual(expected, given.repr);
given= np.random.random_sample((5,));
expected=
"array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])";
Assert.AreEqual(expected, given.repr);
#endif
// Three-by-two array of random numbers from [-5, 0):
// >>> 5 * np.random.random_sample((3, 2)) - 5
// array([[-3.99149989, -0.52338984],
// [-2.99091858, -0.79479508],
// [-1.23204345, -1.75224494]])
//
#if TODO
given= 5 * np.random.random_sample((3, 2)) - 5;
expected=
"array([[-3.99149989, -0.52338984],\n" +
" [-2.99091858, -0.79479508],\n" +
" [-1.23204345, -1.75224494]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void randomTest()
{
// >>> np.random.random_sample()
// 0.47108547995356098
// >>> type(np.random.random_sample())
// <type 'float'>
// >>> np.random.random_sample((5,))
// array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
//
#if TODO
var given= np.random.random_sample();
var expected=
"0.47108547995356098";
Assert.AreEqual(expected, given.repr);
given= type(np.random.random_sample());
expected=
"<type 'float'>";
Assert.AreEqual(expected, given.repr);
given= np.random.random_sample((5,));
expected=
"array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])";
Assert.AreEqual(expected, given.repr);
#endif
// Three-by-two array of random numbers from [-5, 0):
// >>> 5 * np.random.random_sample((3, 2)) - 5
// array([[-3.99149989, -0.52338984],
// [-2.99091858, -0.79479508],
// [-1.23204345, -1.75224494]])
//
#if TODO
given= 5 * np.random.random_sample((3, 2)) - 5;
expected=
"array([[-3.99149989, -0.52338984],\n" +
" [-2.99091858, -0.79479508],\n" +
" [-1.23204345, -1.75224494]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void ranfTest()
{
// >>> np.random.random_sample()
// 0.47108547995356098
// >>> type(np.random.random_sample())
// <type 'float'>
// >>> np.random.random_sample((5,))
// array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
//
#if TODO
var given= np.random.random_sample();
var expected=
"0.47108547995356098";
Assert.AreEqual(expected, given.repr);
given= type(np.random.random_sample());
expected=
"<type 'float'>";
Assert.AreEqual(expected, given.repr);
given= np.random.random_sample((5,));
expected=
"array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])";
Assert.AreEqual(expected, given.repr);
#endif
// Three-by-two array of random numbers from [-5, 0):
// >>> 5 * np.random.random_sample((3, 2)) - 5
// array([[-3.99149989, -0.52338984],
// [-2.99091858, -0.79479508],
// [-1.23204345, -1.75224494]])
//
#if TODO
given= 5 * np.random.random_sample((3, 2)) - 5;
expected=
"array([[-3.99149989, -0.52338984],\n" +
" [-2.99091858, -0.79479508],\n" +
" [-1.23204345, -1.75224494]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void sampleTest()
{
// >>> np.random.random_sample()
// 0.47108547995356098
// >>> type(np.random.random_sample())
// <type 'float'>
// >>> np.random.random_sample((5,))
// array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
//
#if TODO
var given= np.random.random_sample();
var expected=
"0.47108547995356098";
Assert.AreEqual(expected, given.repr);
given= type(np.random.random_sample());
expected=
"<type 'float'>";
Assert.AreEqual(expected, given.repr);
given= np.random.random_sample((5,));
expected=
"array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])";
Assert.AreEqual(expected, given.repr);
#endif
// Three-by-two array of random numbers from [-5, 0):
// >>> 5 * np.random.random_sample((3, 2)) - 5
// array([[-3.99149989, -0.52338984],
// [-2.99091858, -0.79479508],
// [-1.23204345, -1.75224494]])
//
#if TODO
given= 5 * np.random.random_sample((3, 2)) - 5;
expected=
"array([[-3.99149989, -0.52338984],\n" +
" [-2.99091858, -0.79479508],\n" +
" [-1.23204345, -1.75224494]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void choiceTest()
{
// Generate a uniform random sample from np.arange(5) of size 3:
// >>> np.random.choice(5, 3)
// array([0, 3, 4])
// >>> #This is equivalent to np.random.randint(0,5,3)
//
#if TODO
var given= np.random.choice(5, 3);
var expected=
"array([0, 3, 4])";
Assert.AreEqual(expected, given.repr);
given= #This is equivalent to np.random.randint(0,5,3);
#endif
// Generate a non-uniform random sample from np.arange(5) of size 3:
// >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
// array([3, 3, 0])
//
#if TODO
given= np.random.choice(5, 3, p={0.1, 0, 0.3, 0.6, 0});
expected=
"array([3, 3, 0])";
Assert.AreEqual(expected, given.repr);
#endif
// Generate a uniform random sample from np.arange(5) of size 3 without
// replacement:
// >>> np.random.choice(5, 3, replace=False)
// array([3,1,0])
// >>> #This is equivalent to np.random.permutation(np.arange(5))[:3]
//
#if TODO
given= np.random.choice(5, 3, replace=False);
expected=
"array([3,1,0])";
Assert.AreEqual(expected, given.repr);
given= #This is equivalent to np.random.permutation(np.arange(5)){:3};
#endif
// Generate a non-uniform random sample from np.arange(5) of size
// 3 without replacement:
// >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
// array([2, 3, 0])
//
#if TODO
given= np.random.choice(5, 3, replace=False, p={0.1, 0, 0.3, 0.6, 0});
expected=
"array([2, 3, 0])";
Assert.AreEqual(expected, given.repr);
#endif
// Any of the above can be repeated with an arbitrary array-like
// instead of just integers. For instance:
// >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
// >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
// array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],
// dtype='|S11')
//
#if TODO
given= aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher'];
given= np.random.choice(aa_milne_arr, 5, p={0.5, 0.1, 0.1, 0.3});
expected=
"array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],\n" +
" dtype='|S11')";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void bytesTest()
{
// >>> np.random.bytes(10)
// ' eh\x85\x022SZ\xbf\xa4' #random
//
#if TODO
var given= np.random.bytes(10);
var expected=
"' eh\x85\x022SZ\xbf\xa4' #random";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void shuffleTest()
{
// >>> arr = np.arange(10)
// >>> np.random.shuffle(arr)
// >>> arr
// [1 7 5 2 9 4 3 6 0 8]
//
#if TODO
var given= arr = np.arange(10);
given= np.random.shuffle(arr);
given= arr;
var expected=
"[1 7 5 2 9 4 3 6 0 8]";
Assert.AreEqual(expected, given.repr);
#endif
// Multi-dimensional arrays are only shuffled along the first axis:
// >>> arr = np.arange(9).reshape((3, 3))
// >>> np.random.shuffle(arr)
// >>> arr
// array([[3, 4, 5],
// [6, 7, 8],
// [0, 1, 2]])
//
#if TODO
given= arr = np.arange(9).reshape((3, 3));
given= np.random.shuffle(arr);
given= arr;
expected=
"array([[3, 4, 5],\n" +
" [6, 7, 8],\n" +
" [0, 1, 2]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void permutationTest()
{
// >>> np.random.permutation(10)
// array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
//
#if TODO
var given= np.random.permutation(10);
var expected=
"array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])";
Assert.AreEqual(expected, given.repr);
#endif
// >>> np.random.permutation([1, 4, 9, 12, 15])
// array([15, 1, 9, 4, 12])
//
#if TODO
given= np.random.permutation({1, 4, 9, 12, 15});
expected=
"array([15, 1, 9, 4, 12])";
Assert.AreEqual(expected, given.repr);
#endif
// >>> arr = np.arange(9).reshape((3, 3))
// >>> np.random.permutation(arr)
// array([[6, 7, 8],
// [0, 1, 2],
// [3, 4, 5]])
//
#if TODO
given= arr = np.arange(9).reshape((3, 3));
given= np.random.permutation(arr);
expected=
"array([[6, 7, 8],\n" +
" [0, 1, 2],\n" +
" [3, 4, 5]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void binomialTest()
{
// Draw samples from the distribution:
// >>> n, p = 10, .5 # number of trials, probability of each trial
// >>> s = np.random.binomial(n, p, 1000)
// # result of flipping a coin 10 times, tested 1000 times.
//
#if TODO
var given= n, p = 10, .5 # number of trials, probability of each trial;
given= s = np.random.binomial(n, p, 1000);
// result of flipping a coin 10 times, tested 1000 times.
#endif
// A real world example. A company drills 9 wild-cat oil exploration
// wells, each with an estimated probability of success of 0.1. All nine
// wells fail. What is the probability of that happening?
// Let’s do 20,000 trials of the model, and count the number that
// generate zero positive results.
// >>> sum(np.random.binomial(9, 0.1, 20000) == 0)/20000.
// # answer = 0.38885, or 38%.
//
#if TODO
given= sum(np.random.binomial(9, 0.1, 20000) == 0)/20000.;
// answer = 0.38885, or 38%.
#endif
}
[TestMethod]
public void chisquareTest()
{
// >>> np.random.chisquare(2,4)
// array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272])
//
#if TODO
var given= np.random.chisquare(2,4);
var expected=
"array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void dirichletTest()
{
// Taking an example cited in Wikipedia, this distribution can be used if
// one wanted to cut strings (each of initial length 1.0) into K pieces
// with different lengths, where each piece had, on average, a designated
// average length, but allowing some variation in the relative sizes of
// the pieces.
// >>> s = np.random.dirichlet((10, 5, 3), 20).transpose()
//
#if TODO
var given= s = np.random.dirichlet((10, 5, 3), 20).transpose();
#endif
// >>> import matplotlib.pyplot as plt
// >>> plt.barh(range(20), s[0])
// >>> plt.barh(range(20), s[1], left=s[0], color='g')
// >>> plt.barh(range(20), s[2], left=s[0]+s[1], color='r')
// >>> plt.title("Lengths of Strings")
//
#if TODO
given= import matplotlib.pyplot as plt;
given= plt.barh(range(20), s[0]);
given= plt.barh(range(20), s[1], left=s[0], color='g');
given= plt.barh(range(20), s[2], left=s[0]+s[1], color='r');
given= plt.title("Lengths of Strings");
#endif
}
[TestMethod]
public void fTest()
{
// An example from Glantz[1], pp 47-40:
// Two groups, children of diabetics (25 people) and children from people
// without diabetes (25 controls). Fasting blood glucose was measured,
// case group had a mean value of 86.1, controls had a mean value of
// 82.2. Standard deviations were 2.09 and 2.49 respectively. Are these
// data consistent with the null hypothesis that the parents diabetic
// status does not affect their children’s blood glucose levels?
// Calculating the F statistic from the data gives a value of 36.01.
// Draw samples from the distribution:
// >>> dfnum = 1. # between group degrees of freedom
// >>> dfden = 48. # within groups degrees of freedom
// >>> s = np.random.f(dfnum, dfden, 1000)
//
#if TODO
var given= dfnum = 1. # between group degrees of freedom;
given= dfden = 48. # within groups degrees of freedom;
given= s = np.random.f(dfnum, dfden, 1000);
#endif
// The lower bound for the top 1% of the samples is :
// >>> sort(s)[-10]
// 7.61988120985
//
#if TODO
given= sort(s)[-10];
var expected=
"7.61988120985";
Assert.AreEqual(expected, given.repr);
#endif
// So there is about a 1% chance that the F statistic will exceed 7.62,
// the measured value is 36, so the null hypothesis is rejected at the 1%
// level.
}
[TestMethod]
public void gammaTest()
{
// Draw samples from the distribution:
// >>> shape, scale = 2., 2. # mean=4, std=2*sqrt(2)
// >>> s = np.random.gamma(shape, scale, 1000)
//
#if TODO
var given= shape, scale = 2., 2. # mean=4, std=2*sqrt(2);
given= s = np.random.gamma(shape, scale, 1000);
#endif
// Display the histogram of the samples, along with
// the probability density function:
// >>> import matplotlib.pyplot as plt
// >>> import scipy.special as sps
// >>> count, bins, ignored = plt.hist(s, 50, density=True)
// >>> y = bins**(shape-1)*(np.exp(-bins/scale) /
// ... (sps.gamma(shape)*scale**shape))
// >>> plt.plot(bins, y, linewidth=2, color='r')
// >>> plt.show()
//
#if TODO
given= import matplotlib.pyplot as plt;
given= import scipy.special as sps;
given= count, bins, ignored = plt.hist(s, 50, density=True);
given= y = bins**(shape-1)*(np.exp(-bins/scale) /;
var expected=
"... (sps.gamma(shape)*scale**shape))";
Assert.AreEqual(expected, given.repr);
given= plt.plot(bins, y, linewidth=2, color='r');
given= plt.show();
#endif
}
[TestMethod]
public void geometricTest()
{
// Draw ten thousand values from the geometric distribution,
// with the probability of an individual success equal to 0.35:
// >>> z = np.random.geometric(p=0.35, size=10000)
//
#if TODO
var given= z = np.random.geometric(p=0.35, size=10000);
#endif
// How many trials succeeded after a single run?
// >>> (z == 1).sum() / 10000.
// 0.34889999999999999 #random
//
#if TODO
given= (z == 1).sum() / 10000.;
var expected=
"0.34889999999999999 #random";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void gumbelTest()
{
// Draw samples from the distribution:
// >>> mu, beta = 0, 0.1 # location and scale
// >>> s = np.random.gumbel(mu, beta, 1000)
//
#if TODO
var given= mu, beta = 0, 0.1 # location and scale;
given= s = np.random.gumbel(mu, beta, 1000);
#endif
// Display the histogram of the samples, along with
// the probability density function:
// >>> import matplotlib.pyplot as plt
// >>> count, bins, ignored = plt.hist(s, 30, density=True)
// >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta)
// ... * np.exp( -np.exp( -(bins - mu) /beta) ),
// ... linewidth=2, color='r')
// >>> plt.show()
//
#if TODO
given= import matplotlib.pyplot as plt;
given= count, bins, ignored = plt.hist(s, 30, density=True);
given= plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta);
var expected=
"... * np.exp( -np.exp( -(bins - mu) /beta) ),\n" +
"... linewidth=2, color='r')";
Assert.AreEqual(expected, given.repr);
given= plt.show();
#endif
// Show how an extreme value distribution can arise from a Gaussian process
// and compare to a Gaussian:
// >>> means = []
// >>> maxima = []
// >>> for i in range(0,1000) :
// ... a = np.random.normal(mu, beta, 1000)
// ... means.append(a.mean())
// ... maxima.append(a.max())
// >>> count, bins, ignored = plt.hist(maxima, 30, density=True)
// >>> beta = np.std(maxima) * np.sqrt(6) / np.pi
// >>> mu = np.mean(maxima) - 0.57721*beta
// >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta)
// ... * np.exp(-np.exp(-(bins - mu)/beta)),
// ... linewidth=2, color='r')
// >>> plt.plot(bins, 1/(beta * np.sqrt(2 * np.pi))
// ... * np.exp(-(bins - mu)**2 / (2 * beta**2)),
// ... linewidth=2, color='g')
// >>> plt.show()
//
#if TODO
given= means = [];
given= maxima = [];
given= for i in range(0,1000) :;
expected=
"... a = np.random.normal(mu, beta, 1000)\n" +
"... means.append(a.mean())\n" +
"... maxima.append(a.max())";
Assert.AreEqual(expected, given.repr);
given= count, bins, ignored = plt.hist(maxima, 30, density=True);
given= beta = np.std(maxima) * np.sqrt(6) / np.pi;
given= mu = np.mean(maxima) - 0.57721*beta;
given= plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta);
expected=
"... * np.exp(-np.exp(-(bins - mu)/beta)),\n" +
"... linewidth=2, color='r')";
Assert.AreEqual(expected, given.repr);
given= plt.plot(bins, 1/(beta * np.sqrt(2 * np.pi));
expected=
"... * np.exp(-(bins - mu)**2 / (2 * beta**2)),\n" +
"... linewidth=2, color='g')";
Assert.AreEqual(expected, given.repr);
given= plt.show();
#endif
}
[TestMethod]
public void hypergeometricTest()
{
// Draw samples from the distribution:
// >>> ngood, nbad, nsamp = 100, 2, 10
// # number of good, number of bad, and number of samples
// >>> s = np.random.hypergeometric(ngood, nbad, nsamp, 1000)
// >>> from matplotlib.pyplot import hist
// >>> hist(s)
// # note that it is very unlikely to grab both bad items
//
#if TODO
var given= ngood, nbad, nsamp = 100, 2, 10;
// number of good, number of bad, and number of samples
given= s = np.random.hypergeometric(ngood, nbad, nsamp, 1000);
given= from matplotlib.pyplot import hist;
given= hist(s);
// note that it is very unlikely to grab both bad items
#endif
// Suppose you have an urn with 15 white and 15 black marbles.
// If you pull 15 marbles at random, how likely is it that
// 12 or more of them are one color?
// >>> s = np.random.hypergeometric(15, 15, 15, 100000)
// >>> sum(s>=12)/100000. + sum(s<=3)/100000.
// # answer = 0.003 ... pretty unlikely!
//
#if TODO
given= s = np.random.hypergeometric(15, 15, 15, 100000);
given= sum(s>=12)/100000. + sum(s<=3)/100000.;
// answer = 0.003 ... pretty unlikely!
#endif
}
[TestMethod]
public void laplaceTest()
{
// Draw samples from the distribution
// >>> loc, scale = 0., 1.
// >>> s = np.random.laplace(loc, scale, 1000)
//
#if TODO
var given= loc, scale = 0., 1.;
given= s = np.random.laplace(loc, scale, 1000);
#endif
// Display the histogram of the samples, along with
// the probability density function:
// >>> import matplotlib.pyplot as plt
// >>> count, bins, ignored = plt.hist(s, 30, density=True)
// >>> x = np.arange(-8., 8., .01)
// >>> pdf = np.exp(-abs(x-loc)/scale)/(2.*scale)
// >>> plt.plot(x, pdf)
//
#if TODO
given= import matplotlib.pyplot as plt;
given= count, bins, ignored = plt.hist(s, 30, density=True);
given= x = np.arange(-8., 8., .01);
given= pdf = np.exp(-abs(x-loc)/scale)/(2.*scale);
given= plt.plot(x, pdf);
#endif
// Plot Gaussian for comparison:
// >>> g = (1/(scale * np.sqrt(2 * np.pi)) *
// ... np.exp(-(x - loc)**2 / (2 * scale**2)))
// >>> plt.plot(x,g)
//
#if TODO
given= g = (1/(scale * np.sqrt(2 * np.pi)) *;
var expected=
"... np.exp(-(x - loc)**2 / (2 * scale**2)))";
Assert.AreEqual(expected, given.repr);
given= plt.plot(x,g);
#endif
}
[TestMethod]
public void logisticTest()
{
// Draw samples from the distribution:
// >>> loc, scale = 10, 1
// >>> s = np.random.logistic(loc, scale, 10000)
// >>> import matplotlib.pyplot as plt
// >>> count, bins, ignored = plt.hist(s, bins=50)
//
#if TODO
var given= loc, scale = 10, 1;
given= s = np.random.logistic(loc, scale, 10000);
given= import matplotlib.pyplot as plt;
given= count, bins, ignored = plt.hist(s, bins=50);
#endif
// # plot against distribution
// >>> def logist(x, loc, scale):
// ... return exp((loc-x)/scale)/(scale*(1+exp((loc-x)/scale))**2)
// >>> plt.plot(bins, logist(bins, loc, scale)*count.max()/\
// ... logist(bins, loc, scale).max())
// >>> plt.show()
//
#if TODO
given= def logist(x, loc, scale):;
var expected=
"... return exp((loc-x)/scale)/(scale*(1+exp((loc-x)/scale))**2)";
Assert.AreEqual(expected, given.repr);
given= plt.plot(bins, logist(bins, loc, scale)*count.max()/\;
expected=
"... logist(bins, loc, scale).max())";
Assert.AreEqual(expected, given.repr);
given= plt.show();
#endif
}
[TestMethod]
public void lognormalTest()
{
// Draw samples from the distribution:
// >>> mu, sigma = 3., 1. # mean and standard deviation
// >>> s = np.random.lognormal(mu, sigma, 1000)
//
#if TODO
var given= mu, sigma = 3., 1. # mean and standard deviation;
given= s = np.random.lognormal(mu, sigma, 1000);
#endif
// Display the histogram of the samples, along with
// the probability density function:
// >>> import matplotlib.pyplot as plt
// >>> count, bins, ignored = plt.hist(s, 100, density=True, align='mid')
//
#if TODO
given= import matplotlib.pyplot as plt;
given= count, bins, ignored = plt.hist(s, 100, density=True, align='mid');
#endif
// >>> x = np.linspace(min(bins), max(bins), 10000)
// >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))
// ... / (x * sigma * np.sqrt(2 * np.pi)))
//
#if TODO
given= x = np.linspace(min(bins), max(bins), 10000);
given= pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2));
var expected=
"... / (x * sigma * np.sqrt(2 * np.pi)))";
Assert.AreEqual(expected, given.repr);
#endif
// >>> plt.plot(x, pdf, linewidth=2, color='r')
// >>> plt.axis('tight')
// >>> plt.show()
//
#if TODO
given= plt.plot(x, pdf, linewidth=2, color='r');
given= plt.axis('tight');
given= plt.show();
#endif
// Demonstrate that taking the products of random samples from a uniform
// distribution can be fit well by a log-normal probability density
// function.
// >>> # Generate a thousand samples: each is the product of 100 random
// >>> # values, drawn from a normal distribution.
// >>> b = []
// >>> for i in range(1000):
// ... a = 10. + np.random.random(100)
// ... b.append(np.product(a))
//
#if TODO
given= # Generate a thousand samples: each is the product of 100 random;
given= # values, drawn from a normal distribution.;
given= b = [];
given= for i in range(1000):;
expected=
"... a = 10. + np.random.random(100)\n" +
"... b.append(np.product(a))";
Assert.AreEqual(expected, given.repr);
#endif
// >>> b = np.array(b) / np.min(b) # scale values to be positive
// >>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid')
// >>> sigma = np.std(np.log(b))
// >>> mu = np.mean(np.log(b))
//
#if TODO
given= b = np.array(b) / np.min(b) # scale values to be positive;
given= count, bins, ignored = plt.hist(b, 100, density=True, align='mid');
given= sigma = np.std(np.log(b));
given= mu = np.mean(np.log(b));
#endif
// >>> x = np.linspace(min(bins), max(bins), 10000)
// >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))
// ... / (x * sigma * np.sqrt(2 * np.pi)))
//