clear all, close all

load yahoo_1996_2010.txt
data = flipud(yahoo_1996_2010);
y = data(1:690);
t = (1:length(y))'; % dagar

plot(t,y,'.-')
title('Yahoo, Nasdaq,  1996 till Jan 1999')

% modellen (linjär)
A = [ones(size(t)) t];
c = A\y;
f = @(t) c(1) + c(2)*t;

figure(), subplot(2,1,1)
plot(t,y,'.-',t,f(t),'r');
title('Yahoo, Nasdaq,  1996 till Jan 1999')
subplot(2,1,2)
plot(t,y-f(t),'.-'), grid on, title('residualen')

% kubisk...
A = [ones(size(t)) t t.^2 t.^3];
c = A\y;
f3 = @(t) c(1) + c(2)*t + c(3)*t.^2 + c(4)*t.^3;

figure(), subplot(2,1,1)
plot(t,y,'.-',t,f3(t),'r'); 
title('Yahoo, Nasdaq,  1996 till Jan 1999')
subplot(2,1,2)
plot(t,y-f3(t),'.-'), grid on, title('residualen')

% verkligheten
figure()
yy = data(1:1300);
tt = 1:length(yy);
plot(tt,yy,'.-',tt,f3(tt),'r')
