Arrary indices must be positive integers or logical values
1 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Cris LaPierre
2021-3-13
One of your indices is <= 0. You haven't shared enough information for us to say exactly what the problem is.
13 个评论
Thukten Tashi
2021-3-13
编辑:Walter Roberson
2021-3-14
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
2021-3-13
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
2021-3-13
EstimationWindow=t-EstimationWindowSize:t-1;
your t is 1. EstimationWindowSize is 250. 1-250:1-1 is -249 to 0.
Walter Roberson
2021-3-13
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
2021-3-14
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
2021-3-14
编辑:Walter Roberson
2021-3-14
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
2021-3-14
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
2021-3-14
I tried this way. I don't know if the VaR has been calculated as I couldn't plot graph.
Walter Roberson
2021-3-15
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
2021-4-3
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

另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

