How to apply loop on following case?
Show older comments
This is my code where I computed Prediction interval coverage probability for IP_OPT and now want to compute for IS_OPT and RH_OPT (line 2 and 3).
One way to write a separate code for IS_OPT and RH_OPT as I did for IP_OPT which looks not a good way to make code. How can make a loop here for three all three IS_OPT, IS_OPT and RH_OPT to get three separate figures as shown below.
IP_p = prctile(IP_OPT,[0.5 99.5],2);
IS_p = prctile(IS_OPT,[2.5 97.5],2);
RH_p = prctile(RH_OPT,[2.5 97.5],2);
% Assuming have a dataset with n_total_points
n_total_points = size(IP_OPT, 1);
% Assuming you are using a certain percentage for training (e.g., 100%)
train_percentage = 1;
n_train_points = round(train_percentage * n_total_points);
d_obs1 = ip;
q = IP_OPT;
[P50,P1,P99,P] = CI_prob(q);
PICP_train = [];
PICP_pred = [];
for i = 1:10
ind1 = 21 + (i - 1) * (-1);
upper = P(:, ind1);
lower = P(:, i);
cp_indx = (upper>=d_obs1)&(d_obs1>=lower);
% Calculate the coverage probability for the training data
CP_train = sum(cp_indx(1:n_train_points)) / n_train_points;
PICP_train = [PICP_train CP_train];
end
ref_line = 0:0.1:1;
CP_theo = [0.99 0.9:-0.1:0.1];
PICP_linear_train_without = PICP_train;
h2 = figure();
plot(CP_theo,PICP_linear_train_without,'d');
hold on;
plot(ref_line,ref_line,'-.r','linewidth',1.0);
grid on;
xlabel('Theoretical Coverage Probability');
ylabel('Actual Coverage Probability (Training)');
title('Prediction Interval Coverage Probability');
legend('Actual Training Data', 'Ideal Linear Behavior');
%% Function
function [P50,P1,P99,P] = CI_prob(q)
Y = prctile(q,[0.5 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 99.5],2);
P50 = Y(:,11);
P1 = Y(:,end);
P99 = Y(:,1);
P = Y;
% ind = [1,11,21];
% P(:,ind) = [];
end

