Calculating different levels of confidence and drawing the corresponding graph

2 views (last 30 days)
I have monthly results of a natural process, obtained from 32 numerical models.
They show monthly rate of a process. How can I calculate different level of confidence (e.g., 10 %, 20%, 50%, 95%) for each months and draw a graph showing the mean of results and level of confidence for each bound.
Cheers,
Amin

Answers (1)

Aashray
Aashray on 18 Jun 2025
For calculating and visualizing the confidence intervals in a plot, you can do the following:
  1. Calculate the mean of all 32 models for each month using “mean()” function.
  2. Compute confidence intervals (90%, 80%, and 50%) using percentiles “prctile()” function.
  3. Plot shaded bands to represent the confidence intervals using “fill()” function.
This is how I implemented the above steps, you can see different confidence intervals shaded in different shades of blue in the plot:
% Using example data: 32 models × 12 months
rng(1);
data = randn(32, 12) * 2e4 + linspace(1e5, 2.5e5, 12);
meanVals = mean(data, 1);
ci_90 = prctile(data, [5 95], 1);
ci_80 = prctile(data, [10 90], 1);
ci_50 = prctile(data, [25 75], 1);
months = 1:12;
figure; hold on;
% Confidence bands (shaded areas)
fill([months fliplr(months)], [ci_90(1,:) fliplr(ci_90(2,:))], [0.8 0.8 1], 'EdgeColor', 'none');
fill([months fliplr(months)], [ci_80(1,:) fliplr(ci_80(2,:))], [0.6 0.6 1], 'EdgeColor', 'none');
fill([months fliplr(months)], [ci_50(1,:) fliplr(ci_50(2,:))], [0.3 0.3 1], 'EdgeColor', 'none');
% Plot each model line in a different color
colors = lines(32);
colors = [colors ones(32,1)*0.69];
for i = 1:32
plot(months, data(i,:), '-', 'Color', colors(i,:), 'LineWidth', 1);
end
% Plot mean line
plot(months, meanVals, 'k-', 'LineWidth', 2);
% Axes and labels
xticks(1:12);
xticklabels({'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'});
ylabel('Rate (m^3)');
title('Mean & Confidence Intervals from 32 Model Simulations');
legend({'90% CI', '80% CI', '50% CI', 'Models', 'Mean'}, 'Location', 'northwest');
grid on;
I am also attaching the documentation links of the functions used for your reference:
  1. “prctile()”: https://www.mathworks.com/help/matlab/ref/prctile.html
  2. “fill()”: https://www.mathworks.com/help/matlab/ref/fill.html

Categories

Find more on Networks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!