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
@@ -1,3 +1,2 @@
Distributions 0.23.8
julia 1.5
JSON 0.21
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sigma":[4.372448161181262,4.507779737981885,2.308292309093838,1.7741874189836637,0.31507521564566265,1.7945498757493117,2.484120101875779,1.8014970499175265,2.4955804577203144,2.185131411239958,2.805319026010679,0.8251792272092917,0.6749340206857746,0.6095685925273664,1.9222550510739178,3.610927283846249,4.363709627043577,0.8365430160947551,4.9582552039481005,3.944801044938724,1.4085253337087922,3.3578824067227595,0.8757806166602033,4.277911759657698,3.2398177086236357,2.6767355318357424,3.3005646996079396,4.075667020413391,2.1165984455816735,2.573158653777754,3.551529690803654,1.2656405632837509,1.49593156919257,2.3017718957908526,0.4823234640018226,1.1587034896771775,3.0831569181573784,1.1545926583806598,4.824989369186914,4.786962945045098,0.6786152774438026,3.760384015938966,4.197561605952372,1.6217772164357158,0.3640416335603147,1.010091708991523,0.9201385774137683,4.548798603750895,0.04081842516157064,4.36036983426966,4.42320519400549,2.9001714869597017,2.719448199461083,3.3350595840950925,0.9892978690118837,0.10336309764470086,3.9839675215599524,0.3181352586162056,3.762680927232196,0.5074452632699933,2.2006988264529337,2.3496823648832854,1.8428977548334557,0.05438641951580503,0.2733675289242188,0.12663328098316273,4.337325360579709,0.6957303538011506,2.4567548125520733,2.186112925258655,3.6404386228430505,3.7957019241094034,3.743686451322273,3.180827357760939,1.2192110149492374,3.8783824760250054,3.8717090528182863,0.6895675822024083,4.494831697610012,1.9754053512532177,0.43643372611896103,0.5541016454684644,4.333418678833584,4.434437046160502,2.546510808354628,4.153832150411521,2.91332028910133,1.5850106767162986,2.3151923474597047,2.271606850151444,3.683739031658267,2.1709264877425682,3.8551971556157505,3.8786127818556047,4.876192597625505,4.7692510203664185,4.456683646975332,2.5868300814009473,3.1274279753790735,0.052954852877593606],"expected":[3.7253726724275597e6,4.4392569855739765e7,10.26500714995574,0.7583755550911014,0.012537993106318921,0.5250924012496011,19.945929770815166,0.8873932670524205,58.99800429028618,3.0793004645544086,58.23330167244097,0.052983799885807085,0.017694546738403205,0.04276089588170603,0.8861449918044398,25583.978170436756,5.210435247142238e6,0.07726731625142313,2.282205332465917e9,300845.87106584816,0.8489915870937841,7565.624030014332,0.04688564423039902,2.8379738551656352e6,1628.5115935804004,26.04438879060696,2390.226993800247,1.7739110064637365e6,3.915078109264716,21.609887191480563,9902.176511368612,0.23019215723072528,1.1432560967366912,19.020885943664922,0.010549872754361192,0.29304804573033955,1112.5169174009964,0.06463114112942038,1.602215893135758e9,7.821441527583933e8,0.02053177262761622,43793.567353101695,4.00382637761903e6,0.7804898213066502,0.014548072766927071,0.15712050504826136,0.0899250239220967,1.0340983850597422e8,0.002763382281651077,1.3338991378090415e7,1.2029968926432032e7,91.86674114343772,159.76746685609865,2549.6102702632184,0.20540391898584445,0.002524152003730183,594826.9212999693,0.02660417850135068,138348.6954692866,0.07583186352353789,2.5705608582387764,5.481392903582849,2.1002074295351214,0.0045927964728261,0.025922792877743284,0.01222987029480063,1.347241934059741e7,0.0859996285048383,20.615755428435122,2.287552869097583,14659.7842284774,45355.90857710304,132266.81647678112,1381.6538970405961,0.09730611375029731,192589.6471404916,265528.09538348403,0.03066748818694898,3.4273557183928795e7,3.758030539779181,0.00980282677955075,0.028303354996340083,1.0851358083880417e7,1.1553346602298357e7,12.573809763433625,1.491836658643478e6,255.36686025510437,1.4931403273243335,16.442152836251903,9.241619100448267,59708.1223714079,2.986079715753444,214244.36162821326,255664.89844212984,5.993919604195529e8,1.7851854780063e8,8.4554828010827e6,16.592714898506497,389.34647776778627,0.002318327498523008],"mu":[-3.98762546864778,-2.7114954976045658,-2.997040364725514,-3.4023682391911962,-3.2987074965588783,-3.844211209781736,-3.176781759756721,-3.344992506860999,-2.1494304536902606,-3.6458590378443763,-3.805166363852011,-3.2659273608231563,-3.9873261256888033,-2.9386809425324363,-3.803359315774366,-2.8890731802426646,-3.575787755508017,-2.9170213642141847,-3.035886603959496,-2.947101840583809,-2.073674585671807,-2.343997804721533,-3.514844081636834,-3.4419280935348042,-3.100983223431985,-3.904723991270577,-3.1145744321206705,-2.222364355617197,-3.109454573305522,-3.5473281111037314,-3.4128516194375234,-2.958161607778441,-2.0475190459440453,-2.350109542871997,-3.998105101373435,-2.418670542756289,-2.4914391526522905,-3.919109563390459,-2.0858689714614504,-2.437464617091938,-3.847880314063043,-3.4532453645627728,-2.4167623683807977,-2.840597135985702,-3.3195702902127096,-2.6474993350078573,-2.975384923350693,-2.237358071496792,-2.693927848449905,-2.6066231020886623,-3.261832681743491,-3.890544375966013,-2.3213719533114814,-3.278919250986675,-2.3258485877939834,-3.7203581683573184,-2.5759713952532715,-2.5575292634904265,-2.3202348491098905,-2.0953764286339993,-3.8949942128965995,-3.817643045741262,-2.6372001083495875,-2.4738439837938335,-2.411858227645943,-2.3494463622077197,-2.3962361383427733,-2.458521389613533,-3.0083911887067365,-3.9473875144853188,-3.659929232021267,-3.6850570846183386,-2.222611337955388,-2.886605962629553,-3.688168467546199,-2.8735334599189004,-2.5006549004879934,-3.4741908662372865,-2.853627301762462,-2.5681294528080274,-3.939577155827911,-3.206601618579716,-2.5787166434288786,-3.401746212839236,-3.952337357226517,-3.0388029421093,-2.9446308968119768,-2.069096583162575,-2.5579115515060042,-2.933602031459561,-2.572709270933912,-3.614450892236255,-2.587672413853305,-2.5920140896284183,-3.56582794907076,-3.74555223216696,-3.911703486915384,-3.8821051553084347,-3.8163078417287046,-3.1306970243048586]}
{"expected":[33.296135828493554,0.22173877252337126,0.013167428204015966,245032.75895561316,656.814378786937,249951.5999778303,7.659955682026986,11.542903151272373,89305.9960918289,454801.3819156509,4696.100883668945,0.7408522314096593,1.3730765628719777e6,74756.42508083694,41.69754771144814,6.588056830372515e8,0.007544663341414214,0.0117661674868273,1.6171013514751928,0.11078470468761034,1054.3698135035088,3.944865302736056e8,1.2532263736746825,0.200132908362691,0.008124246222514637,1.2245906207901391e8,0.5014545211361671,0.15634630987068773,0.023430653313371057,0.1824216848250615,0.6357328391653366,0.00037864708785296966,99.5823145841211,0.012247956810828959,0.4360550521903704,0.17529754611163834,10.905280777310786,1.9843014279781194e9,4726.682196805757,0.020128826829171415,1.2340702762573578e6,30.03498339038285,0.05619076943734589,0.03765312415013708,0.00516382613215025,0.09040426942333746,0.034588226822355,1.757089130927487e7,0.10400255846462461,12.426307869652987,1.6387153683505628e7,5852.4972113960785,2306.2726180834024,1358.8184355829092,0.35996639831620353,571947.2853510635,1.324617877041741,0.528809567592436,0.12108900592447447,1.0124764325805196e6,3.6947881912426888,0.010463019668770472,1.8174734331538746e9,59463.658906825,1.3364736552839202e8,2.262519809022253,31535.96892722234,812.4005187630886,1.750794323895859e7,4.0530973042918006e6,1.0671320833196738e9,3.14167431418941,4493.122346608968,4.5177471019424087e8,0.8898387538115177,0.006470918617154905,5168.720751324912,1.1607244024862802e8,1.1882843293722134e7,7.394307476726226,0.011027608928167364,1.5163690677262183,0.0038385597786708514,168.8381925925224,1.2717574187343803,2.9091751459136534e8,1.5922412764745876e8,0.01445590562591941,0.07070948946021414,8.707662911813906e8,5535.651907696916,295496.4039345913,8.496806365375681e8,8.707300599794542e6,28384.691766091943,0.08320738369251311,0.14160488787951922,11728.89706046313,187.21173344773524,12.214834497197263],"mu":[-2.3915137116259975,-3.1019086374707423,-3.3190550833873314,-3.7088258654322095,-2.9518718244550994,-3.131055537176037,-3.327589575011668,-2.723841786480249,-3.724109634675675,-2.5341098033027567,-2.566209347269961,-3.3738514531109765,-3.8054077622439078,-2.4792978609967253,-2.2835116101237167,-2.6464174708053827,-3.5973283520267776,-2.7221774220364,-3.0933050409993728,-2.325939674630099,-3.6345212318142166,-3.413010024222376,-3.291322711128439,-3.190289164519498,-3.696232417534511,-3.7396300910070246,-2.3587732712529066,-2.7466091456069543,-2.2357697328227815,-3.3298066848676227,-2.289717533578983,-3.945205081002607,-3.257073029540999,-3.185457319678708,-3.9152786713896335,-3.209061556746237,-2.2248815112503513,-2.793505501911589,-2.8544004791848483,-3.649128852537258,-2.301913725278803,-2.086525471964408,-3.7821591417850784,-2.450640211173785,-3.6984440328453156,-2.001251938480629,-2.254563114488544,-3.491292699302009,-3.6626105657903265,-3.1955019645413754,-2.578588873202553,-2.712739911641811,-2.4791694243707973,-3.6452357146661445,-2.377459077233824,-3.8365797948488094,-2.4567483474873324,-2.8270893401321753,-3.0592780567354856,-2.7574761834137385,-2.4301906245644416,-3.4179554232063922,-2.8105562358070744,-2.477790083935151,-2.1636237495478063,-2.137028858732631,-3.5203955587819964,-2.373784346957203,-2.752073466682835,-2.7868760361941876,-3.720758122224696,-2.054555128690262,-2.9692149488619206,-2.3568130395485123,-2.1068007385665353,-2.6660652153851068,-3.8405500114502376,-2.304671537791454,-3.5403181738427656,-3.4361722233613623,-3.6169622365901306,-3.205766993715338,-3.7131714187797,-3.245084398315777,-3.9370099477013145,-3.1875578195908725,-2.818485597993085,-3.291298516224983,-3.889821333593931,-3.248648087946874,-3.0559437657810653,-2.336687533515765,-3.6522172047416004,-2.384723618028466,-3.0270043982458317,-2.8408411575174624,-2.5006360885689465,-2.322949779683995,-3.281349709225946,-3.5499631349619207],"sigma":[2.428647557746435,1.3026390303506012,0.33449597700119404,4.014719555093447,3.072346504486987,3.9446264972173335,2.31644939239025,2.2743691692118873,3.888950202804642,3.944835353964213,3.319744775013356,1.759833968269946,4.235324334303149,3.701525207710345,2.452585601164514,4.790861786091988,0.26158309524542567,0.17493834424797572,1.894191754702993,0.7439872519001911,3.2550318406702914,4.817271590655677,1.8793315356206426,1.2977545157191765,0.30522061887207463,4.728944642950482,1.3276497220544403,1.0462565353345699,0.21189646544084062,1.3139025632354047,1.3843137595736348,0.019565489946410386,2.803257199403109,0.27927421836600463,1.7629941145409718,1.2580757661760589,2.1492054451690477,4.919556714341288,3.3638349198923434,0.5929140546368278,4.040760101467751,2.34327858003246,1.0509227670634902,0.3894778813760663,0.20223163811526113,0.5375736317084284,0.30709595388205724,4.491441515603629,1.233834772459782,2.3910176375294516,4.380707380022288,3.37451764011574,3.197276264264819,3.2953931674312265,1.2182946530434058,4.134414338372601,1.664397989123389,1.4986306256818822,1.0679909735294557,4.072515931948811,1.9362399005757658,0.2985106625905809,4.912358841314619,3.6702740251795007,4.568844387655472,1.7261469420068343,3.7254905813830113,3.012280719247251,4.407974444547788,4.2428608222618625,4.950656392163446,1.7943535725047033,3.3733549771158544,4.720752823906282,1.4347036264419744,0.09247771305556873,3.520075651106116,4.568850684503092,4.453192602259016,2.3321790630038746,0.37024404143983225,1.9066852752897268,0.15452386655736672,2.893810741337581,2.0457464906858225,4.761943736501316,4.658788358966976,0.35364010706120885,1.176752020217583,4.881959879430762,3.416857128332323,3.8643368956882673,4.920628854108375,4.285369996214496,3.6442571956969765,0.8356964415533435,0.9119598432958065,3.4194685064654355,2.9178228717578,2.460445110772552]}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import Distributions: var, LogNormal
import JSON

