% % Mfile name
%   mtl_inp_sim_speextrapolation.m

% Revised: 
%   August 8, 2008

% Authors:
%   Nathan Collier, Luke Snyder, Dr. Autar Kaw
%   University of South Florida
%   kaw@eng.usf.edu
%   Website: http://numericalmethods.eng.usf.edu
       
% Purpose
%   To illustrate the dangers of extrapolation as a means of approximation.

% Keywords
%   Extrapolation
%   NASDAQ Composite

% Clearing all data, variable names, and files from any other source and
% clearing the command window after each succesive run of the program.
clc
clf
clear all

% INPUTS: This is the only place in the program where the user makes
% changes based on their wishes.  The following NASDAQ composite data is 
% taken from www.bigcharts.com, and serves the purpose of this example.  If
% different data is desired, please enter in the following sections.

    % Array of x-data
    endofyear=[1 2 3 4 5 6];

    % Array of y-data
    NASDAQ=[751.96 1052.13 1291.03 1570.35 2192.69 4069.31];

    % Extrapolated values
    e1 = 7;
    e2 = 8;

% Note: In the above scheme of representing years, year 7 would correspond
%       to 2000 and year 8 would correspond to 2001.

    actual = [2470.52 1950.4];
    
% Note: Range of years actually between 1994 (Year 1) and 1999 (Year 6).  
%       Numbers start from 1 to avoid round-off errors and near singularity 
%       in matrix calculations. End of Year NASDAQ Composite Data taken from
%       www.bigcharts.com

% *************************************************************************

disp(sprintf('\n\nAdditional Interpolation Topics: Danger of Extrapolation'))
disp(sprintf('\nUniversity of South Florida'))
disp(sprintf('United States of America'))
disp(sprintf('kaw@eng.usf.edu'))
disp(sprintf('\nNOTE: This worksheet utilizes Matlab to illustrate the concept'))
disp(sprintf('that extrapolation is often a poor means of approximation.'))

disp(sprintf('\n*****************************Introduction******************************'))

disp(sprintf('\nThe following simulation illustrates why extrapolation as a means of'))
disp(sprintf('approximation is often a bad idea.  During the technology boom of the late'))
disp(sprintf('1990^,s, the NASDAQ Composite Index that tracks mainly technology stocks had'))
disp(sprintf('a phenomenal increase in its value.  For example, at the end of year 1998,'))
disp(sprintf('the NASDAQ index was at an all time high of 1570.35.  It increased to 2192.69'))
disp(sprintf('by the end of 1999 and further to 4069.31 by the end of 2000.  Many people '))
disp(sprintf('who had never ventured into stocks started investing in the technology stocks'))
disp(sprintf('and mutual funds.  Dreams of doubling their money every year were not considered'))
disp(sprintf('unrealistic.  Given below is the data of the NASDAQ index from 1994 to 1999.'))
disp(sprintf('If the data had been extrapolated, what would be the estimation of the NASDAQ'))
disp(sprintf('index by the end of year 2000 and 2001?  How much different was the actual'))
disp(sprintf('index?  This simulation will show clearly why confidence in extrapolation'))
disp(sprintf('could have caused huge losses for those investing.'))

disp(sprintf('\n******************************Simulation*********************************'))    

% SOLUTION

% Creating the first figure of the extrapolated data using polynomial
% interpolation.
figure(1);

% Using polynomial interpolation to curve fit the data.
n=numel(endofyear);
p=polyfit(endofyear,NASDAQ,n-1);
x1=1:0.01:e2;
y1=polyval(p,x1);
plot(x1,y1,'k')
hold on
plot(endofyear, NASDAQ,'ro','MarkerSize',8,'MarkerFaceColor',[1,0,0])
plot([e1 e2], [polyval(p,e1) polyval(p,e2)],'b+','LineWidth',3,'MarkerSize',10,'MarkerFaceColor',[1,0,0])
plot([e1 e2], actual,'ro','MarkerSize',8,'MarkerFaceColor',[0,1,0])
title({'Data from 1994 to 1999 extrapolated using polynomial';'interpolation to yeild results for year 2000 and 2001'},'Fontweight','bold')
xlabel('\bfEnd of Year (starting from 1994)');
ylabel('\bfNASDAQ Composite Index');
legend(' = Polynomial Curve',' = Data Points',' = Extrapolated',' = Actual Index',4,'Location','Northwest')
hold off

