在 MATLAB 中重新引入 AR 和 GARCH 过程
Reintroduction of AR and GARCH processes in MATLAB
我正在尝试将自相关和异方差性重新引入到我的模拟残差中。我的模拟(标准化)残差具有维度(horizon
、nTrials
、nIndices
)。
为了计算今天的mean
/variance
(即t
),我需要使用最后一个时期mean
/variance
(即t-1
) 作为输入。这就是我卡住的地方,我无法将这部分代码获取到 运行。我尝试通过 {t-1}
(例如在 R_{t-1}
中)指定上一期间值的使用,但我收到错误消息,指出 R_
未定义。
如果有任何关于我哪里出错的提示,我将非常高兴。
卡罗琳
for i=1:nIndices
for j=1:nTrials
for t=1:horizon
R_t = AA_alpha(i,:) + AA_beta(i,:) * R_{t-1} + sqrt(h_{t-1}) * Z(t,j,i);
h_t = AA_alpha1(i,:)+AA_GARCH(i,:)*variances(j,i)+AA_ARCH(i,:)*Z({t-1},j,i) + AA_LEVERAGE*ZCopy{t-1}
TR_t = TR_{t-1} * exp(R_t);
end
end
end
我认为您对矩阵索引在 Matlab 中的工作方式有点困惑。
如果理解正确,您有一个变量 TR_t
,您要用它来存储时间 t
的值。
然后您尝试执行以下操作:
TR_t = TR_{t-1} * exp(R_t);
我会尽量用文字解释你在这里做什么:
Set scalar 'TR_t' as follows:
> Take cell matrix 'TR_' and take cell t-1
> Then multiply with exp(R_t)
此声明存在多个问题。首先,变量TR_
不存在,因为你将它命名为TR_t
。其次,您正试图索引这个标量,就好像它是一个单元格矩阵一样。
在继续之前,我建议您仔细研究Matrix Indexing Article并重试。
但只是为了帮助您更快地了解发生了什么,这里是您的代码的重写版本并附有解释,以便您可以按照我的操作进行操作。
// Variables that should already contain values before the loop starts
//**************************************************************************************
// >NAME< >INITIALISATION EXAMPLE< >DIMENSION<
//**************************************************************************************
AA_LEVERAGE = ? //zeros(1,1); // Should be a scalar
ZCopy = ? //zeros(1, horizon); // (1 x horizon)
variances = ? //zeros(nTrials, nIndices); // (nTrials x nIndices)
AA_alpha = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_alpha1 = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_beta = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_GARCH = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_ARCH = ? //zeros(nIndices, horizon); // (nIndices x horizon)
Z = ? //zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_R = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_h = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_TR = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
//**************************************************************************************
// START OF PROGRAM
//**************************************************************************************
for i=1:1:nIndices // For i number of indices (increment 1)
for j=1:1:nTrials // For j number of trials (increment 1)
R = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
h = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
TR = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
for t=2:1:horizon // For t number of horizons (increment 1) (start at 2)
// Assumes R(1,1) is known
R(1,t) = AA_alpha(i,:)+AA_beta(i,:)*R(1,t-1)+sqrt(h(1,t-1))*Z(i,j,t);
// Assumes ZCopy(1,1) is known
h(1,t) = AA_alpha1(i,:)+AA_GARCH(i,:)*variances(j,i)+AA_ARCH(i,:)*...
Z(i,j,t-1)+AA_LEVERAGE*ZCopy(1,t-1);
// Assumes TR(1,1) is known
TR(1,t) = TR(1,t-1)*exp(R(1,t));
end
// Save matrices R, h and TR before end of inner loop otherwise ..
// .. their information will be lost
// For example, you can store their values as follows:
SAVE_R(i,j,:) = R(1,:);
SAVE_h(i,j,:) = h(1,:);
SAVE_TR(i,j,:) = TR(1,:);
end
end
// If all variables initialised correctly, should produce output
// Written for Whosebug question:
我希望这有助于您理解 Matlab 的工作原理。如果您需要有关代码的一般帮助,请考虑在 Code Review Stack Exchange 上发布您的代码以获得建设性的批评和建议,从而使您的代码更好更简洁。
我正在尝试将自相关和异方差性重新引入到我的模拟残差中。我的模拟(标准化)残差具有维度(horizon
、nTrials
、nIndices
)。
为了计算今天的mean
/variance
(即t
),我需要使用最后一个时期mean
/variance
(即t-1
) 作为输入。这就是我卡住的地方,我无法将这部分代码获取到 运行。我尝试通过 {t-1}
(例如在 R_{t-1}
中)指定上一期间值的使用,但我收到错误消息,指出 R_
未定义。
如果有任何关于我哪里出错的提示,我将非常高兴。
卡罗琳
for i=1:nIndices
for j=1:nTrials
for t=1:horizon
R_t = AA_alpha(i,:) + AA_beta(i,:) * R_{t-1} + sqrt(h_{t-1}) * Z(t,j,i);
h_t = AA_alpha1(i,:)+AA_GARCH(i,:)*variances(j,i)+AA_ARCH(i,:)*Z({t-1},j,i) + AA_LEVERAGE*ZCopy{t-1}
TR_t = TR_{t-1} * exp(R_t);
end
end
end
我认为您对矩阵索引在 Matlab 中的工作方式有点困惑。
如果理解正确,您有一个变量 TR_t
,您要用它来存储时间 t
的值。
然后您尝试执行以下操作:
TR_t = TR_{t-1} * exp(R_t);
我会尽量用文字解释你在这里做什么:
Set scalar 'TR_t' as follows:
> Take cell matrix 'TR_' and take cell t-1
> Then multiply with exp(R_t)
此声明存在多个问题。首先,变量TR_
不存在,因为你将它命名为TR_t
。其次,您正试图索引这个标量,就好像它是一个单元格矩阵一样。
在继续之前,我建议您仔细研究Matrix Indexing Article并重试。
但只是为了帮助您更快地了解发生了什么,这里是您的代码的重写版本并附有解释,以便您可以按照我的操作进行操作。
// Variables that should already contain values before the loop starts
//**************************************************************************************
// >NAME< >INITIALISATION EXAMPLE< >DIMENSION<
//**************************************************************************************
AA_LEVERAGE = ? //zeros(1,1); // Should be a scalar
ZCopy = ? //zeros(1, horizon); // (1 x horizon)
variances = ? //zeros(nTrials, nIndices); // (nTrials x nIndices)
AA_alpha = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_alpha1 = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_beta = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_GARCH = ? //zeros(nIndices, horizon); // (nIndices x horizon)
AA_ARCH = ? //zeros(nIndices, horizon); // (nIndices x horizon)
Z = ? //zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_R = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_h = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
SAVE_TR = zeros(nIndices, nTrials, horizon); // (nIndices x nTrials x horizon)
//**************************************************************************************
// START OF PROGRAM
//**************************************************************************************
for i=1:1:nIndices // For i number of indices (increment 1)
for j=1:1:nTrials // For j number of trials (increment 1)
R = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
h = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
TR = zeros(1,horizon); // Re-initialise empty matrix (1 x horizon)
for t=2:1:horizon // For t number of horizons (increment 1) (start at 2)
// Assumes R(1,1) is known
R(1,t) = AA_alpha(i,:)+AA_beta(i,:)*R(1,t-1)+sqrt(h(1,t-1))*Z(i,j,t);
// Assumes ZCopy(1,1) is known
h(1,t) = AA_alpha1(i,:)+AA_GARCH(i,:)*variances(j,i)+AA_ARCH(i,:)*...
Z(i,j,t-1)+AA_LEVERAGE*ZCopy(1,t-1);
// Assumes TR(1,1) is known
TR(1,t) = TR(1,t-1)*exp(R(1,t));
end
// Save matrices R, h and TR before end of inner loop otherwise ..
// .. their information will be lost
// For example, you can store their values as follows:
SAVE_R(i,j,:) = R(1,:);
SAVE_h(i,j,:) = h(1,:);
SAVE_TR(i,j,:) = TR(1,:);
end
end
// If all variables initialised correctly, should produce output
// Written for Whosebug question:
我希望这有助于您理解 Matlab 的工作原理。如果您需要有关代码的一般帮助,请考虑在 Code Review Stack Exchange 上发布您的代码以获得建设性的批评和建议,从而使您的代码更好更简洁。