2
$\begingroup$

For all the added support that Around has gotten in recent versions of MMA it would have been nice if there was native support for plotting functions with coefficients that include errors, for example by sampling over the errors and showing "filled in" curves covering the whole area covered by the function depending on the values taken for the coefficients within the error bars.

Unfortunately something like Plot[Around[1, 0.05] x + Around[1, 0.01], {x, 0, 2}] just doesn't render.

Can we come up with some (hopefully quite general) solution?


One naive approach expanding the equations into a list of equations sampling the mid and end points of the Around Coefficients:

aroundToMinMidMax[{v_?NumberQ,err_?NumberQ}]:={v-err,v,v+err};
expandAroundEquationsToListOfEquations[eq_]:=Module[{aux,coefSets,ii,jj},
    aux=Cases[eq,Around[v_,err_]:>{v,err},Infinity];
    coefSets=Tuples[aroundToMinMidMax/@aux];
    Table[
        ii=1;
        eq/.Around[v_,err_]:>coefSets[[jj,ii++]],
        {jj,1,Length[coefSets]}]
];

Which can then be plotted like this:

fillingRules[numCurves_] := 
  Flatten[Table[{i -> {j}}, {i, 1, numCurves - 1}, {j, i + 1, 
     numCurves}]];
eq = Around[1, 0.1] x + Around[0.5, 0.02];
expanedEqList = expandAroundEquationsToListOfEquations[eq];
Length[expanedEqList]
Plot[expanedEqList, {x, 0, 1}, PlotStyle -> ColorData[97][1], 
 Filling -> fillingRules[9]]
$\endgroup$
2
  • $\begingroup$ What you are assuming is that the coefficient estimators are all independent and it is unlikely if not near impossible for that to occur. Polynomial coefficients are not estimated in isolation of each other. There will almost always be a non-zero correlation that affects the size of a prediction band at any predictor value. If you used LinearModelFit, you can get the prediction bands directly without resorting to Around. Do you have a situation where the polynomial coefficient estimators are estimated independently? $\endgroup$
    – JimB
    Commented 13 hours ago
  • $\begingroup$ @JimB, probably in almost all cases that is true. In my case the errors actually originate from exact bounds (there is an error on the error but it is about 60 magnitudes smaller). $\endgroup$
    – Kvothe
    Commented 5 hours ago

1 Answer 1

3
$\begingroup$

You could use the accessors of Around objects:

f=Around[1,0.05] x + Around[1,0.01];
Plot[{f["Value"],List@@f["Interval"]},{x,0,2},
  Filling->{1->{2}},
  PlotStyle->{Lighter@ColorData[97][1],ColorData[97][1]}
]

to get:

enter image description here

For the PlotStyle the first argument can also be None to leave out the middle line.

$\endgroup$
3
  • 1
    $\begingroup$ Interesting! Did not know you could do this. This seems to be exactly what I want. Do you know how it actually works? How does it determine an "Interval"? $\endgroup$
    – Kvothe
    Commented yesterday
  • $\begingroup$ It is just ±1 uncertainty for a symmetric uncertainty. Documentation has some more cases: in the details section of Around it is the first table. $\endgroup$
    – SHuisman
    Commented yesterday
  • $\begingroup$ Still think it is a good question, I'll ask the devs to consider this, or at least plot the center value, not just give an empty plot. $\endgroup$
    – SHuisman
    Commented yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.