0

我不断收到此错误:

???索引超出矩阵维度。

==> example3_6 在 48 个结果中出错=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) 返回一个 p×1 向量 b 的系数估计值,用于对 y 中的响应对 X 中的预测变量进行多线性回归。X 是 n 次观察中每个观测值的 p 个预测变量的 n×p 矩阵. y 是观察到的响应的 n×1 向量。

但由于我们不知道您如何在 ols 函数中使用回归函数,因此很难为您提供帮助。只需对您发送给 regress() 的两个参数运行 size(...) 检查,我相信您会发现 MATLAB 抱怨的原因。

于 2012-11-03T20:26:22.840 回答