PRCC sensitive analysis graphs

조회 수: 35 (최근 30일)
mallela ankamma rao
mallela ankamma rao 2024년 9월 11일
답변: Pavl M. 2024년 11월 21일 11:47
I have tried to get prcc sensitve analysis graph, but unfortunately i got an error.I request you please help to overcome the error.
CODE:
% Define parameter ranges
paramRanges = struct('beta', [0.1, 0.5], 'sigma', [0.1, 0.3], 'gamma', [0.1, 0.4]);
% Number of samples
nSamples = 100;
% Number of parameters
paramNames = fieldnames(paramRanges);
nParams = numel(paramNames);
% Generate LHS samples
lhsSamples = lhsdesign(nSamples, nParams);
% Scale samples to parameter ranges
scaledSamples = zeros(nSamples, nParams);
for i = 1:nParams
low = paramRanges.(paramNames{i})(1);
high = paramRanges.(paramNames{i})(2);
scaledSamples(:, i) = low + lhsSamples(:, i) * (high - low);
end
% Define SEIR model parameters and calculate R0
% Run simulations to compute R0 for each parameter set
R0Values = zeros(nSamples, 1);
for i = 1:nSamples
R0Values(i) = calculateR0(scaledSamples(i, :));
end
% Calculate PRCC
function prcc = calculatePRCC(outputs, samples)
nParams = size(samples, 2);
prcc = zeros(nParams, 1);
for i = 1:nParams
% Rank data
ranksSamples = tiedrank(samples(:, i));
ranksOutputs = tiedrank(outputs);
% Compute correlation
prcc(i) = corr(ranksSamples, ranksOutputs);
end
end
% Perform PRCC analysis for R0 values
prccValues = calculatePRCC(R0Values, scaledSamples);
function R0 = calculateR0(params)
beta = params(1);
sigma = params(2);
gamma = params(3);
% Basic reproduction number for SEIR model
R0 = beta / gamma;
end
% Display results
for i = 1:nParams
fprintf('PRCC for %s: %.4f\n', paramNames{i}, prccValues(i));
end
ERROR:
Function definitions in a script must appear at the end of the file.
Move all statements after the "calculateR0" function definition to before the first local function definition.

답변 (3개)

Shivam Gothi
Shivam Gothi 2024년 9월 12일
I executed the code shared by you in MATLAB R2021a. I was able to reproduce the error as highlighted by you.
But, when I run the same code in MATLAB R2024a, it was executed without throwing error.
In older versions, all the function definations should go at the end of the file. This is not the case with R2024a version.
Therefore, there are two possible work arounds for this problem:
1) modify your code.
Upon inspecting the code provided by you in the comment section, I found that you have writen MATLAB commands after/in-between function definations, which is violating the defination:
"all the function definations should go at the end of the file"
I have re-organised your code and attached the corresponding (.m) file with this answer. I have compiled it in R2021a version and it is found to be executed without any error.
2) Update your MATLAB version
You can install the updated MATLAB version (R2024b) by refering the link:
R2024b allows to compile your code (shared in the question) without any error.
I hope this helps !
  댓글 수: 2
mallela ankamma rao
mallela ankamma rao 2024년 9월 12일
Thank you very much Shivam gothi sir
I request you please guide me to write prcc graph like below image by taking three parameters values
beta: 0.7196
sigma: 0.0342
gamma: -0.6795
Shivam Gothi
Shivam Gothi 2024년 9월 12일
I appreciate your efforts in seeking solutions. It might be beneficial to ask this as a new question in the MATLAB Answers community. There are many experts who might be able to assist you more effectively.
Additionally, if you find any of the responses you've received helpful, it would be great to provide the feedback or marking an answer as accepted.

댓글을 달려면 로그인하십시오.


