# Demo entry 6646448

stochastic

Submitted by anonymous on Oct 16, 2017 at 23:19
Language: Matlab. Code size: 2.2 kB.

clear;
beta_hat = (x.' * x)^(-1) * x.' * y;
true_beta = true_beta.';

l = max(eig(1/length(y) * (x.') * x));
beta_est = zeros(30,1);
f_beta_hat = 1/(2*length(y))*(norm(y-x*beta_hat))^2;
index = 1:20;
index = index.';

for k = [0.1,1.7,1,0.01]
[diff_obj,diff_true_beta,diff_beta_hat] = get_diff(x,y,beta_est,true_beta,beta_hat,f_beta_hat,l,k);
get_plot(index, diff_obj, diff_true_beta, diff_beta_hat, k);
pause;
end

%,1.7,1,0.01

% %plot for questions (a)-(c) with step size of 0.1/L

% %plot for questions (a)-(c) with step size of 1.7/L

% %plot for questions (a)-(c) with step size of 1/L

% %plot for questions (a)-(c) with step size of 0.01/L

function [diff_obj,diff_true_beta,diff_beta_hat]=get_diff(x,y,beta_est,true_beta,beta_hat,f_beta_hat,l,step)
diff_obj = zeros(20,1);
diff_true_beta = zeros(20,1);
diff_beta_hat = zeros(20,1);
for i = 1:20
for j = 1:1000
beta_est = beta_est - step/l * x(j,:).' * (x(j,:) * beta_est - y(j));
end
diff_obj(i) = log(1/(2*length(y))*(norm(y-x*beta_est))^2 - f_beta_hat);
diff_true_beta(i) = (norm(beta_est - true_beta))^2;
diff_beta_hat(i) = (norm(beta_est - beta_hat))^2;
end
end

function get_plot(index, diff_obj, diff_true_beta, diff_beta_hat, step)
figure;
subplot(1,3,1);
plot(index, diff_obj);
xlabel('Number of pathes (k)');
ylabel('$$log(f(\beta^{(k)})-f(\widehat{\beta}))$$','Interpreter','Latex');
title(['Plot for (a) with step size of ' num2str(step) '/L']);
subplot(1,3,2);
plot(index, diff_true_beta);
xlabel('Number of pathes (k)');
ylabel('$$||\beta^{(k)} - \beta^\ast||^2_2$$','Interpreter','Latex');
title(['Plot for (b) with step size of ' num2str(step) '/L']);
subplot(1,3,3);
plot(index, diff_beta_hat);
xlabel('Number of pathes (k)');
ylabel('$$||\beta^{(k)} - \widehat{\beta}||^2_2$$','Interpreter','Latex');
title(['Plot for (c) with step size of ' num2str(step) '/L']);
end


This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.