2

I am trying reintroduce autocorrelation and heteroskedasticity to my simulated residuals. My simulated (standardized) residuals have the dimension (horizon, nTrials, nIndices). In order to calculate today's mean / variance (i.e. t), I need to use the last periods mean /variance (i.e. t-1) as an input. This is where I am stuck, I can't get this part of the code to run. I try to specify the use of last periods value by {t-1} (for example in R_{t-1}), but I receive the error message that R_ is undefined.

I would be very happy about any hints on where I am going wrong here.

Carolin

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
Joe
  • 457
  • 5
  • 18
Carolin
  • 539
  • 2
  • 7
  • 15
  • You can't get the code to run or you don't know how to implement it? – Jean-Paul Jun 11 '15 at 19:09
  • I can't get the code to run. My implementation approach should be fine, but I am not 100% certain about the necessity and positions of the for loops. – Carolin Jun 11 '15 at 19:14
  • In that case your question is ill-defined. Please update your question with a [**SSCCE**](http://sscce.org/). – Jean-Paul Jun 11 '15 at 20:01
  • Hi Jean, thanks for your Tips! You were right, my question was not very well defined. I have tried to explain my problem in a more concise way now. – Carolin Jun 11 '15 at 20:36
  • You still haven't posted a [SSCCE](http://sscce.org/) but I have tried to answer your question. See below. – Jean-Paul Jun 11 '15 at 21:28

1 Answers1

1

I think you are a bit confused about how matrix indexing works in Matlab.

If understood correctly, you have a variable TR_t with which you want to store the value for time t. You then try to do the following:

TR_t      = TR_{t-1} * exp(R_t);

I will try to explain in words what you are doing here:

Set scalar 'TR_t' as follows:
> Take cell matrix 'TR_' and take cell t-1
> Then multiply with exp(R_t)

There are multiple problems with this statement. First of all, the variable TR_ doesn't exist because you named it TR_t. Second of all, you are trying to index this scalar as if it is a cell matrix.

Before proceeding, I suggest you carefully study the Matrix Indexing Article and try again.

But just to help you understand what is going on quicker, here is a rewritten version of your code with explanation so you can follow what I'm doing.

// 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/

I hope that helps with your understanding of how Matlab works. If you want help with your code in general, consider posting your code on Code Review Stack Exchange to get constructive criticism and suggestions for making your code better and cleaner.

Community
  • 1
  • 1
Jean-Paul
  • 19,910
  • 9
  • 62
  • 88
  • 1
    Hi Jean, many thanks for your efforts! I have managed to implement your code now! I am still a quite inexperienced user of Matlab, so it is sometimes it a bit difficult to understand how matlab processes my matrices. Thanks a lot again for your help, I really appreciate it! Best, Carolin – Carolin Jun 13 '15 at 13:39
  • @Carolin You are very welcome. Everyone has to start somewhere and you are clearly making the effort :) Good luck with your project and you can always ask more questions on SO! – Jean-Paul Jun 13 '15 at 14:35