如何表征最小二乘估计的适应度

时间:2011-12-14 22:02:17

标签: python optimization numpy statistics scipy

我正在开展本地化项目,并使用最小二乘估计来确定发射机的位置。我需要一种方法来统计我的程序中我的解决方案的“适应性”,这可以用来告诉我是否有一个好的答案,或者我需要额外的测量,或者有不好的数据。我已经阅读了一些关于使用“确定系数”或R平方的内容,但未能找到任何好的例子。关于如何表征我是否有一个好的解决方案,或需要额外的测量的任何想法将非常感激。

谢谢!

我的代码给了我以下输出,

grid_lat和grid_lon对应于可能目标位置网格的纬度和经度坐标

grid_lat = [[ 38.16755799  38.16755799  38.16755799  38.16755799  38.16755799
  38.16755799]
  [ 38.17717199  38.17717199  38.17717199  38.17717199  38.17717199
    38.17717199]
  [ 38.186786    38.186786    38.186786    38.186786    38.186786    38.186786  ]
  [ 38.1964      38.1964      38.1964      38.1964      38.1964      38.1964    ]
  [ 38.20601401  38.20601401  38.20601401  38.20601401  38.20601401
    38.20601401]
  [ 38.21562801  38.21562801  38.21562801  38.21562801  38.21562801
    38.21562801]
  [ 38.22524202  38.22524202  38.22524202  38.22524202  38.22524202
    38.22524202]]

grid_lon = [[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]
  [-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
    -75.79807588]]

grid_error对应于每个点的解决方案的“好”。如果我们有0.0的误差,我们有一个完美的解决方案。针对每个测量位置(下面的测量中的轨迹)计算网格上的每个点的网格误差。每个测量位置具有到发射器的估计范围。 “误差”对应于来自测量的发射器的估计范围,减去在测量范围位置和网格点之间计算的实际范围。误差越小,我们接近实际发射机位置的可能性就越大

# Calculate distance between every grid point and every measurement in meters 
measured_distance = spatial.distance.cdist(grid_ecef_array, measurement_ecef_array,    'euclidean')

measurement_error = [pow((measurement - estimated_distance),2) for measurement in measured_distance]

mean_squared_error = [numpy.sqrt(numpy.mean(measurement)) for measurement in measurement_error]

# Find minimum solution 
# Convert array of mean_squared_errors to 2D grid for graphing
N3, N4 = numpy.array(grid_lon).shape
grid_error = numpy.array(mean_squared_error).reshape((N3, N4))

grid_error = [[ 2.33608445  2.02805063  1.85638288  1.84620283  2.02757163  2.38035108]
  [ 1.73675429  1.40649524  1.21799211  1.06503271  1.27373554  1.74265406]
  [ 1.44967789  0.96835022  0.62667257  0.52804942  0.91189678  1.50067864]
  [ 1.70155286  1.24024402  0.9642869   1.00517531  1.32606411  1.81754752]
  [ 2.40218247  2.07449106  1.91044903  1.94272889  2.15511638  2.51683715]
  [ 3.29679348  3.05353929  2.93662134  2.95839307  3.11583615  3.39320682]
  [ 4.27303679  4.08195869  3.99203754  4.00926823  4.13247105  4.35378011]]

# Generate the 3D plot with the Z coordinate being the mean squared error estimate
plot3Dcoordinates(grid_lon, grid_lat, grid_error)

# Generic function using matplotlib to plot coordinates
def plot3Dcoordinates(X, Y, Z):
    fig = plt.figure()
    ax = Axes3D(fig)

    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
          linewidth=0, antialiased=False)

    fig.colorbar(surf, shrink=0.5, aspect=5)

以下是在更大的网格上处理算法的示例图像。我可以直观地看出我有一个非常好的解决方案,因为形状平滑地融合在一个最小点(解决方案)上,看起来有点像倒置的巫婆帽。 enter image description here

第二张图显示了所有测量和位置,溶液绘制在顶部,最小点显示为解决方案(红色x)。

enter image description here

1 个答案:

答案 0 :(得分:3)

R平方越接近1.0,你的拟合越好。选择你自己的“足够好”的阈值,我认为典型的阈值在.92-.98范围内。