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 @@ -21,14 +21,30 @@
// MODULES //

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 //

/**
* Returns the excess kurtosis for a lognormal distribution with location `mu` and scale `sigma`.
*
* ## Method
*
* The excess kurtosis is
*
* ```tex
* e^{4\sigma^2} + 2e^{3\sigma^2} + 3e^{2\sigma^2} - 6
* ```
*
* which cancels for small \\(\sigma\\). With \\(u = e^{\sigma^2} - 1\\), evaluated by `expm1`, it expands to
*
* ```tex
* u(16 + u(15 + u(6 + u)))
* ```
*
* whose terms are all positive.
*
* @param {number} mu - location parameter
* @param {PositiveNumber} sigma - scale parameter
* @returns {number} excess kurtosis
Expand All @@ -54,21 +70,16 @@ var exp = require( '@stdlib/math/base/special/exp' );
* // returns NaN
*/
function kurtosis( mu, sigma ) {
var out;
var s2;
var u;
if (
isnan( mu ) ||
isnan( sigma ) ||
sigma <= 0.0
) {
return NaN;
}
s2 = sigma * sigma;
out = exp( 4.0*s2 );
out += 2.0 * exp( 3.0*s2 );
out += 3.0 * exp( 2.0*s2 );
out -= 6.0;
return out;
u = expm1( sigma*sigma );
return u * ( 16.0 + ( u * ( 15.0 + ( u * ( 6.0 + u ) ) ) ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp"
"@stdlib/math/base/special/expm1"
]
},
{
Expand All @@ -56,7 +56,7 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/expm1",
"@stdlib/constants/float64/eps"
]
},
Expand All @@ -73,7 +73,7 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp"
"@stdlib/math/base/special/expm1"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@

