Skip to content

usptact/RandomizedTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Randomized Response Model with Infer.NET

A demonstration of Bayesian inference using the Randomized Response technique with Microsoft's Infer.NET probabilistic programming framework on .NET 8.0.

The Problem

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.

How Randomized Response Works

The process works as follows:

  1. A student privately determines their true answer (cheated or didn't cheat)
  2. 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.

The Mathematical Model

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.

Prerequisites

Building the Project

# Clone the repository (if not already cloned)
git clone <repository-url>
cd RandomizedTest

# Restore dependencies
dotnet restore

# Build the project
dotnet build -c Release

Running the Application

# Run with Release configuration
dotnet run --project RandomizedTest/RandomizedTest.csproj -c Release

Sample Output

*************
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!

Project Structure

RandomizedTest/
├── RandomizedTest/
│   ├── Program.cs              # Main application code
│   └── RandomizedTest.csproj   # Project file (.NET 8.0)
├── LICENSE                     # BSD License
└── README.md                   # This file

Key Features

  • 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

Technical Details

Dependencies

  • Microsoft.ML.Probabilistic (v0.4.2203.202) - Core Infer.NET library
  • Microsoft.ML.Probabilistic.Compiler (v0.4.2203.202) - Model compiler
  • Microsoft.CodeAnalysis.CSharp (v4.11.0) - Roslyn compiler for cross-platform support

Compiler Configuration

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.

Customization

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)

Further Reading

License

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

Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

Acknowledgments

This project uses Infer.NET, Microsoft's open-source probabilistic programming framework for machine learning and Bayesian inference.

About

Demo of Randomized Response model (Infer.NET)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages