gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
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
"use strict";
/**
 * @license
 * Copyright 2017 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */
Object.defineProperty(exports, "__esModule", { value: true });
var test_util_1 = require("../test_util");
var rand_1 = require("./rand");
var rand_util_1 = require("./rand_util");
function isFloat(n) {
    return Number(n) === n && n % 1 !== 0;
}
describe('MPRandGauss', function () {
    var EPSILON = 0.05;
    var SEED = 2002;
    it('should default to float32 numbers', function () {
        var rand = new rand_1.MPRandGauss(0, 1.5);
        expect(isFloat(rand.nextValue())).toBe(true);
    });
    it('should handle a mean/stdv of float32 numbers', function () {
        var rand = new rand_1.MPRandGauss(0, 1.5, 'float32', false /* truncated */, SEED);
        var values = [];
        var size = 10000;
        for (var i = 0; i < size; i++) {
            values.push(rand.nextValue());
        }
        rand_util_1.expectArrayInMeanStdRange(values, 0, 1.5, EPSILON);
        rand_util_1.jarqueBeraNormalityTest(values);
    });
    it('should handle int32 numbers', function () {
        var rand = new rand_1.MPRandGauss(0, 1, 'int32');
        expect(isFloat(rand.nextValue())).toBe(false);
    });
    it('should handle a mean/stdv of int32 numbers', function () {
        var rand = new rand_1.MPRandGauss(0, 2, 'int32', false /* truncated */, SEED);
        var values = [];
        var size = 10000;
        for (var i = 0; i < size; i++) {
            values.push(rand.nextValue());
        }
        rand_util_1.expectArrayInMeanStdRange(values, 0, 2, EPSILON);
        rand_util_1.jarqueBeraNormalityTest(values);
    });
    it('Should not have a more than 2x std-d from mean for truncated values', function () {
        var stdv = 1.5;
        var rand = new rand_1.MPRandGauss(0, stdv, 'float32', true /* truncated */);
        for (var i = 0; i < 1000; i++) {
            expect(Math.abs(rand.nextValue())).toBeLessThan(stdv * 2);
        }
    });
});
describe('RandGamma', function () {
    var SEED = 2002;
    it('should default to float32 numbers', function () {
        var rand = new rand_1.RandGamma(2, 2, 'float32');
        expect(isFloat(rand.nextValue())).toBe(true);
    });
    it('should handle an alpha/beta of float32 numbers', function () {
        var rand = new rand_1.RandGamma(2, 2, 'float32', SEED);
        var values = [];
        var size = 10000;
        for (var i = 0; i < size; i++) {
            values.push(rand.nextValue());
        }
        test_util_1.expectValuesInRange(values, 0, 30);
    });
    it('should handle int32 numbers', function () {
        var rand = new rand_1.RandGamma(2, 2, 'int32');
        expect(isFloat(rand.nextValue())).toBe(false);
    });
    it('should handle an alpha/beta of int32 numbers', function () {
        var rand = new rand_1.RandGamma(2, 2, 'int32', SEED);
        var values = [];
        var size = 10000;
        for (var i = 0; i < size; i++) {
            values.push(rand.nextValue());
        }
        test_util_1.expectValuesInRange(values, 0, 30);
    });
});
describe('UniformRandom', function () {
    it('float32, no seed', function () {
        var min = 0.2;
        var max = 0.24;
        var dtype = 'float32';
        var xs = [];
        for (var i = 0; i < 10; ++i) {
            var rand = new rand_1.UniformRandom(min, max, dtype);
            var x = rand.nextValue();
            xs.push(x);
        }
        expect(Math.min.apply(Math, xs)).toBeGreaterThanOrEqual(min);
        expect(Math.max.apply(Math, xs)).toBeLessThan(max);
    });
    it('int32, no seed', function () {
        var min = 13;
        var max = 37;
        var dtype = 'int32';
        var xs = [];
        for (var i = 0; i < 10; ++i) {
            var rand = new rand_1.UniformRandom(min, max, dtype);
            var x = rand.nextValue();
            expect(Number.isInteger(x)).toEqual(true);
            xs.push(x);
        }
        expect(Math.min.apply(Math, xs)).toBeGreaterThanOrEqual(min);
        expect(Math.max.apply(Math, xs)).toBeLessThanOrEqual(max);
    });
    it('seed is number', function () {
        var min = -1.2;
        var max = -0.4;
        var dtype = 'float32';
        var seed = 1337;
        var xs = [];
        for (var i = 0; i < 10; ++i) {
            var rand = new rand_1.UniformRandom(min, max, dtype, seed);
            var x = rand.nextValue();
            expect(x).toBeGreaterThanOrEqual(min);
            expect(x).toBeLessThan(max);
            xs.push(x);
        }
        // Assert deterministic results.
        expect(Math.min.apply(Math, xs)).toEqual(Math.max.apply(Math, xs));
    });
    it('seed === null', function () {
        var min = 0;
        var max = 1;
        var dtype = 'float32';
        var seed = null;
        var rand = new rand_1.UniformRandom(min, max, dtype, seed);
        var x = rand.nextValue();
        expect(x).toBeGreaterThanOrEqual(0);
        expect(x).toBeLessThan(1);
    });
    it('seed === undefined', function () {
        var min = 0;
        var max = 1;
        var dtype = 'float32';
        var seed = undefined;
        var rand = new rand_1.UniformRandom(min, max, dtype, seed);
        var x = rand.nextValue();
        expect(x).toBeGreaterThanOrEqual(0);
        expect(x).toBeLessThan(1);
    });
});
//# sourceMappingURL=rand_test.js.map