Shivam Gothi
Shivam Gothi 2024년 9월 11일
편집: Shivam Gothi 2024년 9월 11일
I copied the same code provided by you in a new (.m) file and saved it with some name. Then I executed the file and it did not throw any error. Also, running the above code in the below given "code section" does not produce any error. But, I can suggest the possible cause behind it.
% Define parameter ranges
paramRanges = struct('beta', [0.1, 0.5], 'sigma', [0.1, 0.3], 'gamma', [0.1, 0.4]);
% Number of samples
nSamples = 100;
% Number of parameters
paramNames = fieldnames(paramRanges);
nParams = numel(paramNames);
% Generate LHS samples
lhsSamples = lhsdesign(nSamples, nParams);
% Scale samples to parameter ranges
scaledSamples = zeros(nSamples, nParams);
for i = 1:nParams
low = paramRanges.(paramNames{i})(1);
high = paramRanges.(paramNames{i})(2);
scaledSamples(:, i) = low + lhsSamples(:, i) * (high - low);
end
% Define SEIR model parameters and calculate R0
% Run simulations to compute R0 for each parameter set
R0Values = zeros(nSamples, 1);
for i = 1:nSamples
R0Values(i) = calculateR0(scaledSamples(i, :));
end
% Calculate PRCC
function prcc = calculatePRCC(outputs, samples)
nParams = size(samples, 2);
prcc = zeros(nParams, 1);
for i = 1:nParams
% Rank data
ranksSamples = tiedrank(samples(:, i));
ranksOutputs = tiedrank(outputs);
% Compute correlation
prcc(i) = corr(ranksSamples, ranksOutputs);
end
end
% Perform PRCC analysis for R0 values
prccValues = calculatePRCC(R0Values, scaledSamples);
function R0 = calculateR0(params)
beta = params(1);
sigma = params(2);
gamma = params(3);
% Basic reproduction number for SEIR model
R0 = beta / gamma;
end
% Display results
for i = 1:nParams
fprintf('PRCC for %s: %.4f\n', paramNames{i}, prccValues(i));
end
PRCC for beta: 0.7334 PRCC for sigma: -0.1026 PRCC for gamma: -0.6995
In MATLAB, all the function definations should go at the end of the file. Refer to the below MATLAB answer:
Therefore, I think that the compilation error in your case can be resolved by putting the "for loop" before the function definations as shown in below code:
I hope this helps !
  댓글 수: 5
Shivam Gothi
Shivam Gothi 2024년 9월 11일
Which version of MATLAB are you using?
mallela ankamma rao
mallela ankamma rao 2024년 9월 12일
MATLAB R2021a version sir

댓글을 달려면 로그인하십시오.


Pavl M.
Pavl M. 2024년 11월 21일 11:47
%just made it working on both TCE NCE MPPL Matlab versions:
clc
clear all
close all
% Define parameter ranges
paramRanges = struct('beta', [0.1, 0.5], 'sigma', [0.1, 0.3], 'gamma', [0.1, 0.4]);
% Number of samples
nSamples = 100;
% Number of parameters
paramNames = fieldnames(paramRanges);
nParams = numel(paramNames);
% Generate LHS samples
lhsSamples = lhsdesign(nSamples, nParams);
% Scale samples to parameter ranges
scaledSamples = zeros(nSamples, nParams);
for i = 1:nParams
low = paramRanges.(paramNames{i})(1);
high = paramRanges.(paramNames{i})(2);
scaledSamples(:, i) = low + lhsSamples(:, i) * (high - low);
end
% Define SEIR model parameters and calculate R0
% Run simulations to compute R0 for each parameter set
R0Values = zeros(nSamples, 1);
for i = 1:nSamples
R0Values(i) = calculateR0(scaledSamples(i, :));
end
% Calculate PRCC
% Perform PRCC analysis for R0 values
prccValues = calculatePRCC(R0Values, scaledSamples);
% Display results
for i = 1:nParams
fprintf('PRCC for %s: %.4f\n', paramNames{i}, prccValues(i));
end
PRCC for beta: 0.7228 PRCC for sigma: -0.1311 PRCC for gamma: -0.6203
function R0 = calculateR0(params)
beta = params(1);
sigma = params(2);
gamma = params(3);
% Basic reproduction number for SEIR model
R0 = beta / gamma;
end
function prcc = calculatePRCC(outputs, samples)
nParams = size(samples, 2);
prcc = zeros(nParams, 1);
for i = 1:nParams
% Rank data
ranksSamples = tiedrank(samples(:, i));
ranksOutputs = tiedrank(outputs);
% Compute correlation
prcc(i) = corr(ranksSamples, ranksOutputs);
end
end

카테고리

Help CenterFile Exchange에서 Physical Channels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by