# Randomized Response

A method for querying when those who answer the questions may not want to disclose their true answers.

For example suppose the question has Yes/No answers. The person answering flips a coin, then:

- If tails, respond truthfully.
- If heads, flip a second coin and respond Yes if heads, No if tails.

Only the person answering knows whether their answer was the truth, or is based on what the coin flip forced them to say, since only they know the results of the coin flip.

import numpy as np true_distr_samples = lambda n, p: [1 if np.random.random()<p else 0 for _ in range(n)] p_true = 0.2 p_l1,q_l2 = 0.5, 0.5 pop = true_distr_samples(500,p_true) answers = [] for p in pop: if np.random.random()<p_l1: answers.append(p) else: answers.append(0) if np.random.random()<q_l2 else answers.append(1) p_est = ((sum(answers)/len(answers))-(1-p_l1)*q_l2)/(1-q_l2) print("True ratio of 1s is {}, estimated from data is {:.3f}".format(p_true, p_est))