0

我希望将相同的数据分析应用于多个数据时间序列。然而,数据系列的数量是可变的。因此,我不想对每个系列的分析进行硬编码,而是希望能够指定基金的数量和名称,然后对所有基金进行相同的数据处理,然后再将它们组合成一个投资组合。具体来说,我有一个 exel 文件,其中每个工作表都是一个时间序列,其中第一列是日期,第二列是价格。所有基金的日期可能不对应,因此在组合成一个数据集之前,必须针对所有基金中出现的日期对各个工作表进行筛选,其中有一列日期,所有其他列对应于每个当前基金的数据. 然后分析该组合数据集的均值和方差等。

*% LOAD DATA*

XL='XLData.xlsx';
formatIn = 'dd/mm/yyyy';
formatOut = 'mmm-dd-yyyy';

*%SPECIFY WORKSHEETS*

Fund1Prices=3;
Fund2Prices=4;

*%RETRIEVE VALUES*

[Fund1values, ~, Fund1sheet] = xlsread(XL,Fund1Prices);
[Fund2values, ~, Fund2sheet] = xlsread(XL,Fund2Prices);

*%EXTRACT DATES AND DATA AND COMBINE (TO REMOVE UNNECCESSARY TEXT IN ROWS 1
%TO 4) FOR FUND 1.*

Fund1_dates_data=Fund1sheet(4:end,1:2);
Fund1Dates= cellstr(datestr(datevec(Fund1_dates_data(:,1),formatIn),formatOut));
Fund1Data= cell2mat(Fund1_dates_data(:,2));

*%EXTRACT DATES AND DATA AND COMBINE (TO REMOVE UNNECCESSARY TEXT IN ROWS 1
%TO 4) FOR FUND 2.*

Fund2_dates_data=Fund2sheet(4:end,1:2);
Fund2Dates= cellstr(datestr(datevec(Fund2_dates_data(:,1),formatIn),formatOut));
Fund2Data= cell2mat(Fund2_dates_data(:,2));

*%CREATE TIME SERIES FOR EACH FUND*

Fund1ts=fints(Fund1Dates,Fund1Data,'Fund1');
Fund2ts=fints(Fund2Dates,Fund2Data,'Fund2');

*%CREATE PORTFOLIO*

Port=merge(Fund1ts,Fund2ts,'DateSetMethod','Intersection');

*%ANALYSE PORTFOLIO*

Returns=tick2ret(Port);
q = Portfolio;
q = q.estimateAssetMoments(Port)
[qassetmean, qassetcovar] = q.getAssetMoments
4

1 回答 1

2

根据对问题的编辑,答案被重写

您可以将代码放入function. 这function可以保存为.m-file 并从 Matlab 调用。
但是,您希望Fund1Prices=3用一种自动计算有多少工作表的方法来替换对特定工作表 ( ) 的调用。这是如何在函数中执行此操作的一种方法:

function [Returns,q,qassetmean,qassetcovar] = my_data_series_analysis(XL)

% All input this function requires is a variable
% containing the name of the xls-file you want to process

formatIn = 'dd/mm/yyyy';
formatOut = 'mmm-dd-yyyy';

% Determine the number of worksheets in the xls-file:

[~,my_sheets] = xlsfinfo(XL);

% Loop through the number of sheets
% (change the start value if the first sheets do not contain data):

% this is needed to merge your portfolio
% in case you do not start the for-loop at I=1

merge_count = 1; 

for I=1:size(my_sheets,2)

    % RETRIEVE VALUES 
    % note that Fund1Prices has been replaced with the loop-iterable, I

    [FundValues, ~, FundSheet] = xlsread(XL,I);

    % EXTRACT DATES AND DATA AND COMBINE
    % (TO REMOVE UNNECCESSARY TEXT IN ROWS 1 TO 4)

    Fund_dates_data = FundSheet(4:end,1:2);
    FundDates = cellstr(datestr(datevec(Fund_dates_data(:,1),...
                                        formatIn),formatOut));
    FundData = cell2mat(Fund_dates_data(:,2));

    % CREATE TIME SERIES FOR EACH FUND

    Fundts{I}=fints(FundDates,FundData,['Fund',num2str(I)]);

    if merge_count == 2
        Port = merge(Fundts{I-1},Fundts{I},'DateSetMethod','Intersection');
    end
    if merge_count > 2
        Port = merge(Port,Fundts{I},'DateSetMethod','Intersection');
    end

    merge_count = merge_count + 1;

end

% ANALYSE PORTFOLIO

Returns=tick2ret(Port);
q = Portfolio;
q = q.estimateAssetMoments(Port)
[qassetmean, qassetcovar] = q.getAssetMoments

此函数将返回要处理的 -file中所有工作表的Returnsq和变量。变量应该这样指定: qassetmeanqassetcovarxlsXL

XL = 'my_file.xls';

您还可以遍历多个xls-file。像这样:

% use a cell so that the file names can be of different length:
XL = {'my_file.xls'; 'my_file2.xls'}

for F=1:size(XL,1)
    [Returns{F},q{F},qassetmean{F},qassetcovar{F}] = my_data_series_analysis(XL{F,1});
end

确保将函数返回的值存储在cells(如图所示)或structs(未显示)中,以说明每个文件可能有不同数量的工作表。

于 2013-06-19T11:36:31.017 回答