Matlab で行列のインデックス作成がどのように機能するかについて少し混乱していると思います。
正しく理解されていればTR_t
、 time の値を格納する変数があります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 の記事をよく読んで、もう一度やり直すことをお勧めします。
しかし、何が起こっているのかをより迅速に理解できるようにするために、コードを書き直したバージョンを説明付きで示します。
// 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 StackOverflow question: http://stackoverflow.com/questions/30789390/
Matlab の仕組みを理解するのに役立つことを願っています。コード全般について助けが必要な場合は、Code Review Stack Exchangeにコードを投稿して、コードをより良く、よりクリーンにするための建設的な批判と提案を得ることを検討してください。