%% This demonstrates the variance of parameter meets the CRLB


% This code is taken and modified slightly from FitExample.m


%% Parameter setup

A=3.5;
B=7;
N=100;
sigma=10; %this is the sigma in normally distributed noise model

x=(1:N)';
y=A+B*x; %A and B are switched comparted to FitExample to match my notes

options=optimset;
func=@LSQfunc; %this points to the function that will be minimized


%% Create many data sets (observations) and estimate the parameters

N_experiments=100; %(N_experiments would have to be infinite to reach the CRLB)

clear A_found
clear B_found
for ii=1:N_experiments    
    y_noise=y+sigma*randn(size(y));
    X0=[1 1]; %these are the starting guesses
    [out]=fminsearch(func,X0,options,x,y_noise);
    A_found(ii)=out(2);
    B_found(ii)=out(1); %I switched these to match my notes
end

%% Compare our results to the CRLB

varA_crlb=(2*(2*N-1)*sigma^2)/(N*(N+1));
varB_crlb=(12*sigma^2)/(N*(N^2-1));

varA_observed=var(A_found);
varB_observed=var(B_found);

fprintf('\n')
fprintf('CRLB predicted variance of A: %g\n', varA_crlb)
fprintf('found variance of A:          %g\n\n', varA_observed)

fprintf('CRLB predicted variance of B: %g\n', varB_crlb)
fprintf('found variance of B:          %g\n\n', varB_observed)




