0

このエラーが発生し続けます:

??? インデックスがマトリックスの次元を超えています。

エラー ==> example3_6 at 48 results=regress(cl1(trainset), cl2(trainset)); % 回帰関数を使用

GDX ファイルには 7 列と 386 行が含まれ、GLD ファイルには 7 列と 765 行が含まれますが、両方から 250 のサンプルを取得する場合、これは問題になりません。

誰かがここで何が問題なのかアドバイスできますか?

ありがとうございました

clear; % make sure previously defined variables are erased.

[num, txt]=xlsread('GLD'); % read a spreadsheet named "GLD.xls" into MATLAB. 

tday1=txt(2:end, 1); % the first column (starting from the second row) is the trading days in format mm/dd/yyyy.

tday1=datestr(datenum(tday1,'mm/dd/yyyy'), 'yyyymmdd');

tday1=str2double(cellstr(tday1)); % convert the date strings first into cell arrays and then into numeric format.

adjcls1=num(:, end); % the last column contains the adjusted close prices.

[num, txt]=xlsread('GDX'); % read a spreadsheet named "GDX.xls" into MATLAB. 

tday2=txt(2:end, 1); % the first column (starting from the second row) is the trading days in format mm/dd/yyyy.

tday2=datestr(datenum(tday2,'mm/dd/yyyy'), 'yyyymmdd');

tday2=str2double(cellstr(tday2)); % convert the date strings first into cell arrays and then into numeric format.

adjcls2=num(:, end); % the last column contains the adjusted close prices.

[tday, idx1, idx2]=intersect(tday1, tday2); % find the intersection of the two data sets, and sort them in ascending order

cl1=adjcls1(idx1); 

cl2=adjcls2(idx2);  

trainset=1:252; % define indices for training set

testset=trainset(end)+1:length(tday); % define indices for test set

% determines the hedge ratio on the trainset
results=ols(cl1(trainset), cl2(trainset)); % use regression function 
hedgeRatio=results.beta;
4

1 に答える 1

0

回帰関数の呼び出しに問題があるようです。

ドキュメント回帰によると:

regress(y,X) は、X の予測子に対する y の応答の多重線形回帰の係数推定値の p 行 1 列のベクトル b を返します。X は、n 個の観測のそれぞれにおける p 個の予測子の n 行 p 列の行列です。 . y は、観測された応答の n 行 1 列のベクトルです。

しかし、ols 関数で regress 関数をどのように使用しているかがわからないため、お手伝いするのはかなり困難です。regress() に送信する 2 つの引数に対して size(...) チェックを実行するだけで、MATLAB が不平を言っている理由がわかるはずです。

于 2012-11-03T20:26:22.840 に答える