Skip to content

math.stats #

math.stats

correlation computes the Pearson coefficient for paired numeric samples and returns f64. Lengths must match; empty or constant input returns NaN. linear_regression returns a LinearRegression[T] with f64 coefficients and predictions, including for integer samples. It needs at least two paired samples with distinct x values.

quantiles(sorted_data, n) returns n - 1 cut points as []f64, using inclusive linear interpolation between ascending sorted samples. At least two samples and n >= 1 are required. multimode groups values by equality and returns all modes in first-seen order.

new_normal_dist(mean, std) requires finite parameters and std >= 0; zero denotes a point mass. pdf, cdf and inv_cdf evaluate that distribution. Inverse probabilities must lie strictly between zero and one. Extreme-tail inverse values use a finite rational approximation; ordinary inputs also receive a Halley refinement. A NaN evaluation input returns NaN.

Gaussian evaluation scales differences, densities, and affine inverse values to avoid intermediate overflow or underflow for finite parameters. Final values can still overflow or underflow when the mathematical result lies outside the floating-point range.

fn absdev #

@[inline]
fn absdev[T](data []T) T

absdev calculates the average distance between each data point and the mean Based on https://en.wikipedia.org/wiki/Average_absolute_deviation

fn absdev_mean #

fn absdev_mean[T](data []T, mean T) T

absdev_mean calculates the average distance between each data point and the provided mean Based on https://en.wikipedia.org/wiki/Average_absolute_deviation

fn correlation #

fn correlation[T](data1 []T, data2 []T) f64

correlation returns the Pearson correlation coefficient as f64, including for integer samples. Empty or constant input returns NaN; lengths must match.

fn covariance #

@[inline]
fn covariance[T](data1 []T, data2 []T) T

covariance calculates directional association between datasets positive value denotes variables move in same direction and negative denotes variables move in opposite directions

fn covariance_mean #

fn covariance_mean[T](data1 []T, data2 []T, mean1 T, mean2 T) T

covariance_mean computes the covariance of a dataset with means provided the recurrence relation

fn freq #

fn freq[T](data []T, val T) int

freq calculates the Measure of Occurrence Frequency of a given number Based on https://www.mathsisfun.com/data/frequency-distribution.html

fn geometric_mean #

fn geometric_mean[T](data []T) T

geometric_mean calculates the central tendency of the given input array, product(data)**1/data.len Based on https://www.mathsisfun.com/numbers/geometric-mean.html

fn harmonic_mean #

fn harmonic_mean[T](data []T) T

harmonic_mean calculates the reciprocal of the average of reciprocals of the given input array Based on https://www.mathsisfun.com/numbers/harmonic-mean.html

fn kurtosis #

@[inline]
fn kurtosis[T](data []T) T

kurtosis calculates the measure of the 'tailedness' of the data by finding mean and standard of deviation

fn kurtosis_mean_stddev #

fn kurtosis_mean_stddev[T](data []T, mean T, sd T) T

kurtosis_mean_stddev calculates the measure of the 'tailedness' of the data using the fourth moment the deviations, normalized by the sd

fn lag1_autocorrelation #

@[inline]
fn lag1_autocorrelation[T](data []T) T

lag1_autocorrelation_mean calculates the correlation between values that are one time period apart of a dataset, based on the mean

fn lag1_autocorrelation_mean #

fn lag1_autocorrelation_mean[T](data []T, mean T) T

lag1_autocorrelation_mean calculates the correlation between values that are one time period apart of a dataset, using the recurrence relation

fn linear_regression #

fn linear_regression[T](data1 []T, data2 []T) !LinearRegression[T]

linear_regression fits paired numeric samples by ordinary least squares. Fewer than two samples or identical x values return an error.

fn max #

fn max[T](data []T) T

max finds the maximum value from the dataset

fn max_index #

fn max_index[T](data []T) int

max_index finds the first index of the maximum value

fn mean #

fn mean[T](data []T) T

mean calculates the average of the given input array, sum(data)/data.len Based on https://www.mathsisfun.com/data/central-measures.html

fn median #

fn median[T](sorted_data []T) T

median returns the middlemost value of the given input array ( input array is assumed to be sorted ) Based on https://www.mathsisfun.com/data/central-measures.html

fn min #

fn min[T](data []T) T

min finds the minimum value from the dataset

fn min_index #

fn min_index[T](data []T) int

min_index finds the first index of the minimum value

fn minmax #

fn minmax[T](data []T) (T, T)

minmax finds the minimum and maximum value from the dataset

fn minmax_index #

fn minmax_index[T](data []T) (int, int)

minmax_index finds the first index of the minimum and maximum value

fn mode #

fn mode[T](data []T) T