11 Comments
if CI_prob is your core function that is called several times, then you can do something like this using a loop
X_p = [IP_p, IS_p, RH_p];
D_p = [0.5 99.5;2.5 97.5;2.5 97.5];
for k = 1:numel(X_p)
F_p = prctile(X_p(k,[D_p(k,:)],2);
n_total_points = size(F_p, 1);
% Assuming you are using a certain percentage for training (e.g., 100%)
train_percentage = 1;
n_train_points = round(train_percentage * n_total_points);
d_obs1 = ip;
q = X_p(k);
[P50,P1,P99,P] = CI_prob(q);
%
PICP_train = [];
PICP_pred = [];
for i = 1:10
ind1 = 21 + (i - 1) * (-1);
upper = P(:, ind1);
lower = P(:, i);
cp_indx = (upper>=d_obs1)&(d_obs1>=lower);
% Calculate the coverage probability for the training data
CP_train = sum(cp_indx(1:n_train_points)) / n_train_points;
PICP_train = [PICP_train CP_train];
end
% the below lines need to be modified similar to function calls
ref_line = 0:0.1:1;
CP_theo = [0.99 0.9:-0.1:0.1];
PICP_linear_train_without = PICP_train;
h2 = figure(k);
plot(CP_theo,PICP_linear_train_without,'d');
hold on;
plot(ref_line,ref_line,'-.r','linewidth',1.0);
grid on;
% update figure labels and legends similarly for each figure if neeeded
xlabel('Theoretical Coverage Probability');
ylabel('Actual Coverage Probability (Training)');
title('Prediction Interval Coverage Probability');
legend('Actual Training Data', 'Ideal Linear Behavior');
end
%% Function
function [P50,P1,P99,P] = CI_prob(q)
Y = prctile(q,[0.5 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 99.5],2);
P50 = Y(:,11);
P1 = Y(:,end);
P99 = Y(:,1);
P = Y;
% ind = [1,11,21];
% P(:,ind) = [];
end
Ahmed
on 2 Mar 2024
VBBV
on 2 Mar 2024
F_p = prctile(X_p(k),[D_p(k,:)],2); % sorry a typo error
%^
Please check now.
Also, what are [IP_p, IS_p, RH_p]; ?
Are they numeric arrays ? or strings ? if they are strings, then use a cell array
X_p = {'IP_p', 'IS_p', 'RH_p'};
then code will become something like
X_p = [IP_p, IS_p, RH_p];
D_p = [0.5 99.5;2.5 97.5;2.5 97.5];
for k = 1:3 % just to be sure of elements are string scalars
F_p = prctile(X_p(k),[D_p(k,:)],2); %corrected typo
n_total_points = size(F_p, 1);
% Assuming you are using a certain percentage for training (e.g., 100%)
train_percentage = 1;
n_train_points = round(train_percentage * n_total_points);
d_obs1 = ip;
q = X_p(k);
[P50,P1,P99,P] = CI_prob(q);
%
PICP_train = [];
PICP_pred = [];
for i = 1:10
ind1 = 21 + (i - 1) * (-1);
upper = P(:, ind1);
lower = P(:, i);
cp_indx = (upper>=d_obs1)&(d_obs1>=lower);
% Calculate the coverage probability for the training data
CP_train = sum(cp_indx(1:n_train_points)) / n_train_points;
PICP_train = [PICP_train CP_train];
end
% the below lines need to be modified similar to function calls
ref_line = 0:0.1:1;
CP_theo = [0.99 0.9:-0.1:0.1];
PICP_linear_train_without = PICP_train;
h2 = figure(k);
plot(CP_theo,PICP_linear_train_without,'d');
hold on;
plot(ref_line,ref_line,'-.r','linewidth',1.0);
grid on;
% update figure labels and legends similarly for each figure if neeeded
xlabel('Theoretical Coverage Probability');
ylabel('Actual Coverage Probability (Training)');
title('Prediction Interval Coverage Probability');
legend('Actual Training Data', 'Ideal Linear Behavior');
end
%% Function
function [P50,P1,P99,P] = CI_prob(q)
Y = prctile(q,[0.5 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 99.5],2);
P50 = Y(:,11);
P1 = Y(:,end);
P99 = Y(:,1);
P = Y;
% ind = [1,11,21];
% P(:,ind) = [];
end
Ahmed
on 2 Mar 2024
Ahmed
on 2 Mar 2024
Ok, fif they are arrays of 101 x 1 size then do the following change to this line
F_p = prctile(X_p(:,k),[D_p(k,:)],2);
Ahmed
on 2 Mar 2024
q = X_p(k);
Also, change the above line as
q = X_p(:,k);
i forgot this line calls each of the vectors of [IP_p, IS_p, RH_p] from X_p variable. Check whether it gives correct results now
VBBV
on 2 Mar 2024
Where do you use h2 in your code ? elswhere in other code snippets ?
h2 = figure()
Ahmed
on 2 Mar 2024
Accepted Answer
More Answers (1)
Make a function of the part of the code that you have to run through three times and call this function for IP_OPT, IS_OPT and RH_OPT.
Consider to make the plotting in the calling script part.
2 Comments
Ahmed
on 1 Mar 2024
Small example:
You want to compute x^2 for x = 1,2,3:
x = [1 2 3];
for i = 1:numel(x)
f(i) = square(x(i));
end
f
function f = square(x)
f = x^2;
end
Now imagine x = IP_OPT, IS_OPT and RH_OPT.
Put the necessary commands in a function (like the square function above) such that it returns PICP_train, PISP_train and PRHP_train when called with IP_OPT, IS_OPT and RH_OPT.
Categories
Find more on Creating and Concatenating Matrices 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!



