Skip to content

Commit 9453fc8

Browse files
Aadish JainAadish Jain
authored andcommitted
feat(levy-logpdf): adding all the functionality required for levy logpdf
1 parent f5f16d3 commit 9453fc8

File tree

17 files changed

+1411
-14
lines changed

17 files changed

+1411
-14
lines changed

lib/node_modules/@stdlib/stats/base/dists/levy/logpdf/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,104 @@ for ( i = 0; i < 10; i++ ) {
146146

147147
<!-- /.examples -->
148148

149+
<!-- C interface documentation. -->
150+
151+
* * *
152+
153+
<section class="c">
154+
155+
## C APIs
156+
157+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
158+
159+
<section class="intro">
160+
161+
</section>
162+
163+
<!-- /.intro -->
164+
165+
<!-- C usage documentation. -->
166+
167+
<section class="usage">
168+
169+
### Usage
170+
171+
```c
172+
#include "stdlib/stats/base/dists/levy/logpdf.h"
173+
```
174+
175+
#### stdlib_base_dists_levy_logpdf( x, mu, c )
176+
177+
Evaluates the logarithm of the [probability density function][pdf] (PDF) for a [Lévy][levy-distribution] distribution with parameters `mu` (location parameter) and `c` (scale parameter).
178+
179+
```c
180+
double out = stdlib_base_dists_levy_logpdf( 0.5, 0.0, 2.0 );
181+
// returns ~-1.837
182+
```
183+
184+
The function accepts the following arguments:
185+
186+
- **x**: `[in] double` input value.
187+
- **mu**: `[in] double` location parameter.
188+
- **c**: `[in] double` scale parameter.
189+
190+
```c
191+
double stdlib_base_dists_levy_logpdf( const double x, const double mu, const double c );
192+
```
193+
194+
</section>
195+
196+
<!-- /.usage -->
197+
198+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199+
200+
<section class="notes">
201+
202+
</section>
203+
204+
<!-- /.notes -->
205+
206+
<!-- C API usage examples. -->
207+
208+
<section class="examples">
209+
210+
### Examples
211+
212+
```c
213+
#include "stdlib/stats/base/dists/levy/logpdf.h"
214+
#include <stdlib.h>
215+
#include <stdio.h>
216+
217+
static double random_uniform( const double min, const double max ) {
218+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
219+
return min + ( v*(max-min) );
220+
}
221+
222+
int main( void ) {
223+
double x;
224+
double mu;
225+
double c;
226+
double y;
227+
int i;
228+
229+
for ( i = 0; i < 25; i++ ) {
230+
x = random_uniform( 0.0, 10.0 );
231+
mu = random_uniform( -5.0, 5.0 );
232+
c = random_uniform( 0.1, 5.0 );
233+
y = stdlib_base_dists_levy_logpdf( x, mu, c );
234+
printf( "x: %lf, mu: %lf, c: %lf, logpdf(x;mu,c): %lf\n", x, mu, c, y );
235+
}
236+
}
237+
```
238+
239+
</section>
240+
241+
<!-- /.examples -->
242+
243+
</section>
244+
245+
<!-- /.c -->
246+
149247
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150248

151249
<section class="related">

lib/node_modules/@stdlib/stats/base/dists/levy/logpdf/benchmark/benchmark.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,28 +21,37 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24+
var Float64Array = require( '@stdlib/array/float64' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2426
var randu = require( '@stdlib/random/base/randu' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26-
var EPS = require( '@stdlib/constants/float64/eps' );
2728
var pkg = require( './../package.json' ).name;
2829
var logpdf = require( './../lib' );
2930

3031

3132
// MAIN //
3233

3334
bench( pkg, function benchmark( b ) {
35+
var len;
3436
var mu;
35-
var s;
37+
var c;
3638
var x;
3739
var y;
3840
var i;
3941

42+
len = 100;
43+
x = new Float64Array( len );
44+
mu = new Float64Array( len );
45+
c = new Float64Array( len );
46+
for ( i = 0; i < len; i++ ) {
47+
x[ i ] = ( randu() * 10.0 );
48+
mu[ i ] = ( randu() * 10.0 ) - 5.0;
49+
c[ i ] = ( randu() * 4.9 ) + 0.1;
50+
}
51+
4052
b.tic();
4153
for ( i = 0; i < b.iterations; i++ ) {
42-
mu = ( randu()*20.0 ) - 10.0;
43-
x = ( randu()*40.0 ) + mu;
44-
s = ( randu()*5.0 ) + EPS;
45-
y = logpdf( x, mu, s );
54+
y = logpdf( x[ i % len ], mu[ i % len ], c[ i % len ] );
4655
if ( isnan( y ) ) {
4756
b.fail( 'should not return NaN' );
4857
}
@@ -55,22 +64,22 @@ bench( pkg, function benchmark( b ) {
5564
b.end();
5665
});
5766

58-
bench( pkg+':factory', function benchmark( b ) {
67+
bench( pkg + ':factory', function benchmark( b ) {
5968
var mylogpdf;
6069
var mu;
61-
var s;
70+
var c;
6271
var x;
6372
var y;
6473
var i;
6574

66-
mu = 10.0;
67-
s = 4.0;
68-
mylogpdf = logpdf.factory( mu, s );
75+
mu = 0.0;
76+
c = 1.0;
77+
mylogpdf = logpdf.factory( mu, c );
78+
x = uniform( 100, -2.0, 2.0 );
6979

7080
b.tic();
7181
for ( i = 0; i < b.iterations; i++ ) {
72-
x = randu() * 50.0;
73-
y = mylogpdf( x );
82+
y = mylogpdf( x[ i % x.length ] );
7483
if ( isnan( y ) ) {
7584
b.fail( 'should not return NaN' );
7685
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var logpdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( logpdf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg + '::native', opts, function benchmark( b ) {
43+
var len;
44+
var mu;
45+
var c;
46+
var x;
47+
var y;
48+
var i;
49+
50+
len = 100;
51+
x = new Float64Array( len );
52+
mu = new Float64Array( len );
53+
c = new Float64Array( len );
54+
for ( i = 0; i < len; i++ ) {
55+
x[ i ] = ( randu() * 10.0 );
56+
mu[ i ] = ( randu() * 10.0 ) - 5.0;
57+
c[ i ] = ( randu() * 4.9 ) + 0.1;
58+
}
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
y = logpdf( x[ i % len ], mu[ i % len ], c[ i % len ] );
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});

0 commit comments

Comments
 (0)