Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var expm1 = require( '@stdlib/math/base/special/expm1' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var pow = require( '@stdlib/math/base/special/pow' );


Expand Down Expand Up @@ -69,7 +71,7 @@ function factory( a, b ) {
if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
return NaN;
}
return pow( 1.0 - pow( 1.0-p, 1.0/b ), 1.0/a );
return pow( -expm1( log1p( -p ) / b ), 1.0/a );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var expm1 = require( '@stdlib/math/base/special/expm1' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var pow = require( '@stdlib/math/base/special/pow' );


Expand Down Expand Up @@ -86,7 +88,7 @@ function quantile( p, a, b ) {
) {
return NaN;
}
return pow( 1.0 - pow( 1.0-p, 1.0/b ), 1.0/a );
return pow( -expm1( log1p( -p ) / b ), 1.0/a );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"dependencies": [
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow"
]
},
Expand All @@ -56,6 +58,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/eps"
]
Expand All @@ -73,6 +77,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/eps"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "stdlib/stats/base/dists/kumaraswamy/quantile.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/expm1.h"
#include "stdlib/math/base/special/log1p.h"
#include "stdlib/math/base/special/pow.h"

/**
Expand All @@ -44,5 +46,5 @@ double stdlib_base_dists_kumaraswamy_quantile( const double p, const double a, c
) {
return 0.0/0.0; // NaN
}
return stdlib_base_pow( 1.0 - stdlib_base_pow( 1.0-p, 1.0/b ), 1.0/a );
return stdlib_base_pow( -stdlib_base_expm1( stdlib_base_log1p( -p ) / b ), 1.0/a );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
julia 1.5
JSON 0.21

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env julia
#
# @license Apache-2.0
#
# Copyright (c) 2026 The Stdlib Authors.
#
# 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.

import JSON

"""
gen( p, a, b, name )

Generate fixture data and write to file.

The quantile is evaluated in extended precision from the textbook formula `(1 - (1-p)^(1/b))^(1/a)`, as forming `1-p` in double precision rounds away the low-order bits of `p` (all of them for `p < 2^-53`).

# Arguments

* `p`: input probability
* `a`: first shape parameter
* `b`: second shape parameter
* `name::AbstractString`: output filename
"""
function gen( p, a, b, name )
z = setprecision( BigFloat, 2048 ) do
Float64.( ( 1 .- ( 1 .- BigFloat.( p ) ) .^ ( 1 ./ BigFloat.( b ) ) ) .^ ( 1 ./ BigFloat.( a ) ) )
end

# Store data to be written to file as a collection:
data = Dict([
("p", p),
("a", a),
("b", b),
("expected", z)
]);

# Based on the script directory, create an output filepath:
filepath = joinpath( dir, name );

# Write the data to the output filepath as JSON:
outfile = open( filepath, "w" );
write( outfile, JSON.json(data) );
write( outfile, "\n" );
close( outfile );
end

# Get the filename:
file = @__FILE__;

# Extract the directory in which this file resides:
dir = dirname( file );

# Random values:
p = rand( 1000 );
a = ( rand( 1000 ) .* 5.0 ) .+ 0.5;
b = ( rand( 1000 ) .* 5.0 ) .+ 0.5;
gen( p, a, b, "data.json" );

# Small `p` (`a` is a power of two, so that `1/a` is exact and the test measures the error from `1-p`):
p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
a = repeat( [ 1.0, 2.0, 4.0, 8.0 ], 250 );
b = ( rand( 1000 ) .* 5.0 ) .+ 0.5;
gen( p, a, b, "small_p.json" );

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
// MODULES //

var tape = require( 'tape' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var randu = require( '@stdlib/random/base/randu' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var EPS = require( '@stdlib/constants/float64/eps' );
var factory = require( './../lib/factory.js' );


// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );


// TESTS //

tape( 'main export is a function', function test( t ) {
Expand Down Expand Up @@ -143,22 +147,44 @@ tape( 'if provided a nonpositive `a`, the created function always returns `NaN`'
t.end();
});

tape( 'the returned function evaluates the quantile function of a Kumaraswamy\'s double bounded distribution', function test( t ) {
tape( 'the created function evaluates the quantile for random `p`, `a` and `b`', function test( t ) {
var expected;
var quantile;
var a;
var b;
var i;
var p;
var y;

expected = data.expected;
p = data.p;
a = data.a;
b = data.b;
for ( i = 0; i < expected.length; i++ ) {
quantile = factory( a[i], b[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the created function evaluates the quantile for small `p`', function test( t ) {
var expected;
var quantile;
var a;
var b;
var i;
var p;
var y;

// TODO: Add fixtures
a = ( randu()*5.0 ) + EPS;
b = ( randu()*5.0 ) + EPS;
quantile = factory( a, b );
for ( i = 0; i < 100; i++ ) {
p = randu();
y = quantile( p, a, b );
t.strictEqual( isNumber( y ), true, 'returns expected value' );
expected = smallP.expected;
p = smallP.p;
a = smallP.a;
b = smallP.b;
for ( i = 0; i < expected.length; i++ ) {
quantile = factory( a[i], b[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
var resolve = require( 'path' ).resolve;
var tape = require( 'tape' );
var tryRequire = require( '@stdlib/utils/try-require' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var randu = require( '@stdlib/random/base/randu' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var EPS = require( '@stdlib/constants/float64/eps' );


// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );


// VARIABLES //
Expand Down Expand Up @@ -119,20 +123,40 @@ tape( 'if provided a nonpositive `b`, the function returns `NaN`', opts, functio
t.end();
});

tape( 'the function evaluates the quantile function of a Kumaraswamy\'s double bounded distribution', opts, function test( t ) {
tape( 'the function evaluates the quantile for random `p`, `a` and `b`', opts, function test( t ) {
var expected;
var a;
var b;
var i;
var p;
var y;

expected = data.expected;
p = data.p;
a = data.a;
b = data.b;
for ( i = 0; i < expected.length; i++ ) {
y = quantile( p[i], a[i], b[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for small `p`', opts, function test( t ) {
var expected;
var a;
var b;
var i;
var p;
var y;

// TODO: Add fixtures
for ( i = 0; i < 100; i++ ) {
p = randu();
a = ( randu()*5.0 ) + EPS;
b = ( randu()*5.0 ) + EPS;
y = quantile( p, a, b );
t.strictEqual( isNumber( y ), true, 'returns expected value' );
expected = smallP.expected;
p = smallP.p;
a = smallP.a;
b = smallP.b;
for ( i = 0; i < expected.length; i++ ) {
y = quantile( p[i], a[i], b[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
// MODULES //

var tape = require( 'tape' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var randu = require( '@stdlib/random/base/randu' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var EPS = require( '@stdlib/constants/float64/eps' );
var quantile = require( './../lib' );


// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );


// TESTS //

tape( 'main export is a function', function test( t ) {
Expand Down Expand Up @@ -110,20 +114,40 @@ tape( 'if provided a nonpositive `b`, the function returns `NaN`', function test
t.end();
});

tape( 'the function evaluates the quantile function of a Kumaraswamy\'s double bounded distribution', function test( t ) {
tape( 'the function evaluates the quantile for random `p`, `a` and `b`', function test( t ) {
var expected;
var a;
var b;
var i;
var p;
var y;

expected = data.expected;
p = data.p;
a = data.a;
b = data.b;
for ( i = 0; i < expected.length; i++ ) {
y = quantile( p[i], a[i], b[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for small `p`', function test( t ) {
var expected;
var a;
var b;
var i;
var p;
var y;

// TODO: Add fixtures
for ( i = 0; i < 100; i++ ) {
p = randu();
a = ( randu()*5.0 ) + EPS;
b = ( randu()*5.0 ) + EPS;
y = quantile( p, a, b );
t.strictEqual( isNumber( y ), true, 'returns expected value' );
expected = smallP.expected;
p = smallP.p;
a = smallP.a;
b = smallP.b;
for ( i = 0; i < expected.length; i++ ) {
y = quantile( p[i], a[i], b[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Loading