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.
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.
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:
- Structural Zeros: Generated by a Bernoulli process (coin flip) that produces zeros with probability π
- 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 generative story for each data point:
- Flip a weighted coin with bias π (probability of heads)
- If heads: return 0 (structural zero)
- 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.
- .NET 8.0 SDK or later
- Download from: https://dotnet.microsoft.com/download/dotnet/8.0
- Operating System: Windows, macOS, or Linux
- IDE (Optional): Visual Studio 2022, Visual Studio Code, or JetBrains Rider
The project uses the following NuGet packages:
Microsoft.ML.Probabilistic(v0.4.2301.301) - Core probabilistic programming frameworkMicrosoft.ML.Probabilistic.Compiler(v0.4.2301.301) - Compilation support for model inference
These are automatically restored when building the project.
-
Clone the repository:
git clone https://github.com/yourusername/ZeroInflatedPoisson-Infer.NET.git cd ZeroInflatedPoisson-Infer.NET -
Restore dependencies:
dotnet restore
-
Build the project:
dotnet build
For a release build:
dotnet build -c Release
- Open
ZeroInflatedPoisson.slnin Visual Studio 2022 - Press
Ctrl+Shift+Bto build the solution - Dependencies will be restored automatically
From the repository root:
dotnet run --project ZeroInflatedPoissonOr navigate to the project directory:
cd ZeroInflatedPoisson
dotnet run- Set
ZeroInflatedPoissonas the startup project (if not already) - Press
F5to run with debugging, orCtrl+F5to run without debugging
The program will:
- Generate 1000 synthetic data points from a ZIP model with:
- True π = 0.3 (30% probability of structural zeros)
- True λ = 2.0 (Poisson mean)
- Perform Bayesian inference to estimate these parameters
- 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.
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
-
Data Generation (
SampleDatamethod):- Simulates data from the ZIP model
- Used to create synthetic observations for testing inference
-
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
-
Inference:
- Uses Expectation Propagation (EP) algorithm
- Computes posterior distributions over unknown parameters
- Returns
BetaandGammaposterior distributions
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 meanOr adjust the prior distributions:
Beta cPrior = new Beta(1, 1); // Prior for π
Gamma pPrior = new Gamma(1, 4); // Prior for λ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)
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.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests.
Originally created as a demonstration of Bayesian inference with Infer.NET.
Updated to .NET 8.0 and modern Infer.NET framework (October 2025).