From 8d077c487cd2d63d7806c434bb1bfefaa963cb61 Mon Sep 17 00:00:00 2001 From: olenkabilonizhka Date: Sun, 7 Apr 2024 16:56:17 +0300 Subject: [PATCH 1/2] feat: Improve README examples of stats/base/dists/gumbel --- .../@stdlib/stats/base/dists/gumbel/README.md | 63 +++++++++++++++++- .../stats/base/dists/gumbel/examples/index.js | 65 ++++++++++++++++++- 2 files changed, 123 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md b/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md index c1f940fa3f94..cc7db7544f73 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md @@ -109,10 +109,69 @@ var y = dist.pdf( 2.0 ); ```javascript -var objectKeys = require( '@stdlib/utils/keys' ); var gumbel = require( '@stdlib/stats/base/dists/gumbel' ); -console.log( objectKeys( gumbel ) ); +var x = 6.0; +var t = -0.3; +var p = 0.7; +var mu = 2.0; +var beta = 8.0; + +// Gumbel distribution cumulative distribution function. +console.log(gumbel.cdf( x, mu, beta )); +// => ~0.545 + +// Gumbel distribution logarithm of cumulative distribution function. +console.log(gumbel.logcdf( x, mu, beta )); +// => ~-0.607 + +// Gumbel distribution logarithm of probability density function (PDF). +console.log(gumbel.logpdf( x, mu, beta )); +// => ~-3.186 + +// Gumbel distribution moment-generating function (MGF). +console.log(gumbel.mgf( t, mu, beta )); +// => ~1.636 + +// Gumbel distribution probability density function (PDF). +console.log(gumbel.pdf( x, mu, beta )); +// => ~0.041 + +// Gumbel distribution quantile function. +console.log(gumbel.quantile( p, mu, beta )); +// => ~10.247 + +// Gumbel distribution differential entropy. +console.log(gumbel.entropy( mu, beta )); +// => ~3.657 + +// Gumbel distribution excess kurtosis. +console.log(gumbel.kurtosis( mu, beta )); +// => 2.4 + +// Gumbel distribution expected value. +console.log(gumbel.mean( mu, beta )); +// => ~6.618 + +// Gumbel distribution median. +console.log(gumbel.median( mu, beta )); +// => ~4.932 + +// Gumbel distribution mode. +console.log(gumbel.mode( mu, beta )); +// => 2 + +// Gumbel distribution skewness. +console.log(gumbel.skewness( mu, beta )); +// => ~1.14 + +// Gumbel distribution standard deviation. +console.log(gumbel.stdev( mu, beta )); +// => ~10.26 + +// Gumbel distribution variance. +console.log(gumbel.variance( mu, beta )); +// => ~105.276 ``` diff --git a/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js b/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js index 42a35a8968b1..7e641c852690 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js @@ -18,7 +18,66 @@ 'use strict'; -var objectKeys = require( '@stdlib/utils/keys' ); -var gumbel = require( './../lib' ); +var gumbel = require('./../lib'); -console.log( objectKeys( gumbel ) ); +var x = 6.0; +var t = -0.3; +var p = 0.7; +var mu = 2.0; +var beta = 8.0; + +// Gumbel distribution cumulative distribution function. +console.log(gumbel.cdf(x, mu, beta)); +// => ~0.545 + +// Gumbel distribution logarithm of cumulative distribution function. +console.log(gumbel.logcdf(x, mu, beta)); +// => ~-0.607 + +// Gumbel distribution logarithm of probability density function (PDF). +console.log(gumbel.logpdf(x, mu, beta)); +// => ~-3.186 + +// Gumbel distribution moment-generating function (MGF). +console.log(gumbel.mgf(t, mu, beta)); +// => ~1.636 + +// Gumbel distribution probability density function (PDF). +console.log(gumbel.pdf(x, mu, beta)); +// => ~0.041 + +// Gumbel distribution quantile function. +console.log(gumbel.quantile(p, mu, beta)); +// => ~10.247 + +// Gumbel distribution differential entropy. +console.log(gumbel.entropy(mu, beta)); +// => ~3.657 + +// Gumbel distribution excess kurtosis. +console.log(gumbel.kurtosis(mu, beta)); +// => 2.4 + +// Gumbel distribution expected value. +console.log(gumbel.mean(mu, beta)); +// => ~6.618 + +// Gumbel distribution median. +console.log(gumbel.median(mu, beta)); +// => ~4.932 + +// Gumbel distribution mode. +console.log(gumbel.mode(mu, beta)); +// => 2 + +// Gumbel distribution skewness. +console.log(gumbel.skewness(mu, beta)); +// => ~1.14 + +// Gumbel distribution standard deviation. +console.log(gumbel.stdev(mu, beta)); +// => ~10.26 + +// Gumbel distribution variance. +console.log(gumbel.variance(mu, beta)); +// => ~105.276 From ca578a5e8aae8f2a1bd0a4319d8e099038454c6c Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 23 Sep 2024 20:32:55 -0400 Subject: [PATCH 2/2] chore: rewrite examples --- .../@stdlib/stats/base/dists/gumbel/README.md | 112 ++++++++--------- .../stats/base/dists/gumbel/examples/index.js | 116 ++++++++---------- 2 files changed, 104 insertions(+), 124 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md b/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md index cc7db7544f73..f0c210c99ed4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md +++ b/lib/node_modules/@stdlib/stats/base/dists/gumbel/README.md @@ -109,69 +109,59 @@ var y = dist.pdf( 2.0 ); ```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var filledarrayBy = require('@stdlib/array/filled-by'); +var mean = require( '@stdlib/stats/base/mean' ); +var variance = require( '@stdlib/stats/base/variance' ); +var stdev = require( '@stdlib/stats/base/stdev' ); +var randGumbel = require( '@stdlib/random/base/gumbel' ).factory; var gumbel = require( '@stdlib/stats/base/dists/gumbel' ); -var x = 6.0; -var t = -0.3; -var p = 0.7; -var mu = 2.0; -var beta = 8.0; - -// Gumbel distribution cumulative distribution function. -console.log(gumbel.cdf( x, mu, beta )); -// => ~0.545 - -// Gumbel distribution logarithm of cumulative distribution function. -console.log(gumbel.logcdf( x, mu, beta )); -// => ~-0.607 - -// Gumbel distribution logarithm of probability density function (PDF). -console.log(gumbel.logpdf( x, mu, beta )); -// => ~-3.186 - -// Gumbel distribution moment-generating function (MGF). -console.log(gumbel.mgf( t, mu, beta )); -// => ~1.636 - -// Gumbel distribution probability density function (PDF). -console.log(gumbel.pdf( x, mu, beta )); -// => ~0.041 - -// Gumbel distribution quantile function. -console.log(gumbel.quantile( p, mu, beta )); -// => ~10.247 - -// Gumbel distribution differential entropy. -console.log(gumbel.entropy( mu, beta )); -// => ~3.657 - -// Gumbel distribution excess kurtosis. -console.log(gumbel.kurtosis( mu, beta )); -// => 2.4 - -// Gumbel distribution expected value. -console.log(gumbel.mean( mu, beta )); -// => ~6.618 - -// Gumbel distribution median. -console.log(gumbel.median( mu, beta )); -// => ~4.932 - -// Gumbel distribution mode. -console.log(gumbel.mode( mu, beta )); -// => 2 - -// Gumbel distribution skewness. -console.log(gumbel.skewness( mu, beta )); -// => ~1.14 - -// Gumbel distribution standard deviation. -console.log(gumbel.stdev( mu, beta )); -// => ~10.26 - -// Gumbel distribution variance. -console.log(gumbel.variance( mu, beta )); -// => ~105.276 +// Set the parameters of the Gumbel distribution: +var mu = 30.0; // Location parameter (e.g., average annual maximum temperature in °C) +var beta = 5.0; // Scale parameter + +// Simulate annual maximum daily temperatures over 1000 years: +var N = 1000; +var rgumbel = randGumbel( mu, beta ); +var maxTemperatures = filledarrayBy( N, 'float64', rgumbel ); + +// Compute theoretical statistics of the Gumbel distribution: +var theoreticalMean = gumbel.mean( mu, beta); +var theoreticalVariance = gumbel.variance( mu, beta ); +var theoreticalStdev = gumbel.stdev( mu, beta ); + +// Compute sample statistics of the simulated data: +var sampleMean = mean( N, maxTemperatures, 1 ); +var sampleVariance = variance( N, 1, maxTemperatures, 1 ); // with Bessel's correction +var sampleStdev = stdev( N, 1, maxTemperatures, 1 ); // with Bessel's correction + +// Display theoretical and sample statistics: +console.log( '--- Statistical Comparison ---\n' ); +console.log( 'Mean:'); +console.log( ' Theoretical: %d°C', theoreticalMean.toFixed(2) ); +console.log( ' Sample: %d°C\n', sampleMean.toFixed(2) ); +console.log( 'Variance:'); +console.log( ' Theoretical: %d°C²', theoreticalVariance.toFixed(2) ); +console.log( ' Sample: %d°C²\n', sampleVariance.toFixed(2) ); +console.log( 'Standard Deviation:' ); +console.log( ' Theoretical: %d°C', theoreticalStdev.toFixed(2) ); +console.log( ' Sample: %d°C\n', sampleStdev.toFixed(2) ); + +// Define quantile probabilities: +var p = new Float64Array( [ 0.25, 0.5, 0.75 ] ); +var label = [ 'First Quartile', 'Median', 'Third Quartile' ]; +var theoreticalQuantiles = new Float64Array([ + gumbel.quantile( p[0], mu, beta ), + gumbel.quantile( p[1], mu, beta ), + gumbel.quantile( p[2], mu, beta ) +]); + +console.log( 'Quantiles:' ); +var i; +for ( i = 0; i < p.length; i++ ) { + console.log( label[i] + ': %d°C', theoreticalQuantiles[i].toFixed(2) ); +} ``` diff --git a/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js b/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js index 7e641c852690..c17d6c4996ec 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/dists/gumbel/examples/index.js @@ -18,66 +18,56 @@ 'use strict'; -var gumbel = require('./../lib'); - -var x = 6.0; -var t = -0.3; -var p = 0.7; -var mu = 2.0; -var beta = 8.0; - -// Gumbel distribution cumulative distribution function. -console.log(gumbel.cdf(x, mu, beta)); -// => ~0.545 - -// Gumbel distribution logarithm of cumulative distribution function. -console.log(gumbel.logcdf(x, mu, beta)); -// => ~-0.607 - -// Gumbel distribution logarithm of probability density function (PDF). -console.log(gumbel.logpdf(x, mu, beta)); -// => ~-3.186 - -// Gumbel distribution moment-generating function (MGF). -console.log(gumbel.mgf(t, mu, beta)); -// => ~1.636 - -// Gumbel distribution probability density function (PDF). -console.log(gumbel.pdf(x, mu, beta)); -// => ~0.041 - -// Gumbel distribution quantile function. -console.log(gumbel.quantile(p, mu, beta)); -// => ~10.247 - -// Gumbel distribution differential entropy. -console.log(gumbel.entropy(mu, beta)); -// => ~3.657 - -// Gumbel distribution excess kurtosis. -console.log(gumbel.kurtosis(mu, beta)); -// => 2.4 - -// Gumbel distribution expected value. -console.log(gumbel.mean(mu, beta)); -// => ~6.618 - -// Gumbel distribution median. -console.log(gumbel.median(mu, beta)); -// => ~4.932 - -// Gumbel distribution mode. -console.log(gumbel.mode(mu, beta)); -// => 2 - -// Gumbel distribution skewness. -console.log(gumbel.skewness(mu, beta)); -// => ~1.14 - -// Gumbel distribution standard deviation. -console.log(gumbel.stdev(mu, beta)); -// => ~10.26 - -// Gumbel distribution variance. -console.log(gumbel.variance(mu, beta)); -// => ~105.276 +var Float64Array = require( '@stdlib/array/float64' ); +var filledarrayBy = require('@stdlib/array/filled-by'); +var mean = require( '@stdlib/stats/base/mean' ); +var variance = require( '@stdlib/stats/base/variance' ); +var stdev = require( '@stdlib/stats/base/stdev' ); +var randGumbel = require( '@stdlib/random/base/gumbel' ).factory; +var gumbel = require( './../lib' ); + +// Set the parameters of the Gumbel distribution: +var mu = 30.0; // Location parameter (e.g., average annual maximum temperature in °C) +var beta = 5.0; // Scale parameter + +// Simulate annual maximum daily temperatures over 1000 years: +var N = 1000; +var rgumbel = randGumbel( mu, beta ); +var maxTemperatures = filledarrayBy( N, 'float64', rgumbel ); + +// Compute theoretical statistics of the Gumbel distribution: +var theoreticalMean = gumbel.mean( mu, beta); +var theoreticalVariance = gumbel.variance( mu, beta ); +var theoreticalStdev = gumbel.stdev( mu, beta ); + +// Compute sample statistics of the simulated data: +var sampleMean = mean( N, maxTemperatures, 1 ); +var sampleVariance = variance( N, 1, maxTemperatures, 1 ); // with Bessel's correction +var sampleStdev = stdev( N, 1, maxTemperatures, 1 ); // with Bessel's correction + +// Display theoretical and sample statistics: +console.log( '--- Statistical Comparison ---\n' ); +console.log( 'Mean:'); +console.log( ' Theoretical: %d°C', theoreticalMean.toFixed(2) ); +console.log( ' Sample: %d°C\n', sampleMean.toFixed(2) ); +console.log( 'Variance:'); +console.log( ' Theoretical: %d°C²', theoreticalVariance.toFixed(2) ); +console.log( ' Sample: %d°C²\n', sampleVariance.toFixed(2) ); +console.log( 'Standard Deviation:' ); +console.log( ' Theoretical: %d°C', theoreticalStdev.toFixed(2) ); +console.log( ' Sample: %d°C\n', sampleStdev.toFixed(2) ); + +// Define quantile probabilities: +var p = new Float64Array( [ 0.25, 0.5, 0.75 ] ); +var label = [ 'First Quartile', 'Median', 'Third Quartile' ]; +var theoreticalQuantiles = new Float64Array([ + gumbel.quantile( p[0], mu, beta ), + gumbel.quantile( p[1], mu, beta ), + gumbel.quantile( p[2], mu, beta ) +]); + +console.log( 'Quantiles:' ); +var i; +for ( i = 0; i < p.length; i++ ) { + console.log( label[i] + ': %d°C', theoreticalQuantiles[i].toFixed(2) ); +}