Math 2325
Project 3 (Chapter 6) - Randomized Response Surveys

Survey Simulation

The following program, Ran1Resp, simulates a randomized response survey.  It takes 5 paramaters; in order, they are:  GrpSize, the size of the group being surveyed (Nin the book); YesInGrp, the number of people in the group whose true answer to the question is Yes (in the book, p is YesInGrp divided by N); PrRealQ, the probability that a person will answer the real question (in the book, the probability that the dime lands heads); PrYesDecoy, the probability that the answer to the decoy question is yes (in the book, the probability that the penny lands heads); and NRep, the number of surveys the program will simulate.

Ran1Resp[GrpSize_,YesInGrp_,PrRealQ_,PrYesDecoy_,NRep_]:=Module[{Running=Result=ZeTab=Table[0,{3},{3},{3}],i,j,TrueYes=1,TrueNo=2,Real=1,Decoy=2,Yes=1,No=2,Total=3,topper={" YES "," NO  ","TOTAL"}},Do[Result=ZeTab;Do[If[RandomReal[]<PrRealQ,Result[[TrueYes,Real,Yes]]++,If[RandomReal[]<PrYesDecoy,Result[[TrueYes,Decoy,Yes]]++,Result[[TrueYes,Decoy,No]]++]],{YesInGrp}];Do[If[RandomReal[]<PrRealQ,Result[[TrueNo,Real,No]]++,If[RandomReal[]<PrYesDecoy,Result[[TrueNo,Decoy,Yes]]++,Result[[TrueNo,Decoy,No]]++]],{GrpSize-YesInGrp}];Do[Result[[i,j,Total]]=Result[[i,j,Yes]]+Result[[i,j,No]];Result[[i,Total,j]]=Result[[i,Real,j]]+Result[[i,Decoy,j]];Result[[Total,i,j]]=Result[[TrueYes,i,j]]+Result[[TrueNo,i,j]],{i,3},{j,3}];Running=Running+Result,{NRep}];If[NRep>1,Running=((Round[((Running (10^1))/NRep)])/((10`)^1))];Print["           T R U E    Y E S       T R U E   N O      W H O L E   G R O U P"];Print[TableForm[{" ","REAL","DECOY","TOTAL"}],"\t",TableForm[Prepend[Running[[1]],topper]],"\t",TableForm[Prepend[Running[[2]],topper]],"\t",TableForm[Prepend[Running[[3]],topper]]]]
(*Print[MatrixForm[Transpose[{{" ","REAL","DECOY","TOTAL"}}]],"\t",MatrixForm[Prepend[Running[[1]],topper]],"\t",MatrixForm[Prepend[Running[[2]],topper]],"\t",MatrixForm[Prepend[Running[[3]],topper]]]]*)

Here is an example (maximizing the screen will make the display easier to read).In this example, the group size is 17, the number of people whose true answer to the question is yes is 12, the probabilities of the penny and dime landing head are each .5, and the simulation will run 20 times.

Ran1Resp[17,12,.5,.5,20]

Properties of the Estimate

The following program, Ran2Resp, will help us explore some properties of the estimate you are using.  Be sure to also define Estimate and OpChar (below) before running Ran2Resp!

Ran2Resp[GrpSize_,YesInGrp_,PrRealQ_,PrYesDecoy_,NRep_]:=Module[{RunningSum=0,NYes},Do[NYes=0;Do[If[RandomReal[]<PrRealQ||RandomReal[]<PrYesDecoy,NYes++],{YesInGrp}];Do[If[!RandomReal[]<PrRealQ&&RandomReal[]<PrYesDecoy,NYes++],{GrpSize-YesInGrp}];RunningSum=RunningSum+OpChar[Estimate[GrpSize,NYes,PrRealQ,PrYesDecoy],(YesInGrp/GrpSize)],{NRep}];N[(RunningSum/NRep)]];

To study properties of your estimate of the proportion of "True Yesses" (people whose true answer to the question is Yes, i.e., YesInGrp divided by GrpSize), set Estimate to your estimate.  (In the book, the true proportion is denoted by p, and your estimate is denoted by (OverHat[p]).)  When you open this notebook the first time, Estimate is set to NYes divided by GrpSize, which is simply assuming that the number of True Yesses is simply the number of people reporting a Yes after flipping coins.  So be sure to reset it to your estimate.

Estimate[GrpSize_, NYes_,PrRealQ_,PrYesDecoy_]:=NYes/GrpSize;

OpChar is short for "operating characteristic".  You change its definition, depending on which operating characteristic of your estimate you want to investigate.  To study bias, set it to Estimate - True; to study mean absolute deviation (MAD), set it to Abs[Estimate - True]; to study standard deviation, set it to Sqrt[(Estimate - True)^2].

OpChar[Estimate_,True_]:=Estimate;

Now that we have set OpChar and Estimate, we can run Ran2Resp

Ran2Resp[17,12,.5,.5,20]

Art Duval, artduval@math.utep.edu
September 27, 2004;
Modified for Mathematica 6
November 18, 2008.