現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)

