Skip to content

Instantly share code, notes, and snippets.

@JEM-Mosig
Last active April 8, 2019 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JEM-Mosig/6b6caf20a50567af1f645e3f6c4c5148 to your computer and use it in GitHub Desktop.
Save JEM-Mosig/6b6caf20a50567af1f645e3f6c4c5148 to your computer and use it in GitHub Desktop.
This Wolfram Language (Mathematica) code generates an interactive data-exploration tool for our Snickers experiment.
With[{
data = {<|"ID"->1,"Experimenter"->"Nellissa","Outcome"->"DE\[NotEqual]VN","Countries"->{"DE","VN"},"Same"->False,"Comment"->"One is sweet, the other caramelly"|>,<|"ID"->2,"Experimenter"->"Nellissa","Outcome"->"GB\[NotEqual]VN","Countries"->{"GB","VN"},"Same"->False,"Comment"->"One tasts like coffee and sweeter"|>,<|"ID"->3,"Experimenter"->"Nellissa","Outcome"->"DE\[Equal]VN","Countries"->{"DE","VN"},"Same"->True,"Comment"->""|>,<|"ID"->4,"Experimenter"->"Nellissa","Outcome"->"VN\[NotEqual]VN","Countries"->{"VN","VN"},"Same"->False,"Comment"->"One is sweeter"|>,<|"ID"->5,"Experimenter"->"Nellissa","Outcome"->"VN\[Equal]VN","Countries"->{"VN","VN"},"Same"->True,"Comment"->"Terrible"|>,<|"ID"->6,"Experimenter"->"Nellissa","Outcome"->"DE\[Equal]GB","Countries"->{"DE","GB"},"Same"->True,"Comment"->"(?) Tasts the same, peanutty and caramelly"|>,<|"ID"->7,"Experimenter"->"Nellissa","Outcome"->"GB\[NotEqual]VN","Countries"->{"GB","VN"},"Same"->False,"Comment"->"One is too sweet"|>,<|"ID"->8,"Experimenter"->"Nellissa","Outcome"->"DE\[Equal]GB","Countries"->{"DE","GB"},"Same"->True,"Comment"->"(?) Yummy"|>,<|"ID"->9,"Experimenter"->"Nellissa","Outcome"->"DE\[NotEqual]VN","Countries"->{"DE","VN"},"Same"->False,"Comment"->"One is more peanutty"|>,<|"ID"->10,"Experimenter"->"Nellissa","Outcome"->"GB\[Equal]VN","Countries"->{"GB","VN"},"Same"->True,"Comment"->"(?) Too sweet"|>,<|"ID"->11,"Experimenter"->"Johannes","Outcome"->"DE\[Equal]GB","Countries"->{"DE","GB"},"Same"->True,"Comment"->""|>,<|"ID"->12,"Experimenter"->"Johannes","Outcome"->"DE\[NotEqual]GB","Countries"->{"DE","GB"},"Same"->False,"Comment"->""|>,<|"ID"->13,"Experimenter"->"Johannes","Outcome"->"GB\[NotEqual]VN","Countries"->{"GB","VN"},"Same"->False,"Comment"->"One is a bit sweeter"|>,<|"ID"->14,"Experimenter"->"Johannes","Outcome"->"GB\[NotEqual]VN","Countries"->{"GB","VN"},"Same"->False,"Comment"->""|>,<|"ID"->15,"Experimenter"->"Johannes","Outcome"->"DE\[NotEqual]VN","Countries"->{"DE","VN"},"Same"->False,"Comment"->"Both nutty, but one more"|>,<|"ID"->16,"Experimenter"->"Johannes","Outcome"->"VN\[Equal]VN","Countries"->{"VN","VN"},"Same"->True,"Comment"->""|>,<|"ID"->17,"Experimenter"->"Johannes","Outcome"->"DE\[NotEqual]GB","Countries"->{"DE","GB"},"Same"->False,"Comment"->"One nuttier?"|>,<|"ID"->18,"Experimenter"->"Johannes","Outcome"->"DE\[NotEqual]VN","Countries"->{"DE","VN"},"Same"->False,"Comment"->"One more nutty than the other"|>,<|"ID"->19,"Experimenter"->"Johannes","Outcome"->"DE\[Equal]VN","Countries"->{"DE","VN"},"Same"->True,"Comment"->""|>,<|"ID"->20,"Experimenter"->"Johannes","Outcome"->"VN\[Equal]VN","Countries"->{"VN","VN"},"Same"->True,"Comment"->""|>,<|"ID"->21,"Experimenter"->"Johannes","Outcome"->"GB\[Equal]VN","Countries"->{"GB","VN"},"Same"->True,"Comment"->"Hard to tell, one is an end-piece"|>},
prior = Outer[Times, ConstantArray[1/5,5], {10,13,16,14,12,8,4,2,1,1}/81]
},
Manipulate[
Column[{
ButtonBar[{
"None":>(selected={}),
"All":>(selected=data),
"Only Nellissa":>(selected=Normal@Dataset[data][Select[#Experimenter=="Nellissa"&]]),
"Only Johannes":>(selected=Normal@Dataset[data][Select[#Experimenter=="Johannes"&]]),
"Only positives":>(selected=Normal@Dataset[data][Select[#Same&]]),
"Only negatives":>(selected=Normal@Dataset[data][Select[Not@#Same&]])
}],
With[{posterior = bayesianUpdate[prior, selected]},
Labeled[
believePlot[prior, posterior],
Deploy@BarLegend[{Function[{v}, ColorData["TemperatureMap"][Clip[Sqrt[v/0.2], {0,1}]]],{0, 0.2}},
LegendLayout->"Row",
LegendMargins->0,
LegendMarkerSize->300,
LabelStyle->Black,
TicksStyle -> Black,
Ticks -> {0.`,0.05`,0.1`,0.15,{0.2`,"0.2+"}},
LegendLabel -> Placed["P(\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(i\)]\), \[Epsilon])", Right]
]
]
]
}],
{{selected, {}, Null},
Thread[data -> Map[
Row[{
Pane[#Outcome, 50],
Pane[#Experimenter, 50],
Pane[Style[#Comment, Italic], 200]
}, Spacer[1], Background -> If[OddQ[#ID], None, LightGray]] &,
data
]
],
ControlType -> CheckboxBar,
Appearance -> "Vertical",
ControlPlacement -> Right
},
Initialization :> (
ClearAll[likelihood, bayesianUpdate, believePlot];
likelihood[datum_String,iHypothesis_,iFailurerate_] := Catch[
With[{
\[Epsilon] = iFailurerate 0.1-.05,
a = StringTake[datum,{1,2}],
b = StringTake[datum,{4,5}],
comp = StringTake[datum,{3}]
},
If[comp==="\[NotEqual]",Throw[1-likelihood[a<>"\[Equal]"<>b,iHypothesis,iFailurerate]]];
If[a===b,Throw[1-\[Epsilon]]];
Switch[iHypothesis,
(* all equal (Subscript[\[ScriptCapitalH], =]) *)
1,1-\[Epsilon],
(* all different (Subscript[\[ScriptCapitalH], \[NotEqual]]) *)
2,\[Epsilon],
(* DE \[Equal] GB \[NotEqual] VN (Subscript[\[ScriptCapitalH], VN]) *)
3,If[MemberQ[{a,b},"VN"],\[Epsilon],1-\[Epsilon]],
(* DE \[Equal] VN \[NotEqual] GB (Subscript[\[ScriptCapitalH], GB]) *)
4,If[MemberQ[{a,b},"GB"],\[Epsilon],1-\[Epsilon]],
(* GB \[Equal] VN \[NotEqual] DE (Subscript[\[ScriptCapitalH], DE]) *)
5,If[MemberQ[{a,b},"DE"],\[Epsilon],1-\[Epsilon]]
]
]
];
bayesianUpdate[prior_, datum_String] := Module[{temp,evidence},
temp=Table[
likelihood[datum,k,i] prior[[k,i]],
{k,1,5},{i,1,10}
];
evidence=Total@Flatten[temp];
temp/evidence
];
bayesianUpdate[prior_, data_List] := Fold[
bayesianUpdate,
prior,
data
];
bayesianUpdate[prior_, datum_Association] := bayesianUpdate[prior, datum["Outcome"]];
Options[believePlot]={PlotLabel->"Prior"};
believePlot[believeMatrix_,OptionsPattern[]]:=ArrayPlot[
believeMatrix,
PlotLabel->OptionValue[PlotLabel],
FrameLabel->{None,"failure rate in %"},
DataRange->{{0,1},{1,5}},
FrameTicks->{{{5,"\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(=\)]\)"},{4,"\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(\[NotEqual]\)]\)"},{3,"\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(VN\)]\)"},{2,"\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(GB\)]\)"},{1,"\!\(\*SubscriptBox[\(\[ScriptCapitalH]\), \(DE\)]\)"}},Table[{(n+0.5)/9.,(n+1)10.},{n,-1,9,2}]},
AspectRatio->1,LabelStyle->14,
ColorFunction -> Function[{v}, ColorData["TemperatureMap"][Clip[Sqrt[v/0.2], {0,1}]]],
ColorFunctionScaling -> False,
ImageSize->320
];
believePlot[prior_, posterior_,OptionsPattern[]] := GraphicsRow[{
believePlot[prior],
believePlot[posterior,PlotLabel->"Posterior"]
},0];
),
Paneled->True
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment