I have a Manipulate
that uses RandomVariate
and Random
that I would like to have redraw and recompute random values selectively, depending on what Manipulate
parameters have been changed.
For example, I would like to be able to control redraws and recomputations so that
- new
data
andhonestMeans
are only computed ifn
has changed; - new
punishMeans
are computed only whenn
orpunish
have changed; - a redraw is triggered when
zoom
changes, but without triggering an update todata
,honestMeans
orpunishMeans
; - a redraw triggered when any of
data
,honestMeans
orpunishMeans
orzoom
changes (without triggering a recomputation ofdata
,honestMeans
orpunishMeans
); - everything updates if an explicit update is requested (somehow)
as indicated in the comments below:
scale = 5;
dist = HypergeometricDistribution[scale, Round[0.7*2*scale], 2*scale];
genVotes[punish_, n_] := Module[{},
votes = {data[[1]]};
For[i = 2, i <= n, i++,
If[Random[] < punish,
If[votes[[i - 1]] > data[[i]], vote = 0, vote = scale],
vote = data[[i]]
];
AppendTo[votes, Mean[Append[data[[1 ;; i - 1]], vote]]];
AppendTo[votes, Mean[Append[data[[1 ;; i - 1]], data[[i]]]]];
]; Return[votes];
];
Manipulate[
data = RandomVariate[dist, n];(* Should only change when n changes *)
punishMeans = genVotes[punish, n]; (* Should change when punish or n change *)
honestMeans = genVotes[0, n]; (* Should only change when n changes *)
GraphicsColumn[{ (* Should redraw when any of the above change, or if zoom changes, without triggering recompilation of data, punishMeans or honestMeans *)
Show[
ListPlot[punishMeans,
Joined -> False, PlotStyle -> Gray, PlotMarkers -> Automatic,
PlotRange -> {{0, n}, {(1 - zoom)*#, (1 + zoom)*#} &[Mean[data]]},
Frame -> True, Axes -> None,
Epilog -> {Blue, Line[{{0, Mean[data]}, {n, Mean[data]}}]}],
ListPlot[honestMeans,
Joined -> True, PlotStyle -> Black, PlotMarkers -> None]
],
Show[
Histogram[data, scale, PlotRange -> {{0, scale + 1}, All},
Frame -> True,
FrameTicks -> {{Automatic,
Automatic}, {{# + .5, #, 0} & /@ Range[0, scale], None}},
Axes -> None],
DiscretePlot[n*PDF[dist, x], {x, 0, scale + 1}, Joined -> True,
PlotMarkers -> None, Filling -> None]
]
}],
{{n, 100}, 10, 5000},
{{punish, 0.5}, 0, 1},
Delimiter,
{{zoom, 0.2}, .1, .5},
TrackedSymbols :> {n, punish, zoom}]
Is there a way to do this kind of selective update of elements of a Manipulate
?