You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Arrary indices must be positive integers or logical values
4 views (last 30 days)
Show older comments
for t=TestWindow
i=t-TestWindowStart+1;
EstimationWindow=t-EstimationWindowSize:t-1;
Sigma=std(Returns(EstimationWindow));
Normal95(i)=-Zscore(1)*Sigma;
Normal99(i)=-zscore(2)*Sigma;
end
Array indices must be positive integers or logical values.
Answers (1)
Cris LaPierre
on 13 Mar 2021
One of your indices is <= 0. You haven't shared enough information for us to say exactly what the problem is.
13 Comments
Thukten Tashi
on 13 Mar 2021
Edited: Walter Roberson
on 14 Mar 2021
I am trying to calculate value at risk. I have followed the steps as provied in https://uk.mathworks.com/help/risk/value-at-risk-estimation-and-backtesting.html, however it isn't working on my data. Please help. Here is the syntax that I used:
>> Returns=tick2ret(Price);
>> DateReturns=Date(2:end,:);
>> SampleSize=length(Returns);
>>
>> %%Deteremining 250 days
>> TestWindowStart=find(year(DateReturns)==2016,1);
>> TestWindow=TestWindowStart:SampleSize;
>> EstimationWindowSize=250;
>>
>> %%Confidence level
>> pVaR=[0.05 0.01];
>>
>> %%Normal method
>> Zscore=norminv(pVaR);
>> Normal95=zeros(length(TestWindow),1);
>> Normal99=zeros(length(TestWindow),1);
>>
>> for t=TestWindow
i=t-TestWindowStart+1;
EstimationWindow=t-EstimationWindowSize:t-1;
Sigma=std(Returns(EstimationWindow));
Normal95(i)=-Zscore(1)*Sigma;
Normal99(i)=-Zscore(2)*Sigma;
end
Array indices must be positive integers or logical values.
Cris LaPierre
on 13 Mar 2021
It's an indexing error, so check the value of the variables you are using for indexing. At least one of them is <=0, which is not allowed.
Walter Roberson
on 13 Mar 2021
EstimationWindow=t-EstimationWindowSize:t-1;
your t is 1. EstimationWindowSize is 250. 1-250:1-1 is -249 to 0.
Walter Roberson
on 13 Mar 2021
That is, the estimation window is looking before the current point, and so that fails if the current point is not at least EstimationWindowSize into the data.
Thukten Tashi
on 14 Mar 2021
How should I put it please? I am trying to estimate VaR on 251st day using the past 250 days and then continue the same for all the remaing days. I tried several ways, it doesn't seem to work. Your help would be highly appreciated.
Thukten Tashi
on 14 Mar 2021
Edited: Walter Roberson
on 14 Mar 2021
Still getting wrong. Although I am using 250 days to calculate one day Var on 251st day, the acutal days in some of the years is less than 250. I don't know how to account of that. Using t=251, I am still getting error. Here is the syntax.
>> Returns=tick2ret(Price);
DateReturns=Date(2:end,:);
SampleSize=length(Returns);
>>
>>
>> TestWindowStart=find(year(DateReturns)==2017,251);
>> TestWindow=TestWindowStart:SampleSize;
EstimationWindowSize=250;
>>
>>
>> pVaR=[0.05 0.01];
>> Zscore=norminv(pVaR);
Normal95=zeros(length(TestWindow),1);
>> Normal95=zeros(length(TestWindow),251);
>> Normal99=zeros(length(TestWindow),251);
>>
>> for t=TestWindow
i=t-TestWindowStart+1;
EstimationWindow=t-EstimationWindowSize:t-1;
Sigma=std(Returns(EstimationWindow));
Normal95(i)=-Zscore(1)*Sigma;
Normal99(i)=-Zscore(2)*Sigma;
end
Array indices must be positive integers or logical values.
Walter Roberson
on 14 Mar 2021
For one thing, do not take length() when you do not know for sure which dimension will be shorter. Use size() of the dimension you are interested in.
length(X) is defined as:
temporary = size(X)
if any(temporary==0)
length is 0
else
length is max(temporary)
end
Thukten Tashi
on 14 Mar 2021
I tried this way. I don't know if the VaR has been calculated as I couldn't plot graph.
Walter Roberson
on 15 Mar 2021
Notice your TestWindowStart is a vector of length 230 -- you have 230 entries with year 2017. Your find() asked for a maximum of 251 such entries, which is suspicious .
Notice that you did not ask for the first such entry: you asked for at most 251 of them, no matter how they are scattered through the data. You did not make any attempt to group them either, for the possibility that you had (say) 100 here and another 130 over there, that you wanted to (hypothetically) treat as two groups.
I suspect that your dates are sorted, and that you want the first such entry rather than up to 251 such entries.
Thukten Tashi
on 3 Apr 2021
I have calculated VaR using exponential weight moving average. The figure for first failire under EWMA at 95% confidence level appears as '1'. I thougth that this value should be 77 instead. I am not sure which one is correct. I have used the following steps.
Lambda = 0.94;
Sigma2 = zeros(length(Returns),1);
Sigma2(1) = Returns(1)^2;
for i = 2 : (TestWindowStart-1)
Sigma2(i) = (1-Lambda) * Returns(i-1)^2 + Lambda * Sigma2(i-1);
end
Zscore = norminv(pVaR);
EWMA95 = zeros(length(TestWindow),1);
EWMA99 = zeros(length(TestWindow),1);
for t = TestWindow
k = t - TestWindowStart + 1;
Sigma2(t) = (1-Lambda) * Returns(t-1)^2 + Lambda * Sigma2(t-1);
Sigma = sqrt(Sigma2(t));
EWMA95(k) = -Zscore(1)*Sigma;
EWMA99(k) = -Zscore(2)*Sigma;
end

See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
