# Creating confidence interval plots for random graphs

I have the following code where I create instances of random graphs and use their properties to compute the mean of a quantity labeled 'PoA'. In the last line, I use a range of values for the parameters 'nodes' and 'conn', and create a table for plotting the mean.

I now want to bring Standard deviation also into consideration. I would like the plots to have lines for mean and mean$\pm 2 \sigma$. Can someone help me to add those extra lines in the plot?

PoAGen[nodes_, conn_, n_: 1000] :=
Module[{an = 100, al = 1, s, M, id, od, wd, x, poa, PoA},
Cases[_?NumericQ]@
Table[s =
DirectedGraph[RandomGraph[{nodes, IntegerPart[conn*nodes]}],
"Acyclic"];
id = an + al*VertexInDegree[s];
od = al*VertexOutDegree[s];
wd = -Log[1 - (od/id)];
x = (DiagonalMatrix[id] - M).wd;
poa = N[od - x, 4];
PoA = Total@poa, {n}] // Mean]

dataPlot = Table[{nodes, conn, PoAGen[nodes, conn]}, {nodes, 20, 40, 2},
{conn,2.5, 5, 0.2}];
ListPlot[dataPlot[[All, 1, {1, 3}]], Frame -> True, Joined -> True,
FrameLabel -> {"|V|", "Inefficiency \[CapitalDelta]"},
PlotLabel -> "|E|/|V|=: " ~~ ToString[dataPlot[[1, 1, 2]]]]
ListPlot[dataPlot[[2, All, {2, 3}]], Frame -> True, Joined -> True,
FrameLabel -> {"|E|/|V|", "Inefficiency \[CapitalDelta]"},
PlotLabel -> "|V|: " ~~ ToString[dataPlot[[1, 1, 1]]]]

• Assign the result of Cases within the PoAGen function to a variable, then calculate its Mean and StandardDeviation and return those as a list. You will get a slightly more complex expression for dataPlot, but it should be relatively simple to extract the data you need and plot it, just like you extract the means. Oct 1 '15 at 6:08

Follow the suggestion by MarcoB to make PoAGen return a list of the mean and standard deviation of the result of Cases. I always like to use Filling to show confidence intervals, for example:

ListPlot[
{{#[], #[[3, 1]]} & /@ dataPlot[[All, 1]],
{#[], #[[3, 1]] + 2*#[[3, 2]]} & /@ dataPlot[[All, 1]],
{#[], #[[3, 1]] - 2*#[[3, 2]]} & /@ dataPlot[[All, 1]]},
Frame -> True, Joined -> True,
FrameLabel -> {"|V|", "Inefficiency \[CapitalDelta]"},
PlotLabel -> "|E|/|V|=: " ~~ ToString[dataPlot[[1, 1, 2]]],
PlotStyle -> {Black, Gray, Gray},
Filling -> {1 -> {{2}, LightGray}, 1 -> {{3}, LightGray}}
] NB. You should also reconsider your confidence intervals. The mean±2σ 'rule of thumb' is usually incorrect. For example, see https://en.wikipedia.org/wiki/Confidence_interval#Practical_example

• Thanks Roel. I am not very familiar with Mathematica. Can you please tell me how to return two arguments from Cases? Oct 1 '15 at 9:48
• You can do something like, PoAGen[nodes_, conn_, n_: 1000] := Module[{an = 100, al = 1, s, M, id, od, wd, x, poa, PoA, f}, f = Cases[_?NumericQ] (...) Total@poa, {n}]; {Mean[f],StandardDeviation[f]} ] Oct 2 '15 at 18:34