numo-random 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +1 -4
- data/ext/numo/random/randomext.hpp +84 -0
- data/lib/numo/random/generator.rb +249 -0
- data/lib/numo/random/version.rb +1 -1
- data/lib/numo/random.rb +1 -178
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18a6bedc77eb910e04462508a925d336fe2e5e8c29f9c159643adea1e0988a88
|
4
|
+
data.tar.gz: 728c52eb3e9f390cae89d149e6d4305228be5330e90fa39494ac0a1ffa546126
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd851012b81e6cec4d4dc7e37113ca9c6d5e0a56604107bba201a67cc4935f3618f887285a0c745adf3ad6c25493bbe6bd5fe4af49cac08eead625cb0c6afa78
|
7
|
+
data.tar.gz: 88f85185352601de72dbf8889095e2603c0b581750c11d683eddab26ab313f30c1b38577a1a3a099d2dc6834d29e0fbe2e53052e05918e5de00648adfcf9930d
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
|
-
- Sampling distribution.
|
3
2
|
- Bernoulli distribution.
|
4
3
|
- Poisson distribution.
|
5
4
|
|
5
|
+
## [0.2.0]
|
6
|
+
|
7
|
+
- Add discrete method.
|
8
|
+
|
9
|
+
### Breaking Changes
|
10
|
+
- Change to return array for all methods that fill array with random numbers in Generator class.
|
11
|
+
|
6
12
|
## [0.1.0]
|
7
13
|
- Initial release.
|
data/README.md
CHANGED
@@ -33,14 +33,11 @@ require 'numo/gnuplot'
|
|
33
33
|
|
34
34
|
require 'numo/random'
|
35
35
|
|
36
|
-
# Prepareing array to be filled with random numbers.
|
37
|
-
x = Numo::DFloat.new(5000, 2)
|
38
|
-
|
39
36
|
# Creating random number generator.
|
40
37
|
rng = Numo::Random::Generator.new(seed: 42)
|
41
38
|
|
42
39
|
# Generating random numbers with a normal distribution.
|
43
|
-
rng.normal(
|
40
|
+
x = rng.normal(shape: [5000, 2], loc: 0.0, scale: 1.0)
|
44
41
|
|
45
42
|
# Plotting the generated result.
|
46
43
|
Numo.gnuplot do
|
@@ -58,6 +58,7 @@ public:
|
|
58
58
|
rb_define_method(rb_cPCG64, "seed=", RUBY_METHOD_FUNC(_numo_random_pcg64_set_seed), 1);
|
59
59
|
rb_define_method(rb_cPCG64, "seed", RUBY_METHOD_FUNC(_numo_random_pcg64_get_seed), 0);
|
60
60
|
rb_define_method(rb_cPCG64, "random", RUBY_METHOD_FUNC(_numo_random_pcg64_random), 0);
|
61
|
+
rb_define_method(rb_cPCG64, "discrete", RUBY_METHOD_FUNC(_numo_random_pcg64_discrete), -1);
|
61
62
|
rb_define_method(rb_cPCG64, "uniform", RUBY_METHOD_FUNC(_numo_random_pcg64_uniform), -1);
|
62
63
|
rb_define_method(rb_cPCG64, "cauchy", RUBY_METHOD_FUNC(_numo_random_pcg64_cauchy), -1);
|
63
64
|
rb_define_method(rb_cPCG64, "chisquare", RUBY_METHOD_FUNC(_numo_random_pcg64_chisquare), -1);
|
@@ -143,6 +144,89 @@ private:
|
|
143
144
|
}
|
144
145
|
}
|
145
146
|
|
147
|
+
// #discrete
|
148
|
+
|
149
|
+
template<typename T, typename P> static void _rand_discrete(VALUE& self, VALUE& x, const std::vector<P>& weight) {
|
150
|
+
pcg64* ptr = get_pcg64(self);
|
151
|
+
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
|
152
|
+
std::discrete_distribution<T> discrete_dist(weight.begin(), weight.end());
|
153
|
+
ndfunc_t ndf = { _iter_rand<std::discrete_distribution<T>, T>, FULL_LOOP, 1, 0, ain, 0 };
|
154
|
+
rand_opt_t<std::discrete_distribution<T>> opt = { discrete_dist, ptr };
|
155
|
+
na_ndloop3(&ndf, &opt, 1, x);
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE _numo_random_pcg64_discrete(int argc, VALUE* argv, VALUE self) {
|
159
|
+
VALUE x = Qnil;
|
160
|
+
VALUE kw_args = Qnil;
|
161
|
+
ID kw_table[1] = { rb_intern("weight") };
|
162
|
+
VALUE kw_values[1] = { Qundef };
|
163
|
+
rb_scan_args(argc, argv, "1:", &x, &kw_args);
|
164
|
+
rb_get_kwargs(kw_args, kw_table, 1, 0, kw_values);
|
165
|
+
|
166
|
+
VALUE klass = rb_obj_class(x);
|
167
|
+
if (klass != numo_cInt8 && klass != numo_cInt16 && klass != numo_cInt32 && klass != numo_cInt64
|
168
|
+
&& klass != numo_cUInt8 && klass != numo_cUInt16 && klass != numo_cUInt32 && klass != numo_cUInt64)
|
169
|
+
rb_raise(rb_eTypeError, "invalid NArray class, it must be integer typed array");
|
170
|
+
|
171
|
+
VALUE w = kw_values[0];
|
172
|
+
VALUE w_klass = rb_obj_class(w);
|
173
|
+
if (w_klass != numo_cSFloat && w_klass != numo_cDFloat) rb_raise(rb_eTypeError, "weight must be Numo::DFloat or Numo::SFloat");
|
174
|
+
|
175
|
+
if (!RTEST(nary_check_contiguous(w))) w = nary_dup(w);
|
176
|
+
narray_t* w_nary;
|
177
|
+
GetNArray(w, w_nary);
|
178
|
+
if (NA_NDIM(w_nary) != 1) rb_raise(rb_eArgError, "weight must be 1-dimensional array");
|
179
|
+
|
180
|
+
const size_t w_len = NA_SHAPE(w_nary)[0];
|
181
|
+
if (w_len < 1) rb_raise(rb_eArgError, "length of weight must be > 0");
|
182
|
+
|
183
|
+
if (w_klass == numo_cSFloat) {
|
184
|
+
const float* w_ptr = (float*)na_get_pointer_for_read(w);
|
185
|
+
std::vector<float> w_vec(w_ptr, w_ptr + w_len);
|
186
|
+
if (klass == numo_cInt8) {
|
187
|
+
_rand_discrete<int8_t, float>(self, x, w_vec);
|
188
|
+
} else if (klass == numo_cInt16) {
|
189
|
+
_rand_discrete<int16_t, float>(self, x, w_vec);
|
190
|
+
} else if (klass == numo_cInt32) {
|
191
|
+
_rand_discrete<int32_t, float>(self, x, w_vec);
|
192
|
+
} else if (klass == numo_cInt64) {
|
193
|
+
_rand_discrete<int64_t, float>(self, x, w_vec);
|
194
|
+
} else if (klass == numo_cUInt8) {
|
195
|
+
_rand_discrete<uint8_t, float>(self, x, w_vec);
|
196
|
+
} else if (klass == numo_cUInt16) {
|
197
|
+
_rand_discrete<uint16_t, float>(self, x, w_vec);
|
198
|
+
} else if (klass == numo_cUInt32) {
|
199
|
+
_rand_discrete<uint32_t, float>(self, x, w_vec);
|
200
|
+
} else if (klass == numo_cUInt64) {
|
201
|
+
_rand_discrete<uint64_t, float>(self, x, w_vec);
|
202
|
+
}
|
203
|
+
} else {
|
204
|
+
const double* w_ptr = (double*)na_get_pointer_for_read(w);
|
205
|
+
std::vector<double> w_vec(w_ptr, w_ptr + w_len);
|
206
|
+
if (klass == numo_cInt8) {
|
207
|
+
_rand_discrete<int8_t, double>(self, x, w_vec);
|
208
|
+
} else if (klass == numo_cInt16) {
|
209
|
+
_rand_discrete<int16_t, double>(self, x, w_vec);
|
210
|
+
} else if (klass == numo_cInt32) {
|
211
|
+
_rand_discrete<int32_t, double>(self, x, w_vec);
|
212
|
+
} else if (klass == numo_cInt64) {
|
213
|
+
_rand_discrete<int64_t, double>(self, x, w_vec);
|
214
|
+
} else if (klass == numo_cUInt8) {
|
215
|
+
_rand_discrete<uint8_t, double>(self, x, w_vec);
|
216
|
+
} else if (klass == numo_cUInt16) {
|
217
|
+
_rand_discrete<uint16_t, double>(self, x, w_vec);
|
218
|
+
} else if (klass == numo_cUInt32) {
|
219
|
+
_rand_discrete<uint32_t, double>(self, x, w_vec);
|
220
|
+
} else if (klass == numo_cUInt64) {
|
221
|
+
_rand_discrete<uint64_t, double>(self, x, w_vec);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
RB_GC_GUARD(w);
|
226
|
+
RB_GC_GUARD(x);
|
227
|
+
return Qnil;
|
228
|
+
}
|
229
|
+
|
146
230
|
// #uniform
|
147
231
|
|
148
232
|
template<typename T> static void _rand_uniform(VALUE& self, VALUE& x, const double& low, const double& high) {
|
@@ -0,0 +1,249 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Ruby/Numo (NUmerical MOdules)
|
4
|
+
module Numo
|
5
|
+
# Numo::Random provides random number generation with several distributions for Numo::NArray.
|
6
|
+
module Random
|
7
|
+
# Generator is a class that generates random number with several distributions.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require 'numo/random'
|
11
|
+
#
|
12
|
+
# rng = Numo::Random::Generator.new(seed: 496)
|
13
|
+
# x = rng.uniform(shape: [2, 5], low: -1, high: 2)
|
14
|
+
#
|
15
|
+
# p x
|
16
|
+
# # Numo::DFloat#shape=[2,5]
|
17
|
+
# # [[1.90546, -0.543299, 0.673332, 0.759583, -0.40945],
|
18
|
+
# # [0.334635, -0.0558342, 1.28115, 1.93644, -0.0689543]]
|
19
|
+
class Generator
|
20
|
+
# Returns random number generation algorithm.
|
21
|
+
# @return [String]
|
22
|
+
attr_accessor :algorithm
|
23
|
+
|
24
|
+
# Creates a new random number generator.
|
25
|
+
#
|
26
|
+
# @param seed [Integer] random seed used to initialize the random number generator.
|
27
|
+
# @param algorithm [String] random number generation algorithm.
|
28
|
+
def initialize(seed: nil, algorithm: 'pcg64') # rubocop:disable Lint/UnusedMethodArgument
|
29
|
+
@algorithm = 'pcg64'
|
30
|
+
@rng = PCG64.new(seed: seed)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the seed of random number generator.
|
34
|
+
#
|
35
|
+
# @return [Integer]
|
36
|
+
def seed
|
37
|
+
rng.seed
|
38
|
+
end
|
39
|
+
|
40
|
+
# Sets the seed of random number generator.
|
41
|
+
#
|
42
|
+
# @param val [Integer] random seed.
|
43
|
+
def seed=(val)
|
44
|
+
rng.seed = val
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns random number with uniform distribution in the half-open interval [0, 1).
|
48
|
+
#
|
49
|
+
# @example
|
50
|
+
# require 'numo/random'
|
51
|
+
#
|
52
|
+
# rng = Numo::Random::Generator.new
|
53
|
+
# v = rng.random
|
54
|
+
#
|
55
|
+
# @return [Float]
|
56
|
+
def random
|
57
|
+
rng.random
|
58
|
+
end
|
59
|
+
|
60
|
+
# Generates array consists of random integer values in the interval [0, n).
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# require 'numo/random'
|
64
|
+
#
|
65
|
+
# rng = Numo::Random::Generator.new(seed: 42)
|
66
|
+
# w = Numo::DFloat[0.1, 0.6, 0.2]
|
67
|
+
# x = rng.discrete(shape: [3, 10], weight: w)
|
68
|
+
#
|
69
|
+
# p x
|
70
|
+
#
|
71
|
+
# # Numo::Int32#shape=[3,10]
|
72
|
+
# # [[1, 1, 1, 1, 1, 1, 1, 1, 2, 1],
|
73
|
+
# # [0, 1, 0, 1, 1, 0, 1, 1, 2, 1],
|
74
|
+
# # [2, 1, 1, 1, 1, 2, 2, 1, 1, 2]]
|
75
|
+
#
|
76
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
77
|
+
# @param weight [Numo::DFloat | Numo::SFloat] (shape: [n]) list of probabilities of each integer being generated.
|
78
|
+
# @param dtype [Symbol] data type of random array.
|
79
|
+
# @return [Numo::IntX | Numo::UIntX]
|
80
|
+
def discrete(shape:, weight:, dtype: :int32)
|
81
|
+
x = klass(dtype).new(shape)
|
82
|
+
rng.discrete(x, weight: weight)
|
83
|
+
x
|
84
|
+
end
|
85
|
+
|
86
|
+
# Generates array consists of uniformly distributed random values in the interval [low, high).
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# require 'numo/random'
|
90
|
+
#
|
91
|
+
# rng = Numo::Random::Generator.new
|
92
|
+
# x = rng.uniform(shape: 100, low: -1.5, high: 1.5)
|
93
|
+
#
|
94
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
95
|
+
# @param low [Float] lower boundary.
|
96
|
+
# @param high [Float] upper boundary.
|
97
|
+
# @param dtype [Symbol] data type of random array.
|
98
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
99
|
+
def uniform(shape:, low: 0.0, high: 1.0, dtype: :float64)
|
100
|
+
x = klass(dtype).new(shape)
|
101
|
+
rng.uniform(x, low: low, high: high)
|
102
|
+
x
|
103
|
+
end
|
104
|
+
|
105
|
+
# Generates array consists of random values according to the Cauchy (Lorentz) distribution.
|
106
|
+
#
|
107
|
+
# @example
|
108
|
+
# require 'numo/random'
|
109
|
+
#
|
110
|
+
# rng = Numo::Random::Generator.new
|
111
|
+
# x = rng.cauchy(shape: 100, loc: 0.0, scale: 1.0)
|
112
|
+
#
|
113
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
114
|
+
# @param loc [Float] location parameter.
|
115
|
+
# @param scale [Float] scale parameter.
|
116
|
+
# @param dtype [Symbol] data type of random array.
|
117
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
118
|
+
def cauchy(shape:, loc: 0.0, scale: 1.0, dtype: :float64)
|
119
|
+
x = klass(dtype).new(shape)
|
120
|
+
rng.cauchy(x, loc: loc, scale: scale)
|
121
|
+
x
|
122
|
+
end
|
123
|
+
|
124
|
+
# Generates array consists of random values according to the Chi-squared distribution.
|
125
|
+
#
|
126
|
+
# @example
|
127
|
+
# require 'numo/random'
|
128
|
+
#
|
129
|
+
# rng = Numo::Random::Generator.new
|
130
|
+
# x = rng.chisquare(shape: 100, df: 2.0)
|
131
|
+
#
|
132
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
133
|
+
# @param df [Float] degrees of freedom, must be > 0.
|
134
|
+
# @param dtype [Symbol] data type of random array.
|
135
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
136
|
+
def chisquare(shape:, df:, dtype: :float64)
|
137
|
+
x = klass(dtype).new(shape)
|
138
|
+
rng.chisquare(x, df: df)
|
139
|
+
x
|
140
|
+
end
|
141
|
+
|
142
|
+
# Generates array consists of random values according to the F-distribution.
|
143
|
+
#
|
144
|
+
# @example
|
145
|
+
# require 'numo/random'
|
146
|
+
#
|
147
|
+
# rng = Numo::Random::Generator.new
|
148
|
+
# x = rng.f(shape: 100, dfnum: 2.0, dfden: 4.0)
|
149
|
+
#
|
150
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
151
|
+
# @param dfnum [Float] degrees of freedom in numerator, must be > 0.
|
152
|
+
# @param dfden [Float] degrees of freedom in denominator, must be > 0.
|
153
|
+
# @param dtype [Symbol] data type of random array.
|
154
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
155
|
+
def f(shape:, dfnum:, dfden:, dtype: :float64)
|
156
|
+
x = klass(dtype).new(shape)
|
157
|
+
rng.f(x, dfnum: dfnum, dfden: dfden)
|
158
|
+
x
|
159
|
+
end
|
160
|
+
|
161
|
+
# Generates array consists of random values according to a normal (Gaussian) distribution.
|
162
|
+
#
|
163
|
+
# @example
|
164
|
+
# require 'numo/random'
|
165
|
+
#
|
166
|
+
# rng = Numo::Random::Generator.new
|
167
|
+
# x = rng.normal(shape: 100, loc: 0.0, scale: 1.0)
|
168
|
+
#
|
169
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
170
|
+
# @param loc [Float] location parameter.
|
171
|
+
# @param scale [Float] scale parameter.
|
172
|
+
# @param dtype [Symbol] data type of random array.
|
173
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
174
|
+
def normal(shape:, loc: 0.0, scale: 1.0, dtype: :float64)
|
175
|
+
x = klass(dtype).new(shape)
|
176
|
+
rng.normal(x, loc: loc, scale: scale)
|
177
|
+
x
|
178
|
+
end
|
179
|
+
|
180
|
+
# Generates array consists of random values according to a log-normal distribution.
|
181
|
+
#
|
182
|
+
# @example
|
183
|
+
# require 'numo/random'
|
184
|
+
#
|
185
|
+
# rng = Numo::Random::Generator.new
|
186
|
+
# x = rng.lognormal(shape: 100, mean: 0.0, sigma: 1.0)
|
187
|
+
#
|
188
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
189
|
+
# @param mean [Float] mean of normal distribution.
|
190
|
+
# @param sigma [Float] standard deviation of normal distribution.
|
191
|
+
# @param dtype [Symbol] data type of random array.
|
192
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
193
|
+
def lognormal(shape:, mean: 0.0, sigma: 1.0, dtype: :float64)
|
194
|
+
x = klass(dtype).new(shape)
|
195
|
+
rng.lognormal(x, mean: mean, sigma: sigma)
|
196
|
+
x
|
197
|
+
end
|
198
|
+
|
199
|
+
# Generates array consists of random values according to the Student's t-distribution.
|
200
|
+
#
|
201
|
+
# @example
|
202
|
+
# require 'numo/random'
|
203
|
+
#
|
204
|
+
# rng = Numo::Random::Generator.new
|
205
|
+
# x = rng.standard_t(shape: 100, df: 8.0)
|
206
|
+
#
|
207
|
+
# @param shape [Integer | Array<Integer>] size of random array.
|
208
|
+
# @param df [Float] degrees of freedom, must be > 0.
|
209
|
+
# @param dtype [Symbol] data type of random array.
|
210
|
+
# @return [Numo::DFloat | Numo::SFloat]
|
211
|
+
def standard_t(shape:, df:, dtype: :float64)
|
212
|
+
x = klass(dtype).new(shape)
|
213
|
+
rng.standard_t(x, df: df)
|
214
|
+
x
|
215
|
+
end
|
216
|
+
|
217
|
+
private
|
218
|
+
|
219
|
+
attr_reader :rng
|
220
|
+
|
221
|
+
def klass(dtype) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
|
222
|
+
case dtype.to_sym
|
223
|
+
when :int8
|
224
|
+
Numo::Int8
|
225
|
+
when :int16
|
226
|
+
Numo::Int16
|
227
|
+
when :int32
|
228
|
+
Numo::Int32
|
229
|
+
when :int64
|
230
|
+
Numo::Int64
|
231
|
+
when :uint8
|
232
|
+
Numo::UInt8
|
233
|
+
when :uint16
|
234
|
+
Numo::UInt16
|
235
|
+
when :uint32
|
236
|
+
Numo::UInt32
|
237
|
+
when :uint64
|
238
|
+
Numo::UInt64
|
239
|
+
when :float32, :sfloat
|
240
|
+
Numo::SFloat
|
241
|
+
when :float64, :dfloat
|
242
|
+
Numo::DFloat
|
243
|
+
else
|
244
|
+
raise ArgumentError, "wrong dtype is given: #{dtype}"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
data/lib/numo/random/version.rb
CHANGED
data/lib/numo/random.rb
CHANGED
@@ -4,181 +4,4 @@ require 'numo/narray'
|
|
4
4
|
|
5
5
|
require_relative 'random/version'
|
6
6
|
require_relative 'random/randomext'
|
7
|
-
|
8
|
-
# Ruby/Numo (NUmerical MOdules)
|
9
|
-
module Numo
|
10
|
-
# Numo::Random provides random number generation with several distributions for Numo::NArray.
|
11
|
-
module Random
|
12
|
-
# Generator is a class that generates random number with several distributions.
|
13
|
-
#
|
14
|
-
# @example
|
15
|
-
# require 'numo/random'
|
16
|
-
#
|
17
|
-
# x = Numo::DFloat.new(100)
|
18
|
-
#
|
19
|
-
# rng = Numo::Random::Generator.new
|
20
|
-
# rng.uniform(x, low: -1, high: 2)
|
21
|
-
class Generator
|
22
|
-
# Returns random number generation algorithm.
|
23
|
-
# @return [String]
|
24
|
-
attr_accessor :algorithm
|
25
|
-
|
26
|
-
# Creates a new random number generator.
|
27
|
-
#
|
28
|
-
# @param seed [Integer] random seed used to initialize the random number generator.
|
29
|
-
# @param algorithm [String] random number generation algorithm.
|
30
|
-
def initialize(seed: nil, algorithm: 'pcg64') # rubocop:disable Lint/UnusedMethodArgument
|
31
|
-
@algorithm = 'pcg64'
|
32
|
-
@rng = PCG64.new(seed: seed)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Returns the seed of random number generator.
|
36
|
-
#
|
37
|
-
# @return [Integer]
|
38
|
-
def seed
|
39
|
-
rng.seed
|
40
|
-
end
|
41
|
-
|
42
|
-
# Sets the seed of random number generator.
|
43
|
-
#
|
44
|
-
# @param val [Integer] random seed.
|
45
|
-
def seed=(val)
|
46
|
-
rng.seed = val
|
47
|
-
end
|
48
|
-
|
49
|
-
# Returns random number with uniform distribution in the half-open interval [0, 1).
|
50
|
-
#
|
51
|
-
# @example
|
52
|
-
# require 'numo/random'
|
53
|
-
#
|
54
|
-
# rng = Numo::Random::Generator.new
|
55
|
-
# v = rng.random
|
56
|
-
#
|
57
|
-
# @return [Float]
|
58
|
-
def random
|
59
|
-
rng.random
|
60
|
-
end
|
61
|
-
|
62
|
-
# Fills given array with uniformly distributed random values in the interval [low, high).
|
63
|
-
#
|
64
|
-
# @example
|
65
|
-
# require 'numo/random'
|
66
|
-
#
|
67
|
-
# x = Numo::DFloat.new(100)
|
68
|
-
#
|
69
|
-
# rng = Numo::Random::Generator.new
|
70
|
-
# rng.uniform(x, low: -1.5, high: 1.5)
|
71
|
-
#
|
72
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
73
|
-
# @param low [Float] lower boundary.
|
74
|
-
# @param high [Float] upper boundary.
|
75
|
-
def uniform(x, low: 0.0, high: 1.0)
|
76
|
-
rng.uniform(x, low: low, high: high)
|
77
|
-
end
|
78
|
-
|
79
|
-
# Fills given array with random values according to the Cauchy (Lorentz) distribution.
|
80
|
-
#
|
81
|
-
# @example
|
82
|
-
# require 'numo/random'
|
83
|
-
#
|
84
|
-
# x = Numo::DFloat.new(100)
|
85
|
-
#
|
86
|
-
# rng = Numo::Random::Generator.new
|
87
|
-
# rng.cauchy(x, loc: 0.0, scale: 1.0)
|
88
|
-
#
|
89
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
90
|
-
# @param loc [Float] location parameter.
|
91
|
-
# @param scale [Float] scale parameter.
|
92
|
-
def cauchy(x, loc: 0.0, scale: 1.0)
|
93
|
-
rng.cauchy(x, loc: loc, scale: scale)
|
94
|
-
end
|
95
|
-
|
96
|
-
# Fills given array with random values according to the Chi-squared distribution.
|
97
|
-
#
|
98
|
-
# @example
|
99
|
-
# require 'numo/random'
|
100
|
-
#
|
101
|
-
# x = Numo::DFloat.new(100)
|
102
|
-
#
|
103
|
-
# rng = Numo::Random::Generator.new
|
104
|
-
# rng.chisquare(x, df: 2.0)
|
105
|
-
#
|
106
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
107
|
-
# @param df [Float] degrees of freedom, must be > 0.
|
108
|
-
def chisquare(x, df:)
|
109
|
-
rng.chisquare(x, df: df)
|
110
|
-
end
|
111
|
-
|
112
|
-
# Fills given array with random values according to the F-distribution.
|
113
|
-
#
|
114
|
-
# @example
|
115
|
-
# require 'numo/random'
|
116
|
-
#
|
117
|
-
# x = Numo::DFloat.new(100)
|
118
|
-
#
|
119
|
-
# rng = Numo::Random::Generator.new
|
120
|
-
# rng.f(x, dfnum: 2.0, dfden: 4.0)
|
121
|
-
#
|
122
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
123
|
-
# @param dfnum [Float] degrees of freedom in numerator, must be > 0.
|
124
|
-
# @param dfden [Float] degrees of freedom in denominator, must be > 0.
|
125
|
-
def f(x, dfnum:, dfden:)
|
126
|
-
rng.f(x, dfnum: dfnum, dfden: dfden)
|
127
|
-
end
|
128
|
-
|
129
|
-
# Fills given array with random values according to a normal (Gaussian) distribution.
|
130
|
-
#
|
131
|
-
# @example
|
132
|
-
# require 'numo/random'
|
133
|
-
#
|
134
|
-
# x = Numo::DFloat.new(100)
|
135
|
-
#
|
136
|
-
# rng = Numo::Random::Generator.new
|
137
|
-
# rng.normal(x, loc: 0.0, scale: 1.0)
|
138
|
-
#
|
139
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
140
|
-
# @param loc [Float] location parameter.
|
141
|
-
# @param scale [Float] scale parameter.
|
142
|
-
def normal(x, loc: 0.0, scale: 1.0)
|
143
|
-
rng.normal(x, loc: loc, scale: scale)
|
144
|
-
end
|
145
|
-
|
146
|
-
# Fills given array with random values according to a log-normal distribution.
|
147
|
-
#
|
148
|
-
# @example
|
149
|
-
# require 'numo/random'
|
150
|
-
#
|
151
|
-
# x = Numo::DFloat.new(100)
|
152
|
-
#
|
153
|
-
# rng = Numo::Random::Generator.new
|
154
|
-
# rng.lognormal(x, mean: 0.0, sigma: 1.0)
|
155
|
-
#
|
156
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
157
|
-
# @param mean [Float] mean of normal distribution.
|
158
|
-
# @param sigma [Float] standard deviation of normal distribution.
|
159
|
-
def lognormal(x, mean: 0.0, sigma: 1.0)
|
160
|
-
rng.lognormal(x, mean: mean, sigma: sigma)
|
161
|
-
end
|
162
|
-
|
163
|
-
# Fills given array with random values according to the Student's t-distribution.
|
164
|
-
#
|
165
|
-
# @example
|
166
|
-
# require 'numo/random'
|
167
|
-
#
|
168
|
-
# x = Numo::DFloat.new(100)
|
169
|
-
#
|
170
|
-
# rng = Numo::Random::Generator.new
|
171
|
-
# rng.standard_t(x, df: 8.0)
|
172
|
-
#
|
173
|
-
# @param x [Numo::DFloat | Numo::SFloat] array filled with random values.
|
174
|
-
# @param df [Float] degrees of freedom, must be > 0.
|
175
|
-
def standard_t(x, df:)
|
176
|
-
rng.standard_t(x, df: df)
|
177
|
-
end
|
178
|
-
|
179
|
-
private
|
180
|
-
|
181
|
-
attr_reader :rng
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
7
|
+
require_relative 'random/generator'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numo-random
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-narray
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- ext/numo/random/src/pcg_random.hpp
|
46
46
|
- ext/numo/random/src/pcg_uint128.hpp
|
47
47
|
- lib/numo/random.rb
|
48
|
+
- lib/numo/random/generator.rb
|
48
49
|
- lib/numo/random/version.rb
|
49
50
|
homepage: https://github.com/yoshoku/numo-random
|
50
51
|
licenses:
|
@@ -53,7 +54,7 @@ metadata:
|
|
53
54
|
homepage_uri: https://github.com/yoshoku/numo-random
|
54
55
|
source_code_uri: https://github.com/yoshoku/numo-random
|
55
56
|
changelog_uri: https://github.com/yoshoku/numo-random/blob/main/CHANGELOG.md
|
56
|
-
documentation_uri: https://
|
57
|
+
documentation_uri: https://yoshoku.github.io/numo-random/doc/
|
57
58
|
rubygems_mfa_required: 'true'
|
58
59
|
post_install_message:
|
59
60
|
rdoc_options: []
|