Skip to main content

SSB Coefficient Maker

Project description

SSB Coefficient Maker

PyPI Status Python Version License

Documentation Tests Coverage Quality Gate Status

pre-commit Black Ruff Poetry

Features

  • Arbitrary decimal precision support using mpmath for more accurate calculations
  • Validation system for detecting and handling invalid values (NaN, Inf, pd.NA)
  • Coefficient calculation based on formula definitions stored in a dataframe
  • Comprehensive error reporting with detailed diagnostics for debugging formulas
  • Support for mixed operations between DataFrames and Series
  • Configurable precision and error handling to suit different use cases
  • Flexible column naming in coefficient definition tables

Requirements

  • python >=3.10
  • click >=8.0.1
  • pandas >=2.2.3
  • numpy >=2.2.3
  • sympy >=1.13.3
  • mpmath >=1.3.0
  • pydantic >=2.10.6

Installation

You can install SSB Coefficient Maker via pip from PyPI:

pip install ssb-coefficient-maker

# or alternatively, if you're using poetry
poetry add ssb-coefficient-maker

Usage

Basic Formula Evaluation

The FormulaEvaluator allows you to evaluate mathematical expressions using pandas DataFrames and Series:

import pandas as pd
import numpy as np
from ssb_coefficient_maker import FormulaEvaluator

# Create some sample data
data = {
    'matrix_a': pd.DataFrame({
        'col1': [1.0, 2.0, 3.0],
        'col2': [4.0, 5.0, 6.0],
        'col3': [7.0, 8.0, 9.0],
    }),
    'vector_b': pd.Series([10.0, 20.0, 30.0])  # Note: length matches the number of columns in matrix_a
}

# Initialize the evaluator with default settings
evaluator = FormulaEvaluator(data)

# Evaluate a formula
result = evaluator.evaluate_formula('matrix_a * vector_b')
print(result)

This would produce output similar to:

     col1   col2   col3
0   10.0   80.0  210.0
1   20.0  100.0  240.0
2   30.0  120.0  270.0

Computing Multiple Coefficients

import pandas as pd
from ssb_coefficient_maker import CoefficientCalculator

# Create input data
data = {
    'input_matrix': pd.DataFrame({
        'A': [1.0, 2.0],
        'B': [3.0, 4.0]
    }),
    'adjustment': pd.Series([0.9, 1.1], index=['A', 'B'])  # Series with column names as index
}

# Define coefficient formulas
coef_map = pd.DataFrame({
    'coefficient_name': ['adjusted_matrix', 'squared_matrix'],
    'formula': ['input_matrix * adjustment', 'input_matrix * input_matrix']
})

# Create calculator with custom column names and safe settings
calculator = CoefficientCalculator(
    data,
    coef_map,
    result_name_col='coefficient_name',  # Specify which column contains result names
    formula_name_col='formula',          # Specify which column contains formulas
    adp_enabled=True,                    # Use arbitrary precision
    fill_invalid=True,                   # Replace invalid values with zeros
    verbose=True                         # Print detailed information during calculation
)

# Compute all coefficients
results = calculator.compute_coefficients()

# Access the results
adjusted = results['adjusted_matrix']
squared = results['squared_matrix']

Handling Division by Zero

import pandas as pd
from ssb_coefficient_maker import FormulaEvaluator

# Data with potential division by zero
data = {
    'numerator': pd.DataFrame({'A': [1.0, 2.0], 'B': [3.0, 4.0]}),
    'denominator': pd.DataFrame({'A': [1.0, 0.0], 'B': [0.0, 2.0]})
}

# Safe evaluator that replaces Inf/NaN with zeros
safe_eval = FormulaEvaluator(data, fill_invalid=True)
result = safe_eval.evaluate_formula('numerator / denominator')
print(result)

Output:

     A    B
0  1.0  0.0
1  0.0  2.0

Working with High Precision

import pandas as pd
from ssb_coefficient_maker import FormulaEvaluator

# Create data with fractions that produce repeating decimals
data = {
    'numerator': pd.Series([1, 2, 1]),
    'denominator': pd.Series([3, 3, 7])
}

# Compare precision differences in division operations
print("Arbitrary precision result (50 digits):")
high_prec = FormulaEvaluator(data, decimal_precision=50)
print(high_prec.evaluate_formula('numerator / denominator'))

print("\nStandard precision result (float64):")
std_prec = FormulaEvaluator(data, adp_enabled=False)
print(std_prec.evaluate_formula('numerator / denominator'))

The actual representation of these values would be:

# Arbitrary precision result (50 digits):
# Each value is stored as an mpmath.mpf object with 50 digits of precision
0    0.33333333333333333333333333333333333333333333333333
1    0.66666666666666666666666666666666666666666666666667
2    0.14285714285714285714285714285714285714285714285714
dtype: object

# Standard precision result (float64):
# Each value is stored as a 64-bit floating point number with ~15-17 significant digits
0    0.3333333333333333
1    0.6666666666666666
2    0.14285714285714285
dtype: float64

Please see the Reference Guide for more detailed examples and advanced usage.

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, SSB Coefficient Maker is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from Statistics Norway's SSB PyPI Template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ssb_coefficient_maker-1.0.2.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ssb_coefficient_maker-1.0.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file ssb_coefficient_maker-1.0.2.tar.gz.

File metadata

  • Download URL: ssb_coefficient_maker-1.0.2.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for ssb_coefficient_maker-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fc296bc71b76cece53e1acf1c4fb43d886cc98452de72dbb2e426de299f04d17
MD5 515669a02db1deb149e3dc8314b427ae
BLAKE2b-256 63018de7d2abdb13217a5a235586495389ecff171d70a34d221deee503038c25

See more details on using hashes here.

Provenance

The following attestation bundles were made for ssb_coefficient_maker-1.0.2.tar.gz:

Publisher: release.yml on statisticsnorway/ssb-coefficient-maker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ssb_coefficient_maker-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ssb_coefficient_maker-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c65f4e8f2ae21216e1f749c83d2f11e2c7899bddf88fd74a667f550ff0c8bbf2
MD5 d90b1848cb1f65d7d54599c6b8d5dd58
BLAKE2b-256 60c890a41848b7cd260d15bda63ffa380b0e24c49d4e059acb3cef30d9768385

See more details on using hashes here.

Provenance

The following attestation bundles were made for ssb_coefficient_maker-1.0.2-py3-none-any.whl:

Publisher: release.yml on statisticsnorway/ssb-coefficient-maker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page