Advertisement
Guest User

mlap

a guest
May 4th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 11.54 KB | None | 0 0
  1. function MSE = task1(companyDataInputFileName, sectorDataInputFileName)
  2.  
  3. % Constant Date Format used in the input files.
  4. DATE_FORMAT = 'dd/mm/yyyy';
  5. % Constant First Value of 2009.
  6. FIRST_DATA = 254;
  7. % Constant number of Folds.
  8. FOLD_COUNT = uint8(5);
  9. % Constant Column References
  10. DATE                 = 1;
  11. COMPANY_STOCK_VOLUME = 2;
  12. COMPANY_STOCK_VALUE  = 3;
  13. SECTOR_STOCK_VOLUME  = 4;
  14. SECTOR_STOCK_VALUE   = 5;
  15. DELTA_COMPANY_VOLUME = 6;
  16. DELTA_COMPANY_VALUE  = 7;
  17. DELTA_SECTOR_VOLUME  = 8;
  18. DELTA_SECTOR_VALUE   = 9;
  19.  
  20.  
  21. % Load data from files
  22. companyData = loadCSVData(companyDataInputFileName);
  23. sectorData = loadCSVData(sectorDataInputFileName);
  24.  
  25. % Combine company and sector data.
  26. combinedData = [companyData(:,:) sectorData(:,2:3)];
  27. % Create data points of vectors.
  28. featureData = chooseFeatures(combinedData);
  29. % Create folds.
  30. folds = createFolds(featureData, FOLD_COUNT);
  31.  
  32. % Initialise squared errors to a vector of zeros.
  33. squaredErrors = zeros(FOLD_COUNT, 1);
  34. for i = 1:FOLD_COUNT
  35.     [trainingFolds, validationFold] = distributeFolds(folds, i);
  36.     theta = calculateTheta(trainingFolds);
  37.     % Add squared error for this fold to the vector.
  38.     squaredErrors(i) = calculateSquaredError(theta, validationFold);
  39. end
  40. % Return the mean of the squared errors.
  41. MSE = sum(squaredErrors) / length(featureData);
  42.  
  43.     % Load data from a csv file.
  44.     function data = loadCSVData(filename)
  45.         f = fopen(filename, 'r');
  46.         % Import all data.
  47.         data = textscan(f, repmat('%s',1,3), 'delimiter',',', 'CollectOutput',true);
  48.         % Remove header
  49.         data = data{1}(2:end,:);
  50.         fclose(f);
  51.         clearvars f;
  52.     end
  53.  
  54.     % Create data with the chosen features.
  55.     function featureData = chooseFeatures(data)
  56.         featureData = zeros(length(data));
  57.         for n = FIRST_DATA:length(data)
  58.             date = datenum(data(n, 1), DATE_FORMAT);
  59.             history = lastDays(date, data, 100);
  60.             inputs = [
  61.                 % Constant
  62.                 1;
  63.                
  64.                 %---------------%
  65.                 % FEATURE SET 1 %
  66.                 %---------------%
  67.                 %{
  68.                
  69.                 % last 5 days of company's stock prices.
  70.                 history(end-5:end-1, COMPANY_STOCK_VALUE);
  71.                 % Last 3 days of sector's average stock price.
  72.                 history(end-3:end-1, SECTOR_STOCK_VALUE);
  73.                 % Day of the week.
  74.                 weekday(date);
  75.                
  76.                 %}
  77.                 %---------------%
  78.                 % FEATURE SET 2 %
  79.                 %---------------%
  80.                 %{
  81.                
  82.                 % last 3 days of company's stock prices.
  83.                 history(end-3:end-1, COMPANY_STOCK_VALUE);
  84.                 % Last 5 days of sector's average stock price.
  85.                 history(end-5:end-1, SECTOR_STOCK_VALUE);
  86.                 % Day of the week.
  87.                 weekday(date);
  88.                
  89.                 %}
  90.                 %---------------%
  91.                 % FEATURE SET 3 %
  92.                 %---------------%
  93.                 %{
  94.  
  95.                 % last 10 days of company's stock prices.
  96.                 history(end-10:end-1, COMPANY_STOCK_VALUE);
  97.                
  98.                 %}
  99.                 %---------------%
  100.                 % FEATURE SET 4 %
  101.                 %---------------%
  102.                 %{
  103.                
  104.                 % last 2 days of company's stock prices.
  105.                 history(end-2:end-1, COMPANY_STOCK_VALUE);
  106.                
  107.                 %}
  108.                 %---------------%
  109.                 % FEATURE SET 5 %
  110.                 %---------------%
  111.                 %%{
  112.                
  113.                 % last 5 days of company's stock prices.
  114.                 history(end-5:end-1, COMPANY_STOCK_VALUE);
  115.                 % last 3 differences of sector average stock price.
  116.                 history(end-3:end-1, DELTA_COMPANY_VALUE);
  117.                
  118.                 %}
  119.                 %---------------%
  120.                 % FEATURE SET 6 %
  121.                 %---------------%
  122.                 %%{
  123.                
  124.                
  125.                
  126.                 %%}
  127.                 %---------------%
  128.                 % FEATURE SET 7 %
  129.                 %---------------%
  130.                 %%{
  131.                
  132.                
  133.                 %%}
  134.                 %---------------%
  135.                 % FEATURE SET 8 %
  136.                 %---------------%
  137.                 %%{
  138.                
  139.                
  140.                 %%}
  141.                 %---------------%
  142.                 % FEATURE SET 9 %
  143.                 %---------------%
  144.                 %%{
  145.                
  146.                
  147.                 %%}
  148.                 %----------------%
  149.                 % FEATURE SET 10 %
  150.                 %----------------%
  151.                 %%{
  152.                
  153.                
  154.                 %%}
  155.                 %----------------%
  156.                 % FEATURE SET 11 %
  157.                 %----------------%
  158.                 %%{
  159.                
  160.                
  161.                 %%}
  162.                 %----------------%
  163.                 % FEATURE SET 12 %
  164.                 %----------------%
  165.                 %%{
  166.                
  167.                
  168.                 %%}
  169.                 %----------------%
  170.                 % FEATURE SET 13 %
  171.                 %----------------%
  172.                 %%{
  173.                
  174.                
  175.                 %%}
  176.                 %----------------%
  177.                 % FEATURE SET 14 %
  178.                 %----------------%
  179.                 %%{
  180.                
  181.                
  182.                 %%}
  183.                 %----------------%
  184.                 % FEATURE SET 15 %
  185.                 %----------------%
  186.                 %%{
  187.                
  188.                
  189.                 %%}
  190.                 %----------------%
  191.                 % FEATURE SET 16 %
  192.                 %----------------%
  193.                 %%{
  194.                
  195.                
  196.                 %%}
  197.                 %----------------%
  198.                 % FEATURE SET 17 %
  199.                 %----------------%
  200.                 %%{
  201.                
  202.                
  203.                 %%}
  204.                 %----------------%
  205.                 % FEATURE SET 18 %
  206.                 %----------------%
  207.                 %%{
  208.                
  209.                
  210.                 %%}
  211.                 %----------------%
  212.                 % FEATURE SET 19 %
  213.                 %----------------%
  214.                 %%{
  215.                
  216.                
  217.                 %%}
  218.                 %----------------%
  219.                 % FEATURE SET 20 %
  220.                 %----------------%
  221.                 %%{
  222.                
  223.                
  224.                 %%}
  225.                 %----------------%
  226.                 % FEATURE SET 21 %
  227.                 %----------------%
  228.                 %%{
  229.                
  230.                
  231.                 %%}
  232.                 %----------------%
  233.                 % FEATURE SET 22 %
  234.                 %----------------%
  235.                 %%{
  236.                
  237.                
  238.                 %%}
  239.                 %----------------%
  240.                 % FEATURE SET 23 %
  241.                 %----------------%
  242.                 %%{
  243.                
  244.                
  245.                 %%}
  246.                 %----------------%
  247.                 % FEATURE SET 24 %
  248.                 %----------------%
  249.                 %%{
  250.                
  251.                
  252.                 %%}
  253.                 %----------------%
  254.                 % FEATURE SET 25 %
  255.                 %----------------%
  256.                 %%{
  257.                
  258.                
  259.                 %%}
  260.                 %----------------%
  261.                 % FEATURE SET 26 %
  262.                 %----------------%
  263.                 %%{
  264.                
  265.                
  266.                 %%}
  267.                 %----------------%
  268.                 % FEATURE SET 27 %
  269.                 %----------------%
  270.                 %%{
  271.                
  272.                
  273.                 %%}
  274.                 %----------------%
  275.                 % FEATURE SET 28 %
  276.                 %----------------%
  277.                 %%{
  278.                
  279.                
  280.                 %%}
  281.                 %----------------%
  282.                 % FEATURE SET 29 %
  283.                 %----------------%
  284.                 %%{
  285.                
  286.                
  287.                 %%}
  288.                 %----------------%
  289.                 % FEATURE SET 30 %
  290.                 %----------------%
  291.                 %%{
  292.                
  293.                
  294.                 %%}
  295.                
  296.             ]';
  297.             % Ouput is company stock price.
  298.             output = history(end, 3);
  299.             % Combine inputs and outputs.
  300.             featureData(n, 1:length(inputs)+1) = [inputs output];
  301.         end
  302.         % Shrink featureData down to what's used.
  303.         featureData = featureData(:, 1:length(inputs)+1);
  304.         % Only take the valid data.
  305.         featureData = featureData(FIRST_DATA:end, :);
  306.         clearvars numOfFeatures date history inputs output;
  307.     end
  308.  
  309.     % Split the data into K folds, or equally-sized subsamples of the data.
  310.     function folds = createFolds(data, k)
  311.         randomData = data(randperm(length(data)), :);
  312.         foldSize = idivide(length(data), k);
  313.         folds = {
  314.             randomData(1+0*foldSize:1*foldSize, :)
  315.             randomData(1+1*foldSize:2*foldSize, :)
  316.             randomData(1+2*foldSize:3*foldSize, :)
  317.             randomData(1+3*foldSize:4*foldSize, :)
  318.             randomData(1+4*foldSize:end, :)
  319.         };
  320.         clearvars randomData foldSize;
  321.     end
  322.  
  323.     % Create training and validation folds from some folds.
  324.     function [training, validation] = distributeFolds(folds, i)
  325.         validationFoldVector = folds(i);
  326.         validation = validationFoldVector{1};
  327.         trainingFoldVector = folds([1:i-1 i+1:end]);
  328.         training = vertcat(trainingFoldVector{1:end});
  329.         clearvars validationFoldVector trainingFoldVector;
  330.     end
  331.  
  332.     % Calculates theta (the regression coefficients)
  333.     function theta = calculateTheta(trainingData)
  334.         % Input matrix
  335.         X = trainingData(:, 1:end-1);
  336.         % Output vector
  337.         Y = trainingData(:, end);
  338.         theta = pinv(X'*X)*X'*Y;
  339.         clearvars X Y;
  340.     end
  341.  
  342.     % Return the sum of the squared errors.
  343.     function squaredError = calculateSquaredError(theta, validationData)
  344.         % Input matrix
  345.         X = validationData(:, 1:end-1);
  346.         % Target vector
  347.         target = validationData(:, end);
  348.         % Output vector
  349.         output = X * theta;
  350.         % Sum the Squared Error
  351.         squaredError = sum((target - output).^2);
  352.         clearvars X target output;
  353.     end
  354.  
  355.     % Return the most recent data preceding the date given (if there is
  356.     % data for that day, use it), and also get the last X data points
  357.     % before it, where X = days parameter.
  358.     function subset = lastDays(dateNum, data, days)
  359.         to = find(strcmp(data(:,1), datestr(dateNum, DATE_FORMAT)));
  360.         from = to - days;
  361.        
  362.         % Remove dates (not used after this point).
  363.         firstcolumn = zeros(to-from+1, 1);
  364.         % Original values from company and sector stock data.
  365.         % Removes date.
  366.         values = str2double(data(from:to,2:end));
  367.         % Changes in the above data (also doesn't include date).
  368.         deltas = str2double(data(from:to,2:end)) - str2double(data(from-1:to-1,2:end));
  369.        
  370.         % Return the above data.
  371.         subset = [firstcolumn values deltas];
  372.         clearvars from to deltas firstcolumn values deltas;
  373.     end
  374.  
  375. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement