【问题标题】:Looping issue in MATLABMATLAB中的循环问题
【发布时间】:2021-02-28 06:37:43
【问题描述】:

这是我正在处理的一个非常基本的问题。

我已经模拟了一个包含 6 个变量的数据集,我想测量每个向量的动态极值差。

我试图以某种方式循环以下代码,最终变量 ES 将呈现一个矩阵,该矩阵将有 T 行和 6 列遍历模拟矩形矩阵中的所有数据点。

clc
clear
%% Simulate data set
mu = [0 0 0 0 0 0];
A = rand(6);
Sigma= A * A';

%% Simulation of variables
rng('default')  % For reproducibility
Data = mvnrnd(mu,Sigma,1000);
r=Data(:,1);

我想使用 r = Data,因为估计在所有列而不是每列上运行,并且最终的 ESmatrix 将所有列绘制在一个图中。

 T= length(r);
    conditionalvariance=[];
    p= [0.025];
    VarMdl = garch(1,1)
    Mdl = arima('ARLags',1,'Variance',VarMdl);
    EstMdl = estimate(Mdl,r);
    [res,v,logL] = infer(EstMdl,r);
    conditionalvariance=[conditionalvariance,v];
    Sigma=conditionalvariance;
    ESdynamic=[];
    VaRdynamic=[];
    bpoe=[];
    for J= 1:T
    [Var_Normal, ES_Normal]=hNormalVaRES(Sigma(J),p);
    VaR=Var_Normal;
    ES=ES_Normal;
    disp(J)
    disp('');
    ESdynamic=[ESdynamic,ES];
    VaRdynamic=[]
    end
    ES=ESdynamic;
    Local Functions:
    function [VaR,ES] = hNormalVaRES(Sigma,p)
        % Compute VaR and ES for normal distribution
        % See [4] for technical details
        
        VaR = -norminv(p);
        ES = -Sigma*quad(@(q)q.*normpdf(q),-6,-VaR)/p;
    
    end

当我绘制(ES)时,我想要一个包含 6 个变量的图。

请帮我解决这个循环问题。

【问题讨论】:

    标签: matlab for-loop while-loop nested-loops


    【解决方案1】:

    不确定该图是否应该看起来像这样,但使用额外的外部 for 循环来索引 p 的值并多次运行该函数可用于实现此结果。为了绘制相同的坐标轴,在第一次调用 plot() 之后引入了 hold on 属性。也不确定p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001] 是否正确,因为您提到它是针对六个不同的p 值。在问题中,0.10,1

    clc
    clear
    % Simulate data set
    mu = [0 0 0 0 0 0];
    A = rand(6);
    Sigma= A * A';
    
    % Simulation of variables
    rng('default')  % For reproducibility
    Data = mvnrnd(mu,Sigma,1000);
    r=Data(:,1);
    T= length(r);
    conditionalvariance=[];
    p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001];
    
    
    VarMdl = garch(1,1)
    Mdl = arima('ARLags',1,'Variance',VarMdl);
    EstMdl = estimate(Mdl,r);
    [res,v,logL] = infer(EstMdl,r);
    conditionalvariance=[conditionalvariance,v];
    Sigma=conditionalvariance;
    ESdynamic=[];
    VaRdynamic=[];
    bpoe=[];
    
    for P_Index = 1: +1: length(p) 
    P_Value = p(P_Index);
    for J= 1:T
    [Var_Normal, ES_Normal]=hNormalVaRES(Sigma(J),P_Value);
    VaR = Var_Normal;
    ES = ES_Normal;
    disp(J)
    disp('');
    ESdynamic = [ESdynamic,ES];
    VaRdynamic = []
    end
    ES_Matrix(:,P_Index) = ESdynamic';
    plot(ES_Matrix(:,P_Index));
    hold on
    
    ESdynamic = [];
    end 
    hold off
    
    function [VaR,ES] = hNormalVaRES(Sigma,p)
        % Compute VaR and ES for normal distribution
        % See [4] for technical details
        
        VaR = -norminv(p);
        ES = -Sigma*quad(@(q)q.*normpdf(q),-6,-VaR)/p;
    
    end
    

    使用 MATLAB R2019b 运行

    【讨论】:

    • MichaelTr7,非常感谢我的朋友。虽然这是研究事物的一种方式,但我想要的是将 ESdynamic 作为矩阵,其中列不是 p 值,而是将每列视为不同的变量。让我们只考虑 p=0.05。但是我怎样才能运行一个循环来制作一个 ESdynamic 矩阵。对每一列进行 ES 分析。我希望他们通过循环在所有列上工作。所以,而不是使用 r=Data(:,1);...我希望循环通过 r = Data(:, 1:end) 只有 p=0.05。如何编辑该伙伴的代码?
    • 我不知道你到底想要什么。一个更简化的示例来说明您想要实现的目标可能会帮助我弄清楚您想要做什么。
    • 嗨 MichaelTr7,谢谢你的伙伴。我已经更新了问题和代码供您考虑。我希望为循环中的每一列估计 ES,因为我们有一个矩阵作为输入。
    • 嗨 MichaelTr7,我已经在 Create a matrix by looping the estimation in columns 中更新了这个问题......如果你可以在那里发表评论并且帮助真的很棒
    • 非常感谢。这解决了我的问题。
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多