【问题标题】:Calculate bias and variance in ridge regression MATLAB计算岭回归MATLAB中的偏差和方差
【发布时间】:2017-05-27 00:37:56
【问题描述】:

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

我已经创建了代码来生成一组随机的正常数字。

% 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 的平均值绘制为lambda 的函数,那么我将在哪里能够验证MSE_testMSE_train 之间的巨大差异表示高方差,因此是过拟合。

但是,我想额外做的是计算方差和bias^2。 取自第 7 页的Ridge Regression Notes,它指导我们如何计算偏差和方差。

我的问题是,我应该在整个随机数据集 (600) 上还是在训练集上遵循它的步骤?我认为bias^2 和方差应该在训练集上计算。此外,在定理 2(再次第 7 页)中,偏差由 lambdaWbeta 的负积计算,beta 是我原来的 ww = randn(10,1))我是对的?

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

更新 1:

好的,所以遵循上一篇论文并没有产生任何好的结果。因此,我采用了 Ridge Regression Bias-Variance 的标准形式,即:

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

% Bias and Variance

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

sum_var=sum((mean(x_test*w_train)- x_test*w_train).^2);
Variance = sum_var/test_l;

但是,经过 200 次迭代和 10 个不同的 lambda,这是我得到的,这不是我所期望的。

事实上,我希望得到这样的结果:

【问题讨论】:

    标签: matlab regression variance


    【解决方案1】:

    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,因为您只对残差进行平方,而不是对整个偏差?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2017-09-26
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多