Reorganized the folders and started a common script for the lectures.

This commit is contained in:
2015-10-25 20:24:13 +01:00
parent dd50324683
commit 5791337dea
48 changed files with 1476 additions and 648 deletions

View File

@@ -1,24 +0,0 @@
nsamples = 100;
nresamples = 1000;
% draw a SRS (simple random sample, "Stichprobe") from the population:
x = randn( 1, nsamples );
fprintf('%-30s %-5s %-5s %-5s\n', '', 'mean', 'stdev', 'sem' )
fprintf('%30s %5.2f %5.2f %5.2f\n', 'single SRS', mean( x ), std( x ), std( x )/sqrt(nsamples) )
% bootstrap the mean:
mus = zeros(nresamples,1); % vector for storing the means
for i = 1:nresamples % loop for generating the bootstraps
inx = randi(nsamples, 1, nsamples); % range, 1D-vector, number
xr = x(inx); % resample the original SRS
mus(i) = mean(xr); % compute statistic of the resampled SRS
end
fprintf('%30s %5.2f %5.2f -\n', 'bootstrapped distribution', mean( mus ), std( mus ) )
% many SRS (we can do that with the random number generator, but not in real life!):
musrs = zeros(nresamples,1); % vector for the means of each SRS
for i = 1:nresamples
x = randn( 1, nsamples ); % draw a new SRS
musrs(i) = mean( x ); % compute its mean
end
fprintf('%30s %5.2f %5.2f -\n', 'sampling distribution', mean( musrs ), std( musrs ) )

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +0,0 @@
function error = lsq_error(parameter, x, y)
% parameter(1) is the slope
% parameter(2) is the intercept
f_x = x .* parameter(1) + parameter(2);
error = mean((f_x - y).^2);

View File

@@ -1,7 +0,0 @@
function gradient = lsq_gradient(parameter, x, y)
h = 1e-6;
partial_m = (lsq_error([parameter(1)+h, parameter(2)],x,y) - lsq_error(parameter,x,y))/ h;
partial_n = (lsq_error([parameter(1), parameter(2)+h],x,y) - lsq_error(parameter,x,y))/ h;
gradient = [partial_m, partial_n];

View File

@@ -1,9 +0,0 @@
function gradient = lsq_gradient_sigmoid(parameter, x, y)
h = 1e-6;
gradient = zeros(size(parameter));
for i = 1:length(parameter)
parameter_h = parameter;
parameter_h(i) = parameter_h(i) + h;
gradient(i) = (lsq_sigmoid_error(parameter_h, x, y) - lsq_sigmoid_error(parameter, x, y)) / h;
end

View File

@@ -1,8 +0,0 @@
function error = lsq_sigmoid_error(parameter, x, y)
% p(1) the amplitude
% p(2) the slope
% p(3) the x-shift
% p(4) the y-shift
y_est = parameter(1)./(1+ exp(-parameter(2) .* (x - parameter(3)))) + parameter(4);
error = mean((y_est - y).^2);

Binary file not shown.

View File

@@ -1,29 +0,0 @@
% draw random numbers:
n = 100;
mu = 3.0;
sigma =2.0;
x = randn(n,1)*sigma+mu;
fprintf(' mean of the data is %.2f\n', mean(x))
fprintf('standard deviation of the data is %.2f\n', std(x))
% mean as parameter:
pmus = 2.0:0.01:4.0;
% matrix with the probabilities for each x and pmus:
lms = zeros(length(x), length(pmus));
for i=1:length(pmus)
pmu = pmus(i);
p = exp(-0.5*((x-pmu)/sigma).^2.0)/sqrt(2.0*pi)/sigma;
lms(:,i) = p;
end
lm = prod(lms, 1); % likelihood
loglm = sum(log(lms), 1); % log likelihood
% plot likelihood of mean:
subplot(1, 2, 1);
plot(pmus, lm );
xlabel('mean')
ylabel('likelihood')
subplot(1, 2, 2);
plot(pmus, loglm );
xlabel('mean')
ylabel('log likelihood')

View File

@@ -1,112 +0,0 @@
clear
close all
%% first, plot the raw data
load('lin_regression.mat');
figure()
plot(x,y, 'o')
xlabel('Input')
ylabel('Output')
%% plot the error surface
clear
load('lin_regression.mat')
ms = -5:0.25:5;
ns = -30:1:30;
error_surf = zeros(length(ms), length(ns));
for i = 1:length(ms)
for j = 1:length(ns)
error_surf(i,j) = lsq_error([ms(i), ns(j)], x, y);
end
end
% plot the error surface
figure()
[N,M] = meshgrid(ns, ms);
s = surface(M,N,error_surf);
xlabel('slope')
ylabel('intercept')
zlabel('error')
view(3)
% rotate(s, [1 1 0], 25 )
%% Plot the gradient at different points in the surface
clear
load('lin_regression.mat')
ms = -1:0.5:5;
ns = -10:1:10;
error_surf = zeros(length(ms), length(ns));
gradient_m = zeros(size(error_surf));
gradient_n = zeros(size(error_surf));
for i = 1:length(ms)
for j = 1:length(ns)
error_surf(i,j) = lsq_error([ms(i), ns(j)], x, y);
grad = lsq_gradient([ms(i), ns(j)], x, y);
gradient_m(i,j) = grad(1);
gradient_n(i,j) = grad(2);
end
end
figure()
hold on
[N, M] = meshgrid(ns, ms);
surface(M,N, error_surf, 'FaceAlpha', 0.5);
contour(M,N, error_surf, 50);
quiver(M,N, gradient_m, gradient_n)
view(3)
xlabel('slope')
ylabel('intercept')
zlabel('error')
%% do the gradient descent
clear
close all
load('lin_regression.mat')
ms = -1:0.5:5;
ns = -10:1:10;
position = [-2. 10.];
gradient = [];
error = [];
eps = 0.01;
% claculate error surface
error_surf = zeros(length(ms), length(ns));
for i = 1:length(ms)
for j = 1:length(ns)
error_surf(i,j) = lsq_error([ms(i), ns(j)], x, y);
end
end
figure()
hold on
[N, M] = meshgrid(ns, ms);
surface(M,N, error_surf, 'FaceAlpha', 0.5);
view(3)
xlabel('slope')
ylabel('intersection')
zlabel('error')
% do the descent
while isempty(gradient) || norm(gradient) > 0.1
gradient = lsq_gradient(position, x,y);
error = lsq_error(position, x, y);
plot3(position(1), position(2), error, 'o', 'color', 'red')
position = position - eps .* gradient;
pause(0.25)
end
disp('gradient descent done!')
disp(strcat('final position: ', num2str(position)))
disp(strcat('final error: ', num2str(error)))

View File

@@ -1,44 +0,0 @@
%% fit the sigmoid
clear
close all
load('iv_curve.mat')
figure()
plot(voltage, current, 'o')
xlabel('voltate [mV]')
ylabel('current [pA]')
% amplitude, slope, x-shift, y-shift
%parameter = [10 0.25 -50, 2.5];
parameter = [20 0.5 -50, 2.5];
eps = 0.1;
% do the descent
gradient = [];
steps = 0;
error = [];
while isempty(gradient) || norm(gradient) > 0.01
steps = steps + 1;
gradient = lsq_gradient_sigmoid(parameter, voltage, current);
error(steps) = lsq_sigmoid_error(parameter, voltage, current);
parameter = parameter - eps .* gradient;
end
plot(1:steps, error)
disp('gradient descent done!')
disp(strcat('final position: ', num2str(parameter)))
disp(strcat('final error: ', num2str(error(end))))
%% use fminsearch
parameter = [10 0.5 -50, 2.5];
objective_function = @(p)lsq_sigmoid_error(p, voltage, current);
param = fminunc(objective_function, parameter);
disp(param)
param1 = fminsearch(objective_function, parameter);
disp(param1)