Skip to content

Commit 3100739

Browse files
committed
Add pkg to create an array containing pseudorandom numbers drawn from a beta distribution
1 parent 7be0965 commit 3100739

21 files changed

+4029
-0
lines changed
Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Beta Random Numbers
22+
23+
> Create an array containing pseudorandom numbers drawn from a [beta][@stdlib/random/base/beta] distribution.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var beta = require( '@stdlib/random/array/beta' );
31+
```
32+
33+
#### beta( len, alpha, beta\[, options] )
34+
35+
Returns an array containing pseudorandom numbers drawn from a [beta][@stdlib/random/base/beta] distribution.
36+
37+
```javascript
38+
var out = beta( 10, 2.0, 5.0 );
39+
// returns <Float64Array>
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **len**: output array length.
45+
- **alpha**: first shape parameter.
46+
- **beta**: second shape parameter.
47+
- **options**: function options.
48+
49+
The function accepts the following `options`:
50+
51+
- **dtype**: output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic". Default: `'float64'`.
52+
53+
By default, the function returns a [`Float64Array`][@stdlib/array/float64]. To return an array having a different data type, set the `dtype` option.
54+
55+
```javascript
56+
var opts = {
57+
'dtype': 'generic'
58+
};
59+
60+
var out = beta( 10, 2.0, 5.0, opts );
61+
// returns [...]
62+
```
63+
64+
#### beta.factory( \[alpha, beta, ]\[options] )
65+
66+
Returns a function for creating arrays containing pseudorandom numbers drawn from a [beta][@stdlib/random/base/beta] distribution.
67+
68+
```javascript
69+
var random = beta.factory();
70+
71+
var out = random( 10, 2.0, 5.0 );
72+
// returns <Float64Array>
73+
74+
var len = out.length;
75+
// returns 10
76+
```
77+
78+
If provided `alpha` and `beta`, the returned generator returns random variates from the specified distribution.
79+
80+
```javascript
81+
var random = beta.factory( 2.0, 5.0 );
82+
83+
var out = random( 10 );
84+
// returns <Float64Array>
85+
86+
out = random( 10 );
87+
// returns <Float64Array>
88+
```
89+
90+
If not provided `alpha` and `beta`, the returned generator requires that both parameters be provided at each invocation.
91+
92+
```javascript
93+
var random = beta.factory();
94+
95+
var out = random( 10, 2.0, 5.0 );
96+
// returns <Float64Array>
97+
98+
out = random( 10, 2.0, 5.0 );
99+
// returns <Float64Array>
100+
```
101+
102+
The function accepts the following `options`:
103+
104+
- **prng**: pseudorandom number generator for generating uniformly distributed pseudorandom numbers on the interval `[0,1)`. If provided, the function **ignores** both the `state` and `seed` options. In order to seed the underlying pseudorandom number generator, one must seed the provided `prng` (assuming the provided `prng` is seedable).
105+
- **seed**: pseudorandom number generator seed.
106+
- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
107+
- **copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that an underlying generator has exclusive control over its internal state. Default: `true`.
108+
- **dtype**: default output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic". Default: `'float64'`.
109+
110+
To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
111+
112+
```javascript
113+
var minstd = require( '@stdlib/random/base/minstd' );
114+
115+
var opts = {
116+
'prng': minstd.normalized
117+
};
118+
var random = beta.factory( 2.0, 5.0, opts );
119+
120+
var out = random( 10 );
121+
// returns <Float64Array>
122+
```
123+
124+
To seed the underlying pseudorandom number generator, set the `seed` option.
125+
126+
```javascript
127+
var opts = {
128+
'seed': 12345
129+
};
130+
var random = beta.factory( 2.0, 5.0, opts );
131+
132+
var out = random( 10, opts );
133+
// returns <Float64Array>
134+
```
135+
136+
The returned function accepts the following `options`:
137+
138+
- **dtype**: output array data type. Must be a [real-valued floating-point data type][@stdlib/array/typed-real-float-dtypes] or "generic". This overrides the default output array data type.
139+
140+
To override the default output array data type, set the `dtype` option.
141+
142+
```javascript
143+
var random = beta.factory( 2.0, 5.0 );
144+
145+
var out = random( 10 );
146+
// returns <Float64Array>
147+
148+
var opts = {
149+
'dtype': 'generic'
150+
};
151+
out = random( 10, opts );
152+
// returns [...]
153+
```
154+
155+
#### beta.PRNG
156+
157+
The underlying pseudorandom number generator.
158+
159+
```javascript
160+
var prng = beta.PRNG;
161+
// returns <Function>
162+
```
163+
164+
#### beta.seed
165+
166+
The value used to seed the underlying pseudorandom number generator.
167+
168+
```javascript
169+
var seed = beta.seed;
170+
// returns <Uint32Array>
171+
```
172+
173+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
174+
175+
```javascript
176+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
177+
178+
var random = beta.factory( 2.0, 5.0, {
179+
'prng': minstd
180+
});
181+
182+
var seed = random.seed;
183+
// returns null
184+
```
185+
186+
#### beta.seedLength
187+
188+
Length of underlying pseudorandom number generator seed.
189+
190+
```javascript
191+
var len = beta.seedLength;
192+
// returns <number>
193+
```
194+
195+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
196+
197+
```javascript
198+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
199+
200+
var random = beta.factory( 2.0, 5.0, {
201+
'prng': minstd
202+
});
203+
204+
var len = random.seedLength;
205+
// returns null
206+
```
207+
208+
#### beta.state
209+
210+
Writable property for getting and setting the underlying pseudorandom number generator state.
211+
212+
```javascript
213+
var state = beta.state;
214+
// returns <Uint32Array>
215+
```
216+
217+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
218+
219+
```javascript
220+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
221+
222+
var random = beta.factory( 2.0, 5.0, {
223+
'prng': minstd
224+
});
225+
226+
var state = random.state;
227+
// returns null
228+
```
229+
230+
#### beta.stateLength
231+
232+
Length of underlying pseudorandom number generator state.
233+
234+
```javascript
235+
var len = beta.stateLength;
236+
// returns <number>
237+
```
238+
239+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
240+
241+
```javascript
242+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
243+
244+
var random = beta.factory( 2.0, 5.0, {
245+
'prng': minstd
246+
});
247+
248+
var len = random.stateLength;
249+
// returns null
250+
```
251+
252+
#### beta.byteLength
253+
254+
Size (in bytes) of underlying pseudorandom number generator state.
255+
256+
```javascript
257+
var sz = beta.byteLength;
258+
// returns <number>
259+
```
260+
261+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
262+
263+
```javascript
264+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
265+
266+
var random = beta.factory( 2.0, 5.0, {
267+
'prng': minstd
268+
});
269+
270+
var sz = random.byteLength;
271+
// returns null
272+
```
273+
274+
</section>
275+
276+
<!-- /.usage -->
277+
278+
<section class="notes">
279+
280+
## Notes
281+
282+
- If PRNG state is "shared" (meaning a state array was provided during function creation and **not** copied) and one sets the underlying generator state to a state array having a different length, the function returned by the `factory` method does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize the output of the underlying generator according to the new shared state array, the state array for **each** relevant creation function and/or PRNG must be **explicitly** set.
283+
- If PRNG state is "shared" and one sets the underlying generator state to a state array of the same length, the PRNG state is updated (along with the state of all other creation functions and/or PRNGs sharing the PRNG's state array).
284+
285+
</section>
286+
287+
<!-- /.notes -->
288+
289+
<section class="examples">
290+
291+
## Examples
292+
293+
<!-- eslint no-undef: "error" -->
294+
295+
```javascript
296+
var logEach = require( '@stdlib/console/log-each' );
297+
var beta = require( '@stdlib/random/array/beta' );
298+
299+
// Create a function for generating random arrays originating from the same state:
300+
var random = beta.factory( 2.0, 5.0, {
301+
'state': beta.state,
302+
'copy': true
303+
});
304+
305+
// Generate 3 arrays:
306+
var x1 = random( 5 );
307+
var x2 = random( 5 );
308+
var x3 = random( 5 );
309+
310+
// Print the contents:
311+
logEach( '%f, %f, %f', x1, x2, x3 );
312+
313+
// Create another function for generating random arrays with the original state:
314+
random = beta.factory( 2.0, 5.0, {
315+
'state': beta.state,
316+
'copy': true
317+
});
318+
319+
// Generate a single array which replicates the above pseudorandom number generation sequence:
320+
var x4 = random( 15 );
321+
322+
// Print the contents:
323+
logEach( '%f', x4 );
324+
```
325+
326+
</section>
327+
328+
<!-- /.examples -->
329+
330+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
331+
332+
<section class="related">
333+
334+
</section>
335+
336+
<!-- /.related -->
337+
338+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
339+
340+
<section class="links">
341+
342+
[@stdlib/random/base/beta]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/beta
343+
344+
[@stdlib/array/typed-real-float-dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed-real-float-dtypes
345+
346+
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32
347+
348+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
349+
350+
</section>
351+
352+
<!-- /.links -->

0 commit comments

Comments
 (0)