计算岭回归MATLAB

时间:2017-01-12 02:24:55

标签: matlab regression variance

我无法理解如何从随机集中计算偏差和方差。

我已创建代码以生成随机正常数字集。

% Generate random w, x, and noise from standard Gaussian
w = randn(10,1);
x = randn(600,10);
noise = randn(600,1);

然后提取y

y = x*w + noise;

之后,我将数据分成训练(100)和测试(500)集

% Split data set into a training (100) and a test set (500)
x_train = x([  1:100],:);
x_test  = x([101:600],:);
y_train = y([  1:100],:);
y_test  = y([101:600],:);
train_l = length(y_train);
test_l  = length(y_test);

然后我计算w特定值lambda(1.2)

lambda = 1.2;

% Calculate the optimal w
A = x_train'*x_train+lambda*train_l*eye(10,10);
B = x_train'*y_train;
w_train = A\B;

最后,我正在计算平方误差:

% Compute the mean squared error on both the training and the 

% test set
sum_train = sum((x_train*w_train - y_train).^2);
MSE_train = sum_train/train_l;

sum_test = sum((x_test*w_train - y_test).^2);
MSE_test = sum_test/test_l;

我知道如果我在一些迭代中创建lambda的矢量(我已经这样做了),我可以将平均值MSE_trainMSE_test绘制为{{1然后,我将能够验证lambdaMSE_test之间的巨大差异是否表示高差异,从而过度拟合。

但是,我想做的更多是计算方差和MSE_train。 摘自Ridge Regression Notes第7页,它指导我们如何计算偏差和方差。

我的问题是,我应该在整个随机数据集(600)或训练集上遵循其步骤吗?我认为应该在训练集上计算bias^2和方差。此外,在定理2(第7页)中,偏差是由bias^2lambdaW的负积计算的,beta是我原来的{{1} (beta)我是对的吗?

对于长篇文章感到抱歉,但我真的很想了解这个概念在实践中是如何运作的。

更新1:

好的,所以按照之前的论文没有产生任何好的结果。所以,我采用了脊回归偏差 - 方差的标准形式,即:

enter image description here

基于此,我创建了(我使用了测试集):

w

但是,经过200次迭代和10次不同的lambdas,这就是我得到的,这不是我的预期。

enter image description here

事实上,我希望有这样的事情:

enter image description here

1 个答案:

答案 0 :(得分:0)

sum_bias=sum((y_test - mean(x_test*w_train)).^2); Bias = sum_bias/test_l

您为什么要对 y_testy_predicted = x_test*w_train 之间的差异进行平方? 我不相信你的偏见公式是正确的。在您的问题中,上面蓝色的“偏差项”是 bias^2 但是您的公式肯定既不是偏差也不是 bias^2,因为您只对残差求平方,而不是对整个偏差求平方?

相关问题