Mary
Mary

Reputation: 788

Run time series analysis for multiple series Matlab

I wish to apply the same data analysis to multiple data time series. However the number of data series is variable. So instead of hard-coding each series' analysis I would like to be able to specify the number and name of the funds and then have the same data-manipulation done to all before they are combined into a single portfolio. Specifically I have an exel file where each worksheet is a time series where the first column is dates and the second column is prices. The dates for all funds may not correspond so the individual worksheets must be sifted for dates that occur in all the funds before combining into one data set where there is one column of dates and all other columns correspond to the data of each of the present funds. This combined data set is then analysed for means and variances etc. I currently have worked out how to carry out the merging and the analysis (below) but I would like to know how I can simply add or remove funds (i.e. by including new worksheets containing individual funds data in the excel file) without having to re-write and add/ remove extra/excess matlab code.

*% 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

Upvotes: 0

Views: 629

Answers (1)

Schorsch
Schorsch

Reputation: 7925

Based on edit to the question, the answer was rewritten

You can put your code into a function. This function can be saved as an .m-file and called from Matlab.
However, you want to replace the calls to specific worksheets (Fund1Prices=3) with an automated way of figuring out how many worksheets there are. Here's one way of how to do that in a function:

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

This function will return the Returns, q, qassetmean and qassetcovar variables for all the worksheets in the xls-file you want to process. The variable XL should be specified like this:

XL = 'my_file.xls';

You can also loop over more than one xls-file. Like this:

% 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

Make sure to store the values which are returned from the function in cells (as shown) or structs (not shown) to account for the fact that there may be a different number of sheets per file.

Upvotes: 2

Related Questions