mode calculates the highest occurring value of the given input array Based on https://www.mathsisfun.com/data/central-measures.html

fn multimode #

fn multimode[T](data []T) []T

multimode returns every value occurring with the highest frequency, in first-seen order. Values are grouped by equality, not by their string form.

fn new_normal_dist #

fn new_normal_dist(mean f64, std f64) !NormalDist

new_normal_dist creates a distribution with finite mean and std >= 0. A zero standard deviation represents a point mass at the mean.

fn population_stddev #

@[inline]
fn population_stddev[T](data []T) T

population_stddev calculates how spread out the dataset is Based on https://www.mathsisfun.com/data/standard-deviation.html

fn population_stddev_mean #

@[inline]
fn population_stddev_mean[T](data []T, mean T) T

population_stddev_mean calculates how spread out the dataset is, with the provide mean Based on https://www.mathsisfun.com/data/standard-deviation.html

fn population_variance #

@[inline]
fn population_variance[T](data []T) T

population_variance is the Measure of Dispersion / Spread of the given input array Based on https://www.mathsisfun.com/data/standard-deviation.html

fn population_variance_mean #

fn population_variance_mean[T](data []T, mean T) T

population_variance_mean is the Measure of Dispersion / Spread of the given input array, with the provided mean Based on https://www.mathsisfun.com/data/standard-deviation.html

fn quantile #

fn quantile[T](sorted_data []T, f T) !T

quantile calculates quantile points for more reference https://en.wikipedia.org/wiki/Quantile

fn quantiles #

fn quantiles[T](sorted_data []T, n int) ![]f64

quantiles returns n-1 cut points as f64 using inclusive linear interpolation between the ordered samples. Input must be sorted in ascending order.

fn range #

fn range[T](data []T) T

range calculates the difference between the min and max Range ( Maximum - Minimum ) of the given input array Based on https://www.mathsisfun.com/data/range.html

fn rms #

fn rms[T](data []T) T

rms, Root Mean Square, calculates the sqrt of the mean of the squares of the given input array Based on https://en.wikipedia.org/wiki/Root_mean_square

fn sample_stddev #

@[inline]
fn sample_stddev[T](data []T) T

Measure of Dispersion / Spread Sample Standard Deviation of the given input array Based on https://www.mathsisfun.com/data/standard-deviation.html

fn sample_stddev_mean #

@[inline]
fn sample_stddev_mean[T](data []T, mean T) T

Measure of Dispersion / Spread Sample Standard Deviation of the given input array Based on https://www.mathsisfun.com/data/standard-deviation.html

fn sample_variance #

@[inline]
fn sample_variance[T](data []T) T

sample_variance calculates the spread of dataset around the mean Based on https://www.mathsisfun.com/data/standard-deviation.html

fn sample_variance_mean #

fn sample_variance_mean[T](data []T, mean T) T

sample_variance calculates the spread of dataset around the provided mean Based on https://www.mathsisfun.com/data/standard-deviation.html

fn skew #

@[inline]
fn skew[T](data []T) T

skew calculates the mean and standard of deviation to find the skew from the data

fn skew_mean_stddev #

fn skew_mean_stddev[T](data []T, mean T, sd T) T

skew_mean_stddev calculates the skewness of data

fn tss #

@[inline]
fn tss[T](data []T) T

tts, Sum of squares, calculates the sum over all squared differences between values and overall mean

fn tss_mean #

fn tss_mean[T](data []T, mean T) T

tts_mean, Sum of squares, calculates the sum over all squared differences between values and the provided mean

fn (LinearRegression[T]) predict #

fn (lr &LinearRegression[T]) predict(x T) f64

predict returns the fitted value at x as f64.

struct LinearRegression #

struct LinearRegression[T] {
pub:
	intercept f64
	slope     f64
}

LinearRegression is the least-squares fit of y = intercept + slope * x. Coefficients are f64 even when the input samples are integers.

struct NormalDist #

struct NormalDist {
pub:
	mean f64
	std  f64
}

NormalDist is the normal (Gaussian) distribution with the given mean and standard deviation, the V-idiomatic equivalent of Python's statistics.NormalDist.

fn (NormalDist) pdf #

fn (nd &NormalDist) pdf(x f64) f64

pdf returns the probability density at x.

fn (NormalDist) cdf #

fn (nd &NormalDist) cdf(x f64) f64

cdf returns the cumulative probability P(X <= x).

fn (NormalDist) inv_cdf #

fn (nd &NormalDist) inv_cdf(p f64) !f64

inv_cdf returns the value x with P(X <= x) == p (p must be in (0, 1)). It uses the Acklam inverse-normal approximation followed by one Halley refinement step where its exponential remains finite.