How to apply loop on following case?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
VBBV
am 2 Mär. 2024
Where do you use h2 in your code ? elswhere in other code snippets ?
h2 = figure()
Akzeptierte Antwort
VBBV
am 2 Mär. 2024
Try using the cell array for the testdata you gave
data = load('datatest.mat');
X_p = {data.IP_OPT, data.IS_OPT, data.RH_OPT} % make a cell array
D_p = [0.5 99.5;2.5 97.5;2.5 97.5]; % make a vector
for k = 1:3 % just to be sure of elements are string scalars
F_p = prctile(X_p{k},[D_p(k,:)],2); % use a cell array
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; % what is ip here thats not define anywhere
q = X_p{k}; % use a cell array
[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
3 Kommentare
VBBV
am 2 Mär. 2024
data = load('datatest.mat');
iprho = load('ipisrho.mat')
X_p = {data.IP_OPT, data.IS_OPT, data.RH_OPT} % make a cell array
ipisrho = {iprho.ip iprho.is iprho.rho};
D_p = [0.5 99.5;2.5 97.5;2.5 97.5]; % make a vector
for k = 1:3 % just to be sure of elements are string scalars
F_p = prctile(X_p{k},[D_p(k,:)],2); % use a cell array
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 = ipisrho{k}; % use cell array like before
q = X_p{k}; % use a cell array
[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
Weitere Antworten (1)
Torsten
am 1 Mär. 2024
Bearbeitet: Torsten
am 1 Mär. 2024
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 Kommentare
Torsten
am 1 Mär. 2024
Bearbeitet: Torsten
am 1 Mär. 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.
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




