A demonstration of Bayesian inference using the Randomized Response technique with Microsoft's Infer.NET probabilistic programming framework on .NET 8.0.
Suppose you want to estimate the proportion of students who cheat on exams. The challenge is that no one will honestly admit to cheating in a direct survey due to social stigma and potential consequences. This is a classic example of a sensitive question where respondents are unlikely to answer truthfully.
The Randomized Response model provides a solution by offering plausible deniability to respondents while still allowing accurate statistical inference about the true proportion of the sensitive behavior in the population.
The process works as follows:
- A student privately determines their true answer (cheated or didn't cheat)
- The student flips two fair coins privately:
- If coin #1 is heads: Report the truthful answer
- If coin #1 is tails:
- If coin #2 is heads: Report "cheated"
- If coin #2 is tails: Report "didn't cheat"
Because of the randomization introduced by the coins, any individual response could have been generated by the coins alone, providing plausible deniability. However, with enough responses, the true underlying proportion can be accurately estimated using Bayesian inference.
This implementation uses Infer.NET to build a probabilistic model:
- Prior:
theta ~ Beta(1, 1)(uniform prior over [0,1] for the true cheating proportion) - Generative Model: For each student
i:truth_i ~ Bernoulli(theta)(true state: cheated or not)coin1_i ~ Bernoulli(0.5)(first fair coin)coin2_i ~ Bernoulli(0.5)(second fair coin)- If
coin1_i = 1:response_i = truth_i - If
coin1_i = 0:response_i = coin2_i
The inference engine uses Expectation Propagation (EP) to compute the posterior distribution of theta given the observed responses.
- .NET 8.0 SDK or later
- Works on Windows, macOS, and Linux
# Clone the repository (if not already cloned)
git clone <repository-url>
cd RandomizedTest
# Restore dependencies
dotnet restore
# Build the project
dotnet build -c Release# Run with Release configuration
dotnet run --project RandomizedTest/RandomizedTest.csproj -c Release*************
True p = 0.36
*************
Compiling model...done.
Iterating:
.........|.........|.........|.........|.........| 50
Posterior of theta:
E(theta) = 0.3548001434909748
Std(theta) = 0.02134741027385495
Press any key to exit...
In this example:
- The true cheating proportion is 0.36 (36%)
- The model estimates E(theta) ≈ 0.355 (35.5%)
- The posterior standard deviation is about 0.021, indicating high confidence
The model successfully recovers the true proportion from the randomized responses!
RandomizedTest/
├── RandomizedTest/
│ ├── Program.cs # Main application code
│ └── RandomizedTest.csproj # Project file (.NET 8.0)
├── LICENSE # BSD License
└── README.md # This file
- Modern .NET 8.0: Uses the latest SDK-style project format
- Cross-Platform: Works on Windows, macOS, and Linux (uses Roslyn compiler)
- Latest Infer.NET: Version 0.4.2203.202 with full .NET 8.0 compatibility
- Bayesian Inference: Demonstrates probabilistic programming and Bayesian methods
- Privacy-Preserving: Shows how to gather sensitive information while protecting individual privacy
Microsoft.ML.Probabilistic(v0.4.2203.202) - Core Infer.NET libraryMicrosoft.ML.Probabilistic.Compiler(v0.4.2203.202) - Model compilerMicrosoft.CodeAnalysis.CSharp(v4.11.0) - Roslyn compiler for cross-platform support
The application uses the Roslyn compiler (CompilerChoice.Roslyn) instead of the default CodeDom compiler to ensure cross-platform compatibility, especially on macOS and Linux where CodeDom is not fully supported.
You can modify the parameters in Program.cs:
int numData = 1000; // Number of survey responses
double trueP = 0.36; // True proportion (for synthetic data generation)You can also experiment with:
- Different prior distributions (e.g.,
Beta(2, 5)for an informative prior) - Different coin probabilities (currently both coins are fair: 0.5)
- Alternative inference algorithms (e.g., Variational Message Passing)
- Infer.NET Documentation
- Randomized Response Technique (Warner, 1965)
- Bayesian Inference
- Expectation Propagation
This project is licensed under the BSD License - see the LICENSE file for details.
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
This project uses Infer.NET, Microsoft's open-source probabilistic programming framework for machine learning and Bayesian inference.