"""
Expand All @@ -39,9 +38,10 @@ julia> gen( mu, sigma, "data.json" );
```
"""
function gen( mu, sigma, name )
z = Array{Float64}( undef, length(mu) );
for i in eachindex(mu)
z[ i ] = sqrt( var( LogNormal( mu[i], sigma[i] ) ) );
# The standard deviation is evaluated in extended precision using the textbook formula `sqrt((exp(sigma^2) - 1) * exp(2*mu + sigma^2))`, as subtracting `1` from `exp(sigma^2)` in double-precision cancels for small `sigma`...
z = setprecision( BigFloat, 2048 ) do
s2 = BigFloat.( sigma ) .^ 2;
Float64.( sqrt.( ( exp.( s2 ) .- 1 ) .* exp.( ( 2 .* BigFloat.( mu ) ) .+ s2 ) ) );
end

# Store data to be written to file as a collection:
Expand Down Expand Up @@ -71,3 +71,8 @@ dir = dirname( file );
mu = rand( 100 ) .* 2.0 .- 4.0;
sigma = rand( 100 ) .* 5.0;
gen( mu, sigma, "data.json" );

# Small `sigma`:
mu = ( rand( 1000 ) .* 4.0 ) .- 2.0;
sigma = exp10.( range( -150.0, stop = -1.0, length = 1000 ) );
gen( mu, sigma, "small_sigma.json" );

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var stdev = require( './../lib' );
// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var smallSigma = require( './fixtures/julia/small_sigma.json' );


// TESTS //
Expand Down Expand Up @@ -86,7 +87,26 @@ tape( 'the function returns the standard deviation of a lognormal distribution',
for ( i = 0; i < mu.length; i++ ) {
y = stdev( mu[i], sigma[i] );
if ( expected[i] !== null ) {
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 18 ), true, 'returns expected value' );
}
}
t.end();
});

tape( 'the function returns the standard deviation of a lognormal distribution with a small `sigma`', function test( t ) {
var expected;
var sigma;
var mu;
var y;
var i;

expected = smallSigma.expected;
mu = smallSigma.mu;
sigma = smallSigma.sigma;
for ( i = 0; i < mu.length; i++ ) {
y = stdev( mu[i], sigma[i] );
if ( expected[i] !== null ) {
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
}
t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var NINF = require( '@stdlib/constants/float64/ninf' );
// FIXTURES //

var data = require( './fixtures/julia/data.json' );
var smallSigma = require( './fixtures/julia/small_sigma.json' );


// VARIABLES //
Expand Down Expand Up @@ -98,7 +99,26 @@ tape( 'the function returns the standard deviation of a lognormal distribution',
for ( i = 0; i < mu.length; i++ ) {
y = stdev( mu[i], sigma[i] );
if ( expected[i] !== null ) {
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 18 ), true, 'returns expected value' );
}
}
t.end();
});

tape( 'the function returns the standard deviation of a lognormal distribution with a small `sigma`', opts, function test( t ) {
var expected;
var sigma;
var mu;
var y;
var i;

expected = smallSigma.expected;
mu = smallSigma.mu;
sigma = smallSigma.sigma;
for ( i = 0; i < mu.length; i++ ) {
y = stdev( mu[i], sigma[i] );
if ( expected[i] !== null ) {
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
}
t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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


// MAIN //
Expand Down Expand Up @@ -63,7 +64,7 @@ function variance( mu, sigma ) {
return NaN;
}
s2 = sigma*sigma;
return ( exp( s2 ) - 1.0 ) * exp( ( 2.0*mu ) + s2 );
return expm1( s2 ) * exp( ( 2.0*mu ) + s2 );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/exp"
]
},
Expand All @@ -56,6 +57,7 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp"
]
Expand All @@ -73,6 +75,7 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/exp"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "stdlib/stats/base/dists/lognormal/variance.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/expm1.h"

/**
* Returns the variance for a lognormal distribution with location `mu` and scale `sigma`.
Expand All @@ -41,5 +42,5 @@ double stdlib_base_dists_lognormal_variance( const double mu, const double sigma
return 0.0 / 0.0; // NaN
}
s2 = sigma * sigma;
return ( stdlib_base_exp( s2 ) - 1.0 ) * stdlib_base_exp( ( 2.0 * mu ) + s2 );
return stdlib_base_expm1( s2 ) * stdlib_base_exp( ( 2.0 * mu ) + s2 );
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Distributions 0.23.8
julia 1.5
JSON 0.21
Loading
Loading