#include "stdlib/stats/base/dists/lognormal/kurtosis.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 excess kurtosis for a lognormal distribution with location `mu` and scale `sigma`.
*
* ## Method
*
* The excess kurtosis is
*
* ```tex
* e^{4\sigma^2} + 2e^{3\sigma^2} + 3e^{2\sigma^2} - 6
* ```
*
* which cancels for small \\(\sigma\\). With \\(u = e^{\sigma^2} - 1\\), evaluated by `expm1`, it expands to
*
* ```tex
* u(16 + u(15 + u(6 + u)))
* ```
*
* whose terms are all positive.
*
* @param mu location parameter
* @param sigma scale parameter
* @return excess kurtosis
Expand All @@ -32,19 +48,14 @@
* // returns ~110.936
*/
double stdlib_base_dists_lognormal_kurtosis( const double mu, const double sigma ) {
double out;
double s2;
double u;
if (
stdlib_base_is_nan( mu ) ||
stdlib_base_is_nan( sigma ) ||
sigma <= 0.0
) {
return 0.0/0.0; // NaN
}
s2 = sigma * sigma;
out = stdlib_base_exp( 4.0*s2 );
out += 2.0 * stdlib_base_exp( 3.0*s2 );
out += 3.0 * stdlib_base_exp( 2.0*s2 );
out -= 6.0;
return out;
u = stdlib_base_expm1( sigma * sigma );
return u * ( 16.0 + ( u * ( 15.0 + ( u * ( 6.0 + u ) ) ) ) );
}
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":[0.013606039203587494,1.6770196416730887,4.426416512870041,3.939767112070066,4.419662226502438,0.8434960457795337,1.0026940133952622,1.4512138282428566,2.8623145368917067,3.2153907122177996,2.549051222353713,0.7208063555120703,2.044091167217031,0.9107223308474277,0.32806114406364606,1.7706768795382821,2.7706240687282433,2.290652267933818,4.429448105152064,3.6411858326315416,3.936440891920885,2.6572952706035355,4.846500506506592,3.4914522517183633,4.514825261652415,3.3816883097647055,4.531386430969631,1.5646050466274741,1.1698142455987182,2.3306365033036047,0.25298270392462463,3.432215428336759,2.6393454298572716,4.546023064172649,4.9614981823819315,0.6767380270254053,1.995626862775618,0.9182724132017517,1.6123270518329025,1.176263648034963,4.161705200208267,3.8202396331483337,4.504624824960235,1.9047359026014143,0.03996494779838655,1.0943749037658457,4.294849529418365,1.7338675156060535,2.575391132109978,4.940118927276637,4.6043311839301815,4.766208202403544,0.223040099918973,2.8938559859622193,3.6441421854062006,3.774078854693675,2.281404509767518,1.8760172456551683,3.80300783924309,1.7744213999186365,2.480611887972033,0.9001588606748534,3.007506136357504,2.99031232372142,3.1316138424957716,3.626505161580307,1.7433973679516745,3.391897895536502,1.7749204524124174,4.153575862028073,1.908680439266962,4.6881269264290975,0.9231496485240065,3.5400867587100016,1.1607450221163806,4.806666723939346,2.683479522317742,3.6652189461301674,0.43170185675238004,1.5845150389696294,4.665099694101583,4.157859502858453,3.902770391049705,0.9327170836030874,2.168638486308275,4.997796469821671,3.5833264426009426,2.232959786563533,1.341290922694096,4.860616170905477,2.81668647327327,3.496961574934864,3.592712077690484,4.606904812578824,1.221039634545329,2.0474878204998936,4.775991769508673,3.2005905180987346,1.1102791583885163,4.517173024418933],"expected":[0.0029627772282978526,86904.32612102535,1.0884551812127136e34,9.205927173872686e26,8.570694117978124e33,40.57097462916551,113.02347398129417,5860.96113123167,1.7087123219377416e14,9.125455894930533e17,1.9449352569240494e11,19.975805678561414,1.8701350925232217e7,61.435473542836206,2.0207080750853397,305519.8480698636,2.1657186531854242e13,1.3173712422554023e9,1.211850959251774e34,1.0762506965795024e23,8.290046159529648e26,1.8507069543325483e12,6.365322628572092e40,1.501821208843442e21,2.570525418574072e35,7.346089523598491e19,4.680353750231139e35,21378.184211619016,400.03102625461463,2.753764569169632e9,1.1247575548773288,2.9116631204282466e20,1.2654699173055764e12,7.963149180311166e35,5.796666143283337e42,15.644800914707993,8.603750872707954e6,64.46248406783172,38219.60701320712,421.9616562892313,1.2233683097642807e30,2.2530773927674577e25,1.7790882302630783e35,2.1177298955578553e6,0.025613923280804585,219.97520809679804,1.1053996735304558e32,184640.86231706975,3.3359483533629706e11,2.485630020639736e42,6.728816398359076e36,2.904159117938304e39,0.8559062403015236,3.532055572328829e14,1.1730830263714398e23,5.543598714594206e24,1.1129010052559166e9,1.3802617072481334e6,1.3322219979787817e25,321799.3124232156,4.914289785487553e10,57.468152808846455,5.164623455702553e15,3.4190282136833995e15,1.0878296725197038e17,7.023703106411123e22,210107.91542297904,9.687038239190974e19,324036.5658544803,9.33529272228243e29,2.2472380548227155e6,1.5157385016621903e38,66.50824522254032,5.897787620089486e21,371.3336164258881,1.3672155967061007e40,3.237237724100069e12,2.1724715507692697e23,3.960706589227197,27171.320180376802,6.4043162912494e37,1.0764083038567397e30,2.8842213527928303e26,70.7403144211999,1.5060746781812477e8,2.4613727341799226e43,2.021918544042339e22,4.652789296591771e8,1879.5675199228124,1.1011657934479049e41,6.061768403997698e13,1.751870105915238e21,2.647069569361544e22,7.398109259386784e36,617.4468441893677,1.9761933212281045e7,4.218885118179443e39,6.241641662908753e17,248.55305197979465,2.7980710753276238e35],"mu":[-3.785188913233297,-2.3558649744770355,-3.3138459690143613,-2.9223611054467513,-2.277843450292301,-2.8313848475061523,-2.122842969889887,-2.779670277997986,-3.4129542144140266,-3.017605174232774,-3.2270755124211306,-3.4270122548111033,-3.9717943231511783,-2.3830370154185094,-2.5550404092066095,-2.8817967495314214,-2.502531286316559,-2.4357741219952023,-2.1733363701090442,-2.728269488988314,-2.710744423754661,-2.527014372496091,-3.6913646196420067,-3.9858664200566967,-3.3972121018385684,-2.6181944319390817,-2.737859280611349,-3.1187275564953567,-2.0777464142254476,-2.8035994714081585,-3.0331760887506274,-2.8664954691854305,-3.385305429742553,-2.3982491604332288,-3.4207936628141016,-2.7738508798259445,-3.3212546352727386,-3.133191803271066,-2.574504826434971,-2.003442385493707,-2.2254394340378583,-3.3018369649841675,-2.3637046976567095,-3.7428617372308106,-2.7093912186654125,-2.785996368097992,-2.0199160183078315,-3.632329689818935,-2.580119206736008,-3.930258571516214,-2.8283672729962146,-2.0052323273061843,-2.5568706518664057,-2.3781838978276753,-3.5859983464484637,-3.9087036130752435,-3.4165062159691026,-3.49929811527285,-2.420353356653944,-3.753722460142041,-3.254378589124414,-3.3729536409145533,-3.6456747280492423,-2.1354234399238883,-2.0094084069758176,-3.5512276285654876,-2.785791332081784,-2.7357346852271998,-3.146590229742844,-3.26415480092798,-2.3145113953644247,-3.8960914953470107,-3.60841917183644,-2.3860575144007594,-2.2075436876646544,-3.450663139245176,-3.820225377126244,-3.6124872689803036,-3.7159323405371394,-2.1306306042402037,-2.4275415478005145,-3.331225703727016,-3.0917926435848733,-2.6300856181964534,-3.573040835461344,-2.922436949577368,-2.9627511130863087,-3.4156214519770365,-2.4860214579297,-2.7104406118779694,-3.518425926969099,-3.024364503642358,-3.3736364638188445,-2.164492325513944,-3.597891765857109,-3.1755627047520267,-3.2114388475409577,-3.1780729895872453,-2.9642175380291222,-3.0083504985068727]}
{"expected":[374.6641575762713,0.25813962085582276,1.4447858872781317e6,3.399135621174137e39,3.16008176276834e27,2.369170020695188e33,657842.1230198158,1286.7746878508783,25.291400742239826,5.651081865253103e30,2.1533314229154277e36,1.87770415749761e35,3.0569395328710516e31,2.9340381292384405,7.464285602755757,5.097726402778806e36,7.910465082413064e37,1112.4703001340206,7405.078623373308,10121.912798346424,1.3501134898721713e40,46.8475663168776,1.2314968247021147,1.1567587250723511e41,491460.2135600005,3.9950144862962525e35,1.3777808597406285e18,3.0874297763377917e6,1.2662867856459092e18,2.240252734701454e11,1.9016932398723793e30,1.4289463900371857e20,2.3330153578965325e15,3.0467427575565087,217.0173937193514,3.262882309561579e39,7.895346894203445e31,52.18529377123516,111213.8293969303,3.3352885507406935e7,0.1892280289411066,5.510607969202061e6,1.1389079308742155e31,1.4954980837114608e27,21.41441790941474,1.4694570948468705e13,6.477380609282881e8,1.5772208477237264e6,0.7458796942407523,2.8820320208832912e7,6.341242908833529e28,2.6206898016711603e20,2.3772943611695165e18,1.6554378086983018e16,3.38480397605341e12,9.604897778919878e19,2.0155999680017145e40,3.6969204589727385e35,9.540711002242792e10,6.229872436881677e37,5.69016072869923e30,6.245794854083576e27,4.605375944013919,71660.63775658574,815183.1611355108,813.812969922389,0.03675026419630696,15.012769973600365,1.50674693005475e8,7.482434242009564,1.3161011535825863e21,1.0380749509469504e24,10.348589390718793,128.50575677622444,2.4960865372391215e8,0.005668340232668664,467.2678312079923,1.2083031183089783e33,0.7395662277155438,4.1209481483942305e12,1.263142314213425e26,1.014413562774108,1.1406622380615926e21,3.862782535605391e17,1.6774788339723594e38,1.7870505484339687e43,2.4471090631030197e20,3.293382221951729,5.532690538560754e32,2.320912764190833e18,17.500361835151768,1180.4541468836865,18.03791336731804,2.730594947638017e32,4.249306757383831e33,8.261529718234978e12,0.1503146484066514,3.785067638556271e23,3.2195431394602635e22,19.83892535987055],"mu":[-2.936268483547231,-3.5456407430467287,-2.49801917636995,-2.618252946656159,-2.0466537251006485,-2.7680898517432775,-2.9218119166556513,-2.821305338466147,-2.3507857664865957,-3.440566061106624,-2.8211611958768374,-2.5292492957681727,-2.81215375538012,-2.301579576871421,-2.173445458840998,-2.616630081475671,-2.1430009664551837,-2.9334977377800673,-3.7551044841401326,-3.7618165998806954,-3.0593527055437315,-2.8637232814741362,-3.7970649665660465,-2.609489430698722,-3.520524939250132,-2.3936346100030477,-3.042570407059161,-3.966624207952349,-3.4057618962111387,-3.9898463963056385,-2.0000782726334965,-3.132221671535051,-3.8614716073892144,-3.7908310465377824,-2.1867612028947487,-2.350121414206386,-2.5665092788463353,-3.3336272422938924,-3.6121654291144507,-3.380150333769962,-3.6265414365221185,-3.733532041305666,-2.4456840102716177,-3.2449275225812926,-2.2519890363360857,-2.0945682191109993,-3.5559321694485453,-2.3387300879848723,-3.624880091381444,-2.524104370950912,-2.2783986308237054,-3.7527950766074465,-2.55077892913095,-3.7451982692097214,-2.5199965852831587,-3.201726475991415,-2.99099805724927,-3.9310181413823435,-3.584874534679627,-2.3051876964255826,-2.2116237464290998,-2.8296393607238377,-2.1662507243309403,-2.7505864931201165,-3.057944902385742,-3.8233822698753093,-2.868291630727116,-3.8406269406378533,-2.4312801547487735,-3.454746741655492,-2.812093044037038,-3.8175354740820002,-3.856091813762546,-2.09349523867552,-3.5846319561800746,-2.6297910032704905,-2.4197244231179122,-2.2276301700842636,-3.2998239954204727,-2.530702475024433,-3.867061218893978,-3.8494945095720987,-3.720420788813494,-3.545913663191005,-3.4356969089054554,-2.0507918305066726,-3.5747491472880517,-3.630209442521034,-2.027017436425869,-3.1447549503763716,-3.4757146700719166,-3.1263576823505765,-2.046818479599792,-2.655142149008398,-2.590511030838605,-2.8411850763808406,-3.795422194864786,-3.0509237286700817,-3.197963455853589,-3.854673862143601],"sigma":[1.1618374601125603,0.12558000466621422,1.8791055624062947,4.770333840433493,3.9787056672254124,4.383145210670981,1.825108513213295,1.301966752506325,0.762629870349017,4.207416064409523,4.573294203527411,4.506121616465517,4.257274718606362,0.38339416813371496,0.5419837065220379,4.596788498055146,4.670755694128237,1.286444146606811,1.4725239840884263,1.5004074597003831,4.806339365230706,0.8672214727291544,0.26362663330627645,4.861882626724208,1.8046341425907286,4.527016828375898,3.2313676449874,1.9296382835441221,3.228101643320839,2.5559789424289487,4.1749340534957105,3.4061936582354715,2.974291939103117,0.38928900017469514,1.092595655599145,4.76926172418997,4.285044090986942,0.8847277585025631,1.6958719978806895,2.0794226102337547,0.10784234701417761,1.9672356919130174,4.228185417418687,3.9551313665586214,0.7332180174844305,2.753065436519475,2.2514651911420818,1.8850182036507757,0.20913881135843415,2.0705630442915535,4.071837953229406,3.428378760686874,3.2524007961963655,3.0555380321271812,2.6855558790565723,3.391584059026906,4.816749957739735,4.5248750926663766,2.5138523157846997,4.664359663904484,4.207620801717572,4.000053302764205,0.4570734989970082,1.6621011631078197,1.8400027149086489,1.252261937610346,0.047847044672857386,0.6692465709884676,2.1686643267221877,0.5424211325378964,3.4867230369915077,3.71817859440839,0.601332644704618,1.0210540334233258,2.1976901054565134,0.01881730492160061,1.1884672066630708,4.363900922679868,0.20830531716359846,2.69470312281179,3.876235749338262,0.24129157347034913,3.4815903361228906,3.1817940749478146,4.690829497684816,4.989782698706603,3.425879201850168,0.401626387335699,4.341468564753905,3.251478162812904,0.6970270064484563,1.2927989588822208,0.7024824953249514,4.3210890547188,4.399774537254491,2.7267861350327776,0.09628058468193423,3.6841049912399897,3.59951756460769,0.7195756908041023]}
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: kurtosis, 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 ] = kurtosis( LogNormal( mu[i], sigma[i] ) );
# The excess kurtosis is evaluated in extended precision using the textbook formula `exp(4*sigma^2) + 2*exp(3*sigma^2) + 3*exp(2*sigma^2) - 6`, as subtracting `6` in double precision cancels for small `sigma`.
z = setprecision( BigFloat, 2048 ) do
s2 = BigFloat.( sigma ) .^ 2;
Float64.( exp.( 4 .* s2 ) .+ ( 2 .* exp.( 3 .* s2 ) ) .+ ( 3 .* exp.( 2 .* s2 ) ) .- 6 );
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` (above `1e-154`, where `sigma^2` underflows):
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 @@ -22,16 +22,16 @@

var tape = require( 'tape' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var abs = require( '@stdlib/math/base/special/abs' );
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 kurtosis = require( './../lib' );


// FIXTURES //

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


// TESTS //
Expand Down Expand Up @@ -76,9 +76,7 @@ tape( 'if provided a nonpositive `sigma`, the function returns `NaN`', function

tape( 'the function returns the excess kurtosis of a lognormal distribution', function test( t ) {
var expected;
var delta;
var sigma;
var tol;
var mu;
var y;
var i;
Expand All @@ -89,13 +87,26 @@ tape( 'the function returns the excess kurtosis of a lognormal distribution', fu
for ( i = 0; i < mu.length; i++ ) {
y = kurtosis( mu[i], sigma[i] );
if ( expected[i] !== null ) {
if ( y === expected[i] ) {
t.strictEqual( y, expected[i], 'mu:'+mu[i]+', sigma: '+sigma[i]+', y: '+y+', expected: '+expected[i] );
} else {
delta = abs( y - expected[ i ] );
tol = 10.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. mu: '+mu[i]+'. sigma: '+sigma[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
t.strictEqual( isAlmostSameValue( y, expected[ i ], 61 ), true, 'returns expected value' );
}
}
t.end();
});

tape( 'the function returns the excess kurtosis 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 = kurtosis( mu[i], sigma[i] );
if ( expected[i] !== null ) {
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
}
t.end();
Expand Down
Loading
Loading