我的Python代码是否符合我对以下求和的期望?

时间:2017-05-01 22:59:48

标签: python-2.7 numpy

这是我的python代码,我正在执行一个将在这里链接的求和,我使用python来执行此求和。它们是等价的吗?MSE formula

注意,x_hat(填充数组)是等式中的x_hat [n],x(填充数组)是x [n]。下面的循环仅用于填充具有多个值的arrayMSE []数组以针对SNR绘制。我试图在这个循环中使用sum()执行求和而不使用另一个for循环。

for i,SNR_loop in enumerate(SNR):
      # all other stuff here is irrelevant. Is the code below equivalent 
      # to the picture?
      MSE = (1/N)*sum((x_hat-x)**2)       # summation temp variable
      arrayMSE.append(MSE)                # appending to list for plotting
      MSE_inv_array.append(1/MSE)         # 1/temp for plotting w.r.t SNR

如果这些不等于为什么?需要进行哪些更改才能使这个等效(最好没有另一个用于速度循环)?

1 个答案:

答案 0 :(得分:0)

让我们分解一下:

xh = [xh_1, 
      xh_2, 
      ...
      xh_n]

x = [x_1, 
     x_2, 
     ...
     x_n]

(xh - x) = [(xh_1 - x_1),
            (xh_2 - x_2),
                 ...
            (xh_n - x_n)]

(xh - x)**2 = [(xh_1 - x_1)**2,
               (xh_2 - x_2)**2,
                    ...
               (xh_n - x_n)**2]

sum((xh - x)**2) = (xh_1 - x_1)**2 + (xh_2 - x_2)**2 + ... + (xh_n - x_n)**2

希望您可以看到这等同于您的等式。如果我需要解释任何步骤,请告诉我。

如果我可以在Math.SE

上使用嵌入式Latex,这看起来会更好