Skip to main content

A Python library for filtering and stabilizing probability predictions from multiple models inspired by Rick and Morty

Project description

Probability Stasis Filter Library

A Python library for filtering and stabilizing probability predictions from multiple models.

Installation

Clone this repository and install locally or just pip install it:

pip install .

Usage

Basic Example

from probability_stasis import StasisFilter
import numpy as np

# Define a simple model
class DummyModel:
    def predict_proba(self, X):
        return np.random.random((len(X), 2))

# Initialize filter and model
filter = StasisFilter(threshold=0.1, window_size=3)
filter.add_model(DummyModel())

# Make predictions
X = np.random.random((5, 10))
predictions = filter.predict(X)
print(predictions[0])  # Filtered probabilities from model 0

API Documentation

StasisFilter

Main class for probability stasis filtering.

Parameters

  • threshold (float): Maximum allowed probability deviation (default: 0.1)
  • window_size (int): Number of predictions to track (default: 3)
  • models (List[PredictionModel], optional): Initial list of prediction models

Methods

  • add_model(model): Add a new prediction model
  • predict(X): Make filtered predictions
  • reset_history(): Clear prediction history

PredictionModel

Abstract base class for models. Must implement:

  • predict_proba(X): Return probability predictions as numpy array

Features

  • Maintains probability stability within threshold
  • Supports multiple models simultaneously
  • Rolling window of predictions
  • Automatic stabilization when thresholds are exceeded

example_usage.py

import numpy as np
from probability_stasis import StasisFilter

class SimpleModel:
    def predict_proba(self, X):
        return np.random.random((len(X), 2))

def main():
    # Initialize filter with two models
    filter = StasisFilter(threshold=0.05, window_size=4)
    filter.add_model(SimpleModel())
    filter.add_model(SimpleModel())
    
    # Generate sample data
    X = np.random.random((3, 5))
    
    # Make multiple predictions to demonstrate stasis
    for _ in range(5):
        preds = filter.predict(X)
        print(f"\nPredictions:")
        for model_idx, probs in preds.items():
            print(f"Model {model_idx}: {probs}")

if __name__ == "__main__":
    main()

How to Use the Library

  1. Installation:

    • Place all files in the directory structure as shown
    • Install using pip install . from the probability_stasis directory
  2. Basic Usage:

    • Create a model class that inherits from PredictionModel or implements predict_proba
    • Initialize StasisFilter with desired threshold and window size
    • Add models using add_model()
    • Call predict() with input data
  3. Key Features:

    • The filter maintains a history of predictions per model
    • Checks if new predictions deviate beyond the threshold from recent history
    • Stabilizes predictions by averaging when stasis is broken
    • Handles multiple models independently
  4. Customization:

    • Adjust threshold for sensitivity to probability changes
    • Modify window_size for how many past predictions to consider
    • Add as many models as needed

This implementation provides a robust, reusable library that can be integrated with various prediction models while ensuring probability stability across predictions. The example shows how to use it with dummy models, but you can replace these with real ML models (e.g., from scikit-learn) that implement the predict_proba method.

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

probability_stasis-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

probability_stasis-0.1.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file probability_stasis-0.1.0.tar.gz.

File metadata

  • Download URL: probability_stasis-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.11

File hashes

Hashes for probability_stasis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d2ec8c3b976b4055ec95f8b71f5a647a47382a5f58963791c28d74e11d1ab8b
MD5 598449b9c5481142dc38ef55fc73ad2b
BLAKE2b-256 fc1f91780faf21be294cd7dd1dea20f43ac045f67aa9289863fd5e5e46097604

See more details on using hashes here.

File details

Details for the file probability_stasis-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for probability_stasis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbcbd52844d8319bb6e3bb433cc7dec333c7660a64d55c69278aba9531e9eb79
MD5 39fb760f6f1a40a7cea4c32ccf4089ce
BLAKE2b-256 28946673cdd1f1ec694297179c35eb8628d62c3343b3ea90d18a887937fdd979

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