이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Arrary indices must be positive integers or logical values
조회 수: 4 (최근 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 (한국어)