% Calculating the percent relative true error for 2000 (polynomial 
% interpolation):
true_error00p = abs((actual(1)-polyval(p,e1))/actual(1)*100);

% Calculating the percent relative true error for 2001 (polynomial:
% interpolation):
true_error01p = abs((actual(2)-polyval(p,e2))/actual(2)*100);

%%%%%%%%% COMPARISON: Polynomial interpolation vs. Actual Values  %%%%%%%%%%%%

disp(sprintf('\n*********Table of Values:Polynomial Interpolation vs. Actual Values******'))
disp(sprintf('End of                        Polynomial          Percent Relative'))
disp(sprintf('Year          Actual          Interpolation       True Error'))
disp(sprintf('-------------------------------------------------------------------------'))

% Displaying the data in the table.
disp(sprintf('%g             %1.3e      %1.3e          %1.3e',e1,actual(1),polyval(p,e1),true_error00p));
disp(sprintf('%g             %1.3e      %1.3e          %1.3e',e2,actual(2),polyval(p,e2),true_error01p));


%%%%%%%% Regression using cubic splines %%%%%%%%

% Using cubic splines to interpolate and extrapolate the data for 2000 and
% 2001.
figure(2)

xx=1:0.01:e2;
yy=spline(endofyear,NASDAQ,xx);
plot(xx,yy)
hold on
fe1=spline(endofyear,NASDAQ,e1);
fe2=spline(endofyear,NASDAQ,e2);
plot(endofyear, NASDAQ,'ro','MarkerSize',8,'MarkerFaceColor',[1,0,0])
plot([e1 e2], [fe1 fe2],'b+','LineWidth',3,'MarkerSize',10,'MarkerFaceColor',[1,0,0])
plot([e1 e2], actual,'ro','MarkerSize',8,'MarkerFaceColor',[0,1,0])
title({'Data from 1994 to 1999 extrapolated using cubic';'splines to yield results for 2000 and 2001'},'Fontweight','bold')
xlabel('\bfEnd of Year (starting from 1994)');
ylabel('\bfNASDAQ Composite Index');
legend(' = Cubic Spline Curve',' = Data Points',' = Extrapolated',' = Actual Index',4,'Location','Northwest')
hold off

% Calculating the percent relative true error for 2000 (cubic spline):
true_error00cs = abs((actual(1) - fe1)/actual(1)*100);

% Calculating the percent relative true error for 2000 (cubic spline):
true_error01cs = abs((actual(2) - fe2)/actual(2)*100);

%%%%%%%%% COMPARISON: Cubic Splines vs actual data %%%%%%%%%%%%

disp(sprintf('\n********Table of Values:Cubic Spline Interpolation vs. Actual Values******'))
disp(sprintf('End of                        Cubic Spline        Percent Relative'))
disp(sprintf('Year          Actual          Interpolation       True Error'))
disp(sprintf('-------------------------------------------------------------------------'))

% Displaying the data in the table.
disp(sprintf('%g             %1.3e      %1.3e          %1.3e',e1,actual(1),fe1,true_error00cs));
disp(sprintf('%g             %1.3e      %1.3e          %1.3e',e2,actual(2),fe2,true_error01cs));


%%%%%%%%% Summary of all interpolation methods vs. the actual values %%%%%%

disp(sprintf('\n***Table of Values: Summary of Interpolation Methods vs. Actual Values*******'))
disp(sprintf('End of                Cubic Sp      Poly        |   Percent Relative True Error'))
disp(sprintf('Year     Actual       Interp        Interp      |   (Cubic)        (Poly)'))
disp(sprintf('------------------------------------------------------------------------------'))

% Displaying values inside the table.
disp(sprintf('%g        %1.3e   %1.3e    %1.3e      %1.3e     %1.3e',e1,actual(1),fe1,polyval(p,e1),true_error00cs,true_error00p))
disp(sprintf('%g        %1.3e   %1.3e    %1.3e      %1.3e     %1.3e',e2,actual(2),fe2,polyval(p,e2),true_error01cs,true_error01p))

