Skip to main content

A Python package that intelligently selects the most robust average (mean, median, or mode) for price analysis

Project description

Robust Average

A Python package that intelligently selects the most robust average (mean, median, or mode) for price analysis based on outlier and skewness detection.

Installation

pip install robust-average

Quick Start

from robust_average import robust_average

# Example with clean data
prices = [97.87, 109.99, 129.99, 89.99, 119.99]
result = robust_average(prices)
print(f"Selected average: {result['value']} (method: {result['method']})")
# Output: Selected average: 109.99 (method: mean)

# Example with outliers
prices_with_outlier = [97.87, 109.99, 129.99, 89.99, 119.99, 500.00]
result = robust_average(prices_with_outlier)
print(f"Selected average: {result['value']} (method: {result['method']})")
# Output: Selected average: 109.99 (method: median)

Features

  • Automatic Method Selection: Intelligently chooses between mean, median, or mode
  • Outlier Detection: Uses IQR method to identify and handle outliers
  • Skewness Analysis: Measures data distribution asymmetry
  • Transparent Results: Returns the method used and supporting statistics
  • Business Ready: Ensures accurate price reporting for contracts and compliance

How It Works

The function uses a systematic approach to select the most appropriate average:

  1. Outlier Detection (IQR Method):

    • Calculates Q1 (25th percentile) and Q3 (75th percentile)
    • Defines outliers as values outside [Q1 - 1.5×IQR, Q3 + 1.5×IQR]
  2. Skewness Analysis:

    • Calculates skewness coefficient using scipy.stats.skew()
    • Values close to 0 indicate symmetric distribution
  3. Decision Criteria:

    • Use MEAN if: No outliers AND |skewness| < 0.5
    • Use MEDIAN if: Outliers present OR |skewness| ≥ 0.5
    • Use MODE if: A single value appears in >50% of the dataset

🛠️ Process Flow

flowchart TD
    A["Start: Input prices (list or Series)"]
    B["Convert to pandas Series, drop NaNs"]
    C["Calculate statistics:\n- Mean\n- Median\n- Mode\n- Std\n- Skewness"]
    D["Detect outliers using IQR"]
    E{"Outliers present or |skew| >= 0.5?"}
    F["Use Median as robust average"]
    G["No outliers and |skew| < 0.5"]
    H["Use Mean as robust average"]
    I{"Is mode dominant? (>50% values)"}
    J["Use Mode as robust average"]
    K["Return selected value, method, and stats"]

    A --> B --> C --> D --> E
    E -- Yes --> F --> K
    E -- No --> G --> I
    I -- Yes --> J --> K
    I -- No --> H --> K

API Reference

robust_average(prices, return_all_stats=False)

Parameters:

  • prices (list or pd.Series): List or Series of numeric prices
  • return_all_stats (bool): If True, returns all computed statistics

Returns:

{
    'value': selected_average_value,
    'method': 'mean' | 'median' | 'mode',
    'mean': mean_value,
    'median': median_value,
    'mode': mode_value_or_None,
    'std': standard_deviation,
    'skew': skewness,
    'outliers': list_of_outlier_values,
    'count': number_of_prices
}

Use Cases

  • Price Analysis: Ensure accurate average prices for reporting
  • Contract Negotiations: Use defensible statistics in pricing discussions
  • Compliance Reporting: Meet regulatory requirements with robust averages
  • Data Quality: Automatically handle messy, real-world price data

Requirements

  • Python >= 3.8
  • numpy >= 1.20.0
  • pandas >= 1.3.0
  • scipy >= 1.7.0

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

robust_average-0.1.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

robust_average-0.1.5-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file robust_average-0.1.5.tar.gz.

File metadata

  • Download URL: robust_average-0.1.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for robust_average-0.1.5.tar.gz
Algorithm Hash digest
SHA256 993b62da9e1247b05bc85b07e8e4ce1fa696270232f694b2abd6094b49994e26
MD5 4f35da2aec857cc44b1373cdbf2e2cfa
BLAKE2b-256 75d919fbc8e4110b3c81ec222377e80c07e4891e9b827f0c2f320986631c56bb

See more details on using hashes here.

File details

Details for the file robust_average-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: robust_average-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for robust_average-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0df3876a2d714482a7c822e9aec39323f5bb7b7b25aeb435bf54819fa48f2d7e
MD5 e452c4670555af7036803c90643da861
BLAKE2b-256 8ed4a56853e8881429447b023175cd6639bc0b86321973550ed6513c0b8e561d

See more details on using hashes here.

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