Skip to content

Instantly share code, notes, and snippets.

@runarorama
Created September 7, 2022 13:33
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 runarorama/87c5392afe28a5203b62cc773936132a to your computer and use it in GitHub Desktop.
Save runarorama/87c5392afe28a5203b62cc773936132a to your computer and use it in GitHub Desktop.
CombineEstimates[es_] := Module[{result, ds, var, tvals, newe, t},
(*Gets the standard deviation for a confidence interval `e`.*)
Deviation[e_,
confidence_] := (Max[e] -
Min[e])/(InverseCDF[NormalDistribution[], confidence]*2);
(*Gets the confidence for an interval given its standard
deviation.*)
Confidence[e_, dev_] :=
CDF[NormalDistribution[], ((Max[e] - Min[e])/dev)/2];
ds = Map[Deviation @@ # &, es];
(*Find the minimum variation `var` and
the t-values that yield it.*)
{var, tvals} =
Minimize[{Plus @@ Table[t[i]^2*ds[[i]]^2, {i, 1, Length[ds]}],
Plus @@ Table[t[i], {i, 1, Length[ds]}] == 1},
Table[t[i], {i, 1, Length[ds]}]];
(*The combined estimate*)
newe =
Fold[#1 + #2[[1]]*#2[[2]] &, 0,
Transpose[{es[[All, 1]], Values[tvals]}]];
result = {newe, Confidence[newe, Sqrt[var]]}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment