MATLAB Answers


How can I effeciently store about 200 months of multivariate regression data?

Asked by Andrew Kreitzman on 2 Jul 2013

So I am running a regression of monthly returns for about 30000 stocks (each regression is run separately for stocks grouped by country) over anout 9 different factors. I am using this for loop to loop through every file, because each month's data is stored in a differentfile. I am trying to use a structure with fields to represent all of the outputs of my multivariate regression function, but when the regression is complete, every part of the 1x196 structure labeled 'regression_structure' is exactly the same! It just copied over the first month's rgeression 196 times. Where did I go wrong!?

for i=1:num_months
    % need to pass file names for monthly data files
    file1 = MRET_files(i).name;
    file2 = RSK_files(i).name; 
    [industry_rep_cell, country_rep_array,u_country_codes, u_industries, beta, mv] = Factor_Returns_Function(file1, file2);
      regression_structure(i).rep_industries = industry_rep_cell;
      regression_structure(i).rep_countries = country_rep_array;
      regression_structure(i).all_industries = u_industries;
      regression_structure(i).all_countries = u_country_codes;
      regression_structure(i).beta_simple = beta;
      regression_structure(i).beta_multi = mv.beta1;



No products are associated with this question.

1 Answer

Answer by the cyclist
on 2 Jul 2013
Edited by the cyclist
on 2 Jul 2013

This simple code gives different values for each element of the structure:

for i=1:196
    [B,bint] = regress(rand(3,1),rand(3,1));
     regression_structure(i).B = B;
     regression_structure(i).bint = bint;

This test suggests to me that the problem is either

  1. Your structures MRET_files(i).name and RSK_files(i).name are not actually different for different values of i, or
  2. Your function Factor_Returns_Function doesn't actually process the inputs the way you think

I can't test those hypotheses.


Do you know how to use debug mode? You can step through your code line-by-line (probably just for i=1 and i=2 in your case) to see why there are no differences in the result, even when then inputs are different.

Ill check that out! I am teaching myself matlab while i use it at work, so i am learning new stuff every day. Thanks!

This may help you understand the debugging tools:

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!