Skip to content

usptact/ZeroInflatedPoisson-Infer.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zero-Inflated Poisson Model with Infer.NET

A demonstration of implementing and inferring parameters for a Zero-Inflated Poisson (ZIP) model using Microsoft's Infer.NET probabilistic programming framework on .NET 8.0.

Overview

This project showcases Bayesian inference for a Zero-Inflated Poisson distribution, a statistical model commonly used when count data contains an excess of zeros beyond what a standard Poisson distribution would predict.

What is a Zero-Inflated Poisson Model?

A Zero-Inflated Poisson (ZIP) model is useful for modeling count data with excess zeros. The model assumes that the data generating process has two components:

  1. Structural Zeros: Generated by a Bernoulli process (coin flip) that produces zeros with probability π
  2. Count Process: When the Bernoulli outcome is "tails," a sample is drawn from a Poisson(λ) distribution, which may also produce zeros

This is analogous to deciding whether to visit a store (coin flip). If you don't go (heads), you buy 0 items. If you do go (tails), the number of items purchased follows a Poisson distribution (which could still be 0 if the item is out of stock).

The Model

The generative story for each data point:

  1. Flip a weighted coin with bias π (probability of heads)
  2. If heads: return 0 (structural zero)
  3. If tails: sample from Poisson(λ) distribution (count process)

Parameters to Infer:

  • π (pi): The probability of generating a structural zero (coin bias)
  • λ (lambda): The mean of the Poisson distribution

Prior Distributions:

  • π ~ Beta(1, 1) - uniform prior over [0, 1]
  • λ ~ Gamma(1, 4) - weakly informative prior for the Poisson rate

The model uses Expectation Propagation (EP) as the inference algorithm to compute posterior distributions for both parameters.

Prerequisites

Dependencies

The project uses the following NuGet packages:

  • Microsoft.ML.Probabilistic (v0.4.2301.301) - Core probabilistic programming framework
  • Microsoft.ML.Probabilistic.Compiler (v0.4.2301.301) - Compilation support for model inference

These are automatically restored when building the project.

Building the Project

Using .NET CLI

  1. Clone the repository:

    git clone https://github.com/yourusername/ZeroInflatedPoisson-Infer.NET.git
    cd ZeroInflatedPoisson-Infer.NET
  2. Restore dependencies:

    dotnet restore
  3. Build the project:

    dotnet build

    For a release build:

    dotnet build -c Release

Using Visual Studio

  1. Open ZeroInflatedPoisson.sln in Visual Studio 2022
  2. Press Ctrl+Shift+B to build the solution
  3. Dependencies will be restored automatically

Running the Application

Using .NET CLI

From the repository root:

dotnet run --project ZeroInflatedPoisson

Or navigate to the project directory:

cd ZeroInflatedPoisson
dotnet run

Using Visual Studio

  1. Set ZeroInflatedPoisson as the startup project (if not already)
  2. Press F5 to run with debugging, or Ctrl+F5 to run without debugging

Expected Output

The program will:

  1. Generate 1000 synthetic data points from a ZIP model with:
    • True π = 0.3 (30% probability of structural zeros)
    • True λ = 2.0 (Poisson mean)
  2. Perform Bayesian inference to estimate these parameters
  3. Display the posterior means:
    E(pi) = 0.299...
    E(lambda) = 2.01...
    Press any key ...
    

The inferred values should be close to the true parameters used to generate the data.

Project Structure

ZeroInflatedPoisson-Infer.NET/
├── ZeroInflatedPoisson/
│   ├── Program.cs                  # Main application code
│   └── ZeroInflatedPoisson.csproj  # Project file (.NET 8.0)
├── ZeroInflatedPoisson.sln         # Solution file
├── README.md                       # This file
└── LICENSE                         # MIT License

Understanding the Code

Key Components

  1. Data Generation (SampleData method):

    • Simulates data from the ZIP model
    • Used to create synthetic observations for testing inference
  2. Model Definition:

    • Uses Infer.NET's modeling API to declare random variables
    • Encodes the ZIP generative process using conditional statements
    • Implements the mixture of structural zeros and Poisson counts
  3. Inference:

    • Uses Expectation Propagation (EP) algorithm
    • Computes posterior distributions over unknown parameters
    • Returns Beta and Gamma posterior distributions

Customization

You can modify the following parameters in Program.cs:

int N = 1000;           // Number of data points
double pi = 0.3;        // True coin bias (structural zero probability)
double lambda = 2.0;    // True Poisson mean

Or adjust the prior distributions:

Beta cPrior = new Beta(1, 1);    // Prior for π
Gamma pPrior = new Gamma(1, 4);  // Prior for λ

Applications

Zero-Inflated Poisson models are commonly used in:

  • Healthcare: Modeling hospital visits (many people have zero visits)
  • Ecology: Counting species occurrences (many sites have zero observations)
  • Manufacturing: Defect counts (many items have zero defects)
  • Insurance: Claim counts (many policyholders file zero claims)
  • Marketing: Purchase counts (many customers make zero purchases)

About Infer.NET

Infer.NET is a framework for running Bayesian inference in graphical models. It can be used for a variety of different probabilistic inference tasks, including:

  • Classification
  • Regression
  • Clustering
  • Dimensionality reduction
  • Time-series analysis

The framework is developed by Microsoft Research and is open-source under the MIT license.

License

This project is licensed under the MIT License - see the LICENSE file for details.

References

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Author

Originally created as a demonstration of Bayesian inference with Infer.NET.
Updated to .NET 8.0 and modern Infer.NET framework (October 2025).

About

Simple demo of Zero-Inflated Poisson (ZIP) model using Infer.NET framework

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages