0
votes

I am trying to maintain the variance-covariance matrix of a 10 asset portfolio of stocks. The data is represented by a column of returns for 10 stocks. I.e.

Stock A Stock B

Return 1 Return 1

Return 2 Return 2

Etc Etc

When I upload my data I have tried the cov(data) function but it will not work. I keep receiving this message:

"Undefined function 'sum' for input arguments of type 'dataset'.

Error in cov (line 154) xc = bsxfun(@minus,x,sum(x,1)/m); % Remove meanUndefined function 'sum' for input arguments of type 'dataset'.

Error in cov (line 154) xc = bsxfun(@minus,x,sum(x,1)/m); % Remove mean"

My question is how do I proceed with finding the average returns of each stock and the variance-covariance matrix?

1
Please add the code producing these errors.Adriaan
The source of the error seems to be that data is a dataset instead of a matrix.Some Guy
I've found a way around it by defining my data input as a numeric matrix as opposed to column vectors. The problem now is that Matlab will not read my first row in every column (i.e. the names of the stocks). What do I do now?Calum

1 Answers

0
votes

Dataset objects are being deprecated, so you should consider using tables instead. But with the dataset you want

>> dataAsMatrix = double(NameOfYourDataSetVariable);
>> cov(dataAsMatrix)
>> mean(dataAsMatrix)

To convert to a table use `dataset2table',

>> dataAsTable = dataset2table(NameOfYourDataSetVariable);

Then for some operations you can do things like

>> varfun(@mean,dataAsTable)

Although for cov I think you still need to extract the data

>> cov(dataAsTable{:,:})