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
77 changes: 72 additions & 5 deletions lib/node_modules/@stdlib/ndarray/base/assert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
Copyright (c) 2024 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.
Expand Down Expand Up @@ -86,10 +86,77 @@ var o = ns;
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/ndarray/base/assert' );

console.log( objectKeys( ns ) );
var Float64Array = require('@stdlib/array/float64');
var ndarray = require('@stdlib/ndarray').ndarray;
var randn = require('@stdlib/random/base/randn');
var sqrt = require('@stdlib/math/base/special/sqrt');
var pow = require('@stdlib/math/base/special/pow');
var erf = require('@stdlib/math/base/special/erf');
var cdf = require('@stdlib/stats/base/dists/normal/cdf');
var isContiguous = require('@stdlib/ndarray/base/assert/is-contiguous');

/*
* Let's take an example of generating a 5x5 multidimensional array filled with random numbers from a normal distribution.
* We want to calculate some statistical properties of this data, such as the mean and variance, and use some special mathematical functions to analyze the data further.
*/

var varianceValue;
var meanValue;
var sdValue;
var erfValue;
var cdfValue;
var strides;
var arrLen;
var shape;
var data;
var array;
var i;

function calculateVariance(acc, val) {
return acc + pow(val - meanValue, 2);
}

function calculateMean(acc, val) {
return acc + val;
}

shape = [5, 5];
data = new Float64Array(shape[0] * shape[1]);
for (i = 0; i < data.length; i++) {
// Fill the array with random numbers from a normal distribution.
data[i] = randn();
}

strides = [shape[1], 1];
array = ndarray('float64', data, shape, strides, 0, 'row-major');

// Check if the array is contiguous and of the correct data type
if (isContiguous(array.shape, array.strides, array.offset)) {
// Calculate the mean of the array data which gives us a measure of the central tendency of the data.
arrLen = array.data.length;
meanValue = array.data.reduce(calculateMean, 0) / arrLen;

// Calculate the variance of the array data which gives us a measure of how much the data varies from the mean.
varianceValue = array.data.reduce(calculateVariance, 0) / arrLen;

// Calculate the standard deviation of the array data which, being the square root of the variance, gives us a measure of the spread of the data around the mean.
sdValue = sqrt(varianceValue);

// Calculate the error function at the mean. The error function is used in statistics to find probabilities in normal distributions.
erfValue = erf(meanValue);

// Calculate the cumulative distribution function at the mean which gives us the probability that a random variable is less than or equal to a certain value.
cdfValue = cdf(meanValue, 0, sdValue);

console.log(true);
console.log(meanValue);
console.log(varianceValue);
console.log(sdValue);
console.log(erfValue);
console.log(cdfValue);
} else {
console.log(false);
}
```

</section>
Expand Down
75 changes: 71 additions & 4 deletions lib/node_modules/@stdlib/ndarray/base/assert/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -18,7 +18,74 @@

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( './../lib' );
var Float64Array = require('@stdlib/array/float64');
var ndarray = require('@stdlib/ndarray').ndarray;
var randn = require('@stdlib/random/base/randn');
var sqrt = require('@stdlib/math/base/special/sqrt');
var pow = require('@stdlib/math/base/special/pow');
var erf = require('@stdlib/math/base/special/erf');
var cdf = require('@stdlib/stats/base/dists/normal/cdf');
var isContiguous = require( './../is-contiguous/lib' );

console.log( objectKeys( ns ) );
/*
* Let's take an example of generating a 5x5 multidimensional array filled with random numbers from a normal distribution.
* We want to calculate some statistical properties of this data, such as the mean and variance, and use some special mathematical functions to analyze the data further.
*/

var varianceValue;
var meanValue;
var sdValue;
var erfValue;
var cdfValue;
var strides;
var arrLen;
var shape;
var data;
var array;
var i;

function calculateVariance(acc, val) {
return acc + pow(val - meanValue, 2);
}

function calculateMean(acc, val) {
return acc + val;
}

shape = [5, 5];
data = new Float64Array(shape[0] * shape[1]);
for (i = 0; i < data.length; i++) {
// Fill the array with random numbers from a normal distribution.
data[i] = randn();
}

strides = [shape[1], 1];
array = ndarray('float64', data, shape, strides, 0, 'row-major');

// Check if the array is contiguous and of the correct data type
if (isContiguous(array.shape, array.strides, array.offset)) {
// Calculate the mean of the array data which gives us a measure of the central tendency of the data.
arrLen = array.data.length;
meanValue = array.data.reduce(calculateMean, 0) / arrLen;

// Calculate the variance of the array data which gives us a measure of how much the data varies from the mean.
varianceValue = array.data.reduce(calculateVariance, 0) / arrLen;

// Calculate the standard deviation of the array data which, being the square root of the variance, gives us a measure of the spread of the data around the mean.
sdValue = sqrt(varianceValue);

// Calculate the error function at the mean. The error function is used in statistics to find probabilities in normal distributions.
erfValue = erf(meanValue);

// Calculate the cumulative distribution function at the mean which gives us the probability that a random variable is less than or equal to a certain value.
cdfValue = cdf(meanValue, 0, sdValue);

console.log(true);
console.log(meanValue);
console.log(varianceValue);
console.log(sdValue);
console.log(erfValue);
console.log(cdfValue);
} else {
console.log(false);
}