air =[ 1982 735.403301816046
1983 955.709541669618
1984 836.702280403177
1985 759.837968749349
1986 743.081159793134
1987 697.243120496166
1988 961.544289566035
1989 866.734443173754
1990 908.628765340288
1991 785.456208433496
1992 785.023827180553
1993 866.907336535751
1994 952.85246593786
1995 800.380205594926
1996 858.673272277702
1997 871.561407987435
1998 859.468119732363
1999 822.134574790892
2000 778.44088008849
2001 793.614203610427
2002 662.197796567331
2003 849.679313577223
2004 744.987582369206
2005 857.190495548418
2006 870.222630405468
2007 935.418611378442
2008 887.323177948036
2009 667.971028328757
2010 845.966928408054];


tickers ={'AAPL','XOM','MSFT','BAC^I','IBM','CVX','GE','WMT','T','JNJ','PG','WFC','KO','PFE','JPM','GOOG','PM','VOD','ORCL','INTC','MRK','VZ','BRK/A','QCOM','PEP','CSCO','AMZN','ABT','MCD','SLB','C','BRK/B','BAC','RY','HD','DIS','TD','UTX','OXY','AXP','KFT','COP','MO','CAT','V','CMCSA','MMM','USB','AIG','EMC','CVS','BNS','UNH','BA','UPS','BMY','AMGN','UNP','GS','MA','EBAY','DD','HPQ','LLY','CL','SU','SPG','EPD','HON','LVS','ESRX','SBUX','NKE','ACN','F','MDT','SO','MON','TEVA','DOW','GILD','ABX','IMO','LOW','DHR','CNI','DB','TGT','PCLN','BMO','POT','MET','COST','EMR','BIDU','TXN','GM','TWX','CNQ','FCX','APC','PNC','PX','APA','BP','YUM','DTV','DE','NWSA','QQQ','MS','BCE','ENB','BIIB','TJX','KMB','CELG','BAX','TRP','NOV','HAL','D','GG','CM','MCI','WAG','RDS/A','DUK','LMT','DELL','EOG','AGN','FDX','ERIC','BK','SCCO','ITW','NEE','DVN','ADP','AMT','COV','INFY','ACE','BLK','EXC','TYC','BEN','PCP','TRV','COF','KMI','PRU','GIS','TWC','TRI','CVE','GD','PSA','AMX','CTL','VIAB','NSC','RAI','CSX','MFC','LYB','NEM','ISRG','STT','BBT','WLP','JCI','ADM','MPV','CTSH','MCK','CRM','COH','EP','GLW','CMI','SYK','AFL','CBS','BAM','TCK','ASML','CB','TMO','SE','FE','WMB','CCL','KMP','PSX','MRO','EQR','PCG','AEP','MMC','ECL','YHOO','SHPGY','CMCSK','APD','BHI','K','RTN','GSK','DFS','WPZ','SDRL','HES','ED','LO','SCHW','M','MJN','CME','HNZ','BRCM','PBR/A','VTR','WPPGY','NBL','TSM','ALL','RIG','GGP','INTU','HCP','VFC','VALE','BBBY','WFM','CCI','NVS','SYY','ALXN','L','F^A','PLD','BDX','PGN','ADBE','DG','PPG','VNO','BXP','PEG','VALE/P','NLY','PPL','AON','GR','WM','SRE','NOC','NWS','VRX','ECA','CTXS','TROW','ETN','AZO','MOS','RCI','AET','LGI','MSI','CAH','LTD','TEL','PCAR','AMAT','EL','ROST','EIX','A','GWW','CLR','IP','OMC','ATVI','AVB','EMQ','GPS','MPC','NTAP','VMW','CERN','STX','MHP','TVE','SLF','DEO','AGU','PGR','RDS/B','ORLY','PXD','XEL','ITUB','IIF','HUM','FAST','MAR','SLE','CI','KR','CMG','FITB','PH','WYNN','CA','STI','CP','PAA','VLO','IR','SHW','SWK','TDC','BAP','KSS','ESV','ABV','DLTR','CF','REGN','HCN','CAM','NUE','TLM','HOG','STJ','MMI','HST','HCA','MXF','MNST','JWN','ETR','SYMC','AMP','CHK','CHKP','HOT','RHT','NTRS','ARMH','ZMH','MAT','ADI','DOV','ROK','HSY','ETP','WU','ALTR','PAYX','MTB','WY','VIV','CPB','CAG','NLSN','SNY','IVZ','FTI','AUY','WFT','FFIV','JNPR','RL','RRC','SPLS','XRX','FMX','GPC','RYAAY','AMTD','LINTA','SWN','CXO','SLW','AA','RSG','MGA','CHRW','MUR','CBE','ROP','UN','FIS','EW','DLPH','FLR','PRGO','KGC','TOT','AGNC','DTE','LMCA','RF','BBD','NXY','BG','WPI','FISV','DAL','ABC','ICE','AES','BEAM','NE','WDC','GRMN','BPO','DGX','ETE','DO','FRX','OKE','THI','APH','BSX','MYL','XLNX','CIE','CCJ','CPN','MCO','CLX','CCE','SNDK','HIG','TU','CNP','BWA','SJM','LULU','ADSK','DPS','LH','WEC','EXPD','KORS','BCR','TIF','KLAC','AVP','CLF','SIAL','KSU','MAC','MRVL','SJR','CNA','SIRI','VRSK','AME','MXIM','FOSL','FDO','KIM','LIFE','BTU','DVA','DLR','MMP','VRTX','RAX','AEE','IVN','PFG','NTES','BHP','LINE','COL','AVGO','CIT','DISCA','MPEL','CNX','HRL','GOLD','OKS','NVDA','KEY','WYN','BF/B','WAT','EQIX','PWE','UAL','CHD','JCP'}

start_date='01011982';
end_date='01122011';
stocks = hist_stock_data(start_date,end_date,tickers{:}, 'frequency','m');

n = length(stocks);

all_days=NaN(12*30,1);
im=1;
for jj=1982:2011
  for ii=1:12
    all_days(im) = datenum([int2str(jj),'-',int2str(ii)]);
    im=im+1;
  end
end
  
dta = NaN(1,30*12);
iok=1;
for ii=1:n
  wrk = stocks(ii).Close;
  if length(wrk)==30*12
    dta(iok,:) = wrk;
    iok=iok+1;
  end
end

rdta = log(dta(:,5:12:end)./dta(:,4:12:end));

n=size(rdta,1)
for ii=1:n
  c(ii)=corr2(air(:,2),rdta(ii,1:29)');
end

[u,s,v] = svd(rdta,'econ');

return
stepwisefit(rdta(:,1:29)',air(:,2))
stepwisefit(v(1:29,:),air(:,2))

y = air(:,2);
x = [rdta([33,41],1:29);ones(1,29)]';
x = [v(1:29,[4,17,21])';ones(1,29)]';

b=regress(y,x);
yhat = x*b;