#monetary #statistical #mean #median #ratio #amounts #negative

nightly tktax-money

Statistical and algebraic operations on monetary amounts for the tktax project

1 unstable release

new 0.2.2 Feb 1, 2025

#8 in #amounts


Used in 14 crates (13 directly)

MIT/Apache

76KB
1K SLoC

tktax-money

A specialized Rust library for advanced monetary computations in taxation and financial analysis.
It integrates sophisticated mathematical and statistical routines (e.g. mean, median, standard deviation, skewness, kurtosis, correlation, linear regression) tailored to currency-valued data types.

Features

  • MonetaryAmount type based on rust_decimal::Decimal, preventing floating-point anomalies.
  • Basic Statistics (BasicStats): mean, median, standard deviation, repeated-value detection, min/max, midrange.
  • Advanced Statistics (AdvancedMoneyStats): quartiles, IQR-based outlier detection, variance, skewness (ἔγκλισις), kurtosis (κύρτωσις), cumulative frequency.
  • Dimensionless ratio types for statistical coefficients and correlation (correlatio, συσχέτισις).
  • Safe Arithmetic: checks for division-by-zero, negative square roots, and large exponentiations.
  • Segmenting: flexible grouping of monetary data based on user-defined lambdas.

Usage

Add this to your Cargo.toml:

[dependencies]
tktax-money = { git = "https://github.com/klebs6/tktax", subdir = "tktax-money" }

Example for basic stats:

// Suppose you already have a slice of MonetaryAmount named `amounts`:
match amounts.mean() {
    Ok(avg) => println!("Average transaction amount: {}", avg),
    Err(e)  => eprintln!("Error computing mean: {:?}", e),
}

// Finding repeated values (duplicates above count=1):
if let Ok(repeats) = amounts.repeated_values() {
    for (val, count) in repeats {
        println!("{} repeats {} times", val, count);
    }
}

Advanced analytics example (quartiles, outliers):

// quartiles_iqr => (q1, median, q3, iqr)
if let Ok((q1, median, q3, iqr)) = amounts.quartiles_iqr() {
    println!("Q1 = {}, Median = {}, Q3 = {}, IQR = {}", q1, median, q3, iqr);

    // Outlier detection
    if let Ok(outliers) = amounts.outliers_iqr() {
        println!("Found {} outliers", outliers.len());
    }
}

Dimensionless correlation:

// correlation => dimensionless measure of linear association
let x_data = vec![MonetaryAmount::from(1), MonetaryAmount::from(2)];
let y_data = vec![MonetaryAmount::from(2), MonetaryAmount::from(4)];
let corr = x_data.as_slice().correlation(&y_data);
println!("Correlation: {:?}", corr);

For more examples, review the tests within this crate (cargo test).

Contributing

  1. Fork the repository and create a new branch for your feature/fix.
  2. Implement, ensuring that you handle numerical edge cases safely.
  3. Open a Pull Request describing your changes.

License

Licensed under either of:

  • Apache License, Version 2.0
  • MIT License

at your option. See LICENSE for details.

Dependencies

~26–37MB
~641K SLoC