% Tutorial: Bayesian Linear Regression

close all;
% set seed for random number generator
rand('seed',1);

% generate data.
numExamples = 10;
xGrid = (-1:0.001:1)';
x = randsample(xGrid,numExamples);
% polynom: y = x^3 + 0.7x^2-0.5x + 0.2 plus
y = x.^3+0.7*x.^2-0.5*x+0.2;
% add some noise
y = y + 0.05*randn(length(x),1);

% plot data
scatter(x,y,'filled');
axis([-1 1 0 1]);
hold on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TASK 1: Plot original polynom
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
XGrid = [ones(size(xGrid)) xGrid xGrid.^2 xGrid.^3];

% YOUR CODE GOES HERE

hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% generate polynomial features to train linear regression
X = [ones(size(x)) x x.^2 x.^3];

% posterior of bayesian linear regression (see lecture): normal
% distribution with parameter thetaStar and A^-1
I = eye(size(x,2));
sigma = 0.05;
sigma_p = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TASK 2: Compute MAP-model and covariance-matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% YOUR CODE GOES HERE
thetaStar = 0;
A = 0 ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% draw sample from posterior
numSamples = 3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TASK 3: Sample polynoms from posterior
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% YOUR CODE GOES HERE (Note: W = numSamples x 4)
W = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TASK 4: Plot sampled polynoms
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% YOUR CODE GOES HERE (Note: you need thetaStar and A)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


