SSB Coefficient Maker
Project description
SSB Coefficient Maker
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc296bc71b76cece53e1acf1c4fb43d886cc98452de72dbb2e426de299f04d17
|
|
| MD5 |
515669a02db1deb149e3dc8314b427ae
|
|
| BLAKE2b-256 |
63018de7d2abdb13217a5a235586495389ecff171d70a34d221deee503038c25
|
Provenance
The following attestation bundles were made for ssb_coefficient_maker-1.0.2.tar.gz:
Publisher:
release.yml on statisticsnorway/ssb-coefficient-maker
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ssb_coefficient_maker-1.0.2.tar.gz -
Subject digest:
fc296bc71b76cece53e1acf1c4fb43d886cc98452de72dbb2e426de299f04d17 - Sigstore transparency entry: 183303213
- Sigstore integration time:
-
Permalink:
statisticsnorway/ssb-coefficient-maker@a8433ced648ac63093ab1f7862da0232f50c6e37 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/statisticsnorway
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a8433ced648ac63093ab1f7862da0232f50c6e37 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ssb_coefficient_maker-1.0.2-py3-none-any.whl.
File metadata
- Download URL: ssb_coefficient_maker-1.0.2-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c65f4e8f2ae21216e1f749c83d2f11e2c7899bddf88fd74a667f550ff0c8bbf2
|
|
| MD5 |
d90b1848cb1f65d7d54599c6b8d5dd58
|
|
| BLAKE2b-256 |
60c890a41848b7cd260d15bda63ffa380b0e24c49d4e059acb3cef30d9768385
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ssb_coefficient_maker-1.0.2-py3-none-any.whl -
Subject digest:
c65f4e8f2ae21216e1f749c83d2f11e2c7899bddf88fd74a667f550ff0c8bbf2 - Sigstore transparency entry: 183303216
- Sigstore integration time:
-
Permalink:
statisticsnorway/ssb-coefficient-maker@a8433ced648ac63093ab1f7862da0232f50c6e37 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/statisticsnorway
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a8433ced648ac63093ab1f7862da0232f50c6e37 -
Trigger Event:
push
-
Statement type: