简体   繁体   English

我可以相信 R 中 `nloptr` 的结果吗?

[英]Can I trust the results from `nloptr` in R?

Trying to solve a nonlinear program with inequality constraints using sequential quadratic programming.尝试使用序列二次规划求解具有不等式约束的非线性程序。 I've solved it with Python but I get inconsistent results in R.我已经用 Python 解决了它,但在 R 中得到了不一致的结果。

The objective function takes a vector y and a matrix X and looks for weights W that minimize the L2 norm.目标函数采用向量y和矩阵X并寻找使 L2 范数最小的权重W There are two constraints:有两个约束:

  1. each weight in the vector W is between 0 and 1向量W中的每个权重都在 0 和 1 之间
  2. W sums to 1 W总和为 1

In Python I use scipy.optimize.fmin_slsqp , which " implements the SLSQP Optimization subroutine originally implemented by Dieter Kraft ":在 Python 中,我使用scipy.optimize.fmin_slsqp ,它“ 实现了最初由 Dieter Kraft 实现的 SLSQP 优化子程序”:

import numpy as np
from scipy.optimize import fmin_slsqp

np.random.seed(42)

def loss(W):
    return np.sqrt(np.mean((y - X @ W)**2))

N = X.shape[1]
w_start = [1/N]*N
w = fmin_slsqp(loss,
               np.array(w_start),
               f_eqcons=lambda x: np.sum(x) - 1,
               bounds=[(0.0, 1.0)]*len(w_start),
               disp=True)
w.round(3)

which gives me这给了我

Optimization terminated successfully    (Exit mode 0)
            Current function value: 2.3149922441277146
            Iterations: 13
            Function evaluations: 514
            Gradient evaluations: 13
array([0.   , 0.   , 0.   , 0.085, 0.   , 0.   , 0.   , 0.   , 0.   ,
       0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   ,
       0.   , 0.113, 0.105, 0.457, 0.   , 0.   , 0.   , 0.   , 0.   ,
       0.   , 0.   , 0.   , 0.   , 0.   , 0.24 , 0.   , 0.   , 0.   ,
       0.   , 0.   ])

and I obtain the same results regardless of the starting values w_start .无论起始值w_start什么,我都会获得相同的结果。

In RI try to solve the problem with nloptr::slsqp , which to my understanding implements the same algorithm :在 RI 中尝试使用nloptr::slsqp解决问题, nloptr::slsqp我所知,它实现了相同的算法

set.seed(42)
loss = function(W){
  return(sqrt(mean(y - X %*% W)^2))
}
N = nrow(X)
w_start = rep(0, N)
m = nloptr::slsqp(x0 = w_start,
              fn = loss,
              lower = rep(0, N),
              upper = rep(1, N),
              heq = function(x) sum(x) - 1)

However, I get different results:但是,我得到了不同的结果:

> m$value
[1] 0.000407041
> round(m$par,3)
 [1] 0.027 0.000 0.000 0.000 0.000 0.000 0.062 0.000 0.000 0.007 0.000 0.000 0.000 0.000 0.002 0.010 0.000
[18] 0.035 0.053 0.000 0.171 0.152 0.000 0.049 0.000 0.000 0.000 0.000 0.000 0.080 0.000 0.000 0.337 0.000
[35] 0.000 0.000 0.017 0.000

and the results change with different starting values.并且结果随着不同的起始值而变化。

Am I implementing this comparison between the Python and R solvers correctly?我是否正确地在 Python 和 R 求解器之间进行了这种比较? If so, is this an issue with nloptr ?如果是这样,这是nloptr的问题吗?

Data to recreate the problem:重现问题的数据:

> dput(y)
c(123, 121, 123.5, 124.400001525879, 126.699996948242, 127.099998474121, 
128, 126.400001525879, 126.099998474121, 121.900001525879, 120.199996948242, 
118.599998474121, 115.400001525879, 110.800003051758, 104.800003051758, 
102.800003051758, 99.6999969482422, 97.5, 90.0999984741211, 38.7999992370605, 
39.7000007629395, 39.9000015258789, 39.9000015258789, 41.9000015258789, 
45, 48.2999992370605, 49, 58.7000007629395, 60.0999984741211, 
62.0999984741211, 66.4000015258789, 72.8000030517578, 84.9000015258789, 
94.9000015258789, 98, 104.400001525879, 103.900001525879, 117.400001525879
)
> dput(X)
structure(c(89.8000030517578, 95.4000015258789, 101.099998474121, 
102.900001525879, 108.199996948242, 111.699996948242, 116.199996948242, 
117.099998474121, 123, 121.400001525879, 123.199996948242, 119.599998474121, 
119.099998474121, 116.300003051758, 113, 114.5, 116.300003051758, 
114, 112.099998474121, 39.5999984741211, 42.7000007629395, 42.2999992370605, 
42.0999984741211, 43.0999984741211, 46.5999984741211, 50.4000015258789, 
50.0999984741211, 55.0999984741211, 56.7999992370605, 60.5999984741211, 
68.8000030517578, 73.0999984741211, 84.4000015258789, 90.8000030517578, 
99, 103, 110, 114.400001525879, 100.300003051758, 104.099998474121, 
103.900001525879, 108, 109.699996948242, 114.800003051758, 119.099998474121, 
122.599998474121, 127.300003051758, 126.5, 131.800003051758, 
128.699996948242, 127.400001525879, 128, 123.099998474121, 125.800003051758, 
126, 122.300003051758, 121.5, 36.7000007629395, 38.7999992370605, 
44.0999984741211, 45.0999984741211, 45.5, 48.5999984741211, 50.9000015258789, 
52.5999984741211, 56.5, 58.4000015258789, 61.5, 64.6999969482422, 
72.0999984741211, 82, 93.5999984741211, 98.5, 103.599998474121, 
113, 119.900001525879, 124.800003051758, 125.5, 134.300003051758, 
137.899993896484, 132.800003051758, 131, 134.199996948242, 132, 
129.199996948242, 131.5, 131, 133.800003051758, 130.5, 125.300003051758, 
119.699996948242, 112.400001525879, 109.900001525879, 102.400001525879, 
94.5999984741211, 29.3999996185303, 31.1000003814697, 31.2000007629395, 
32.7000007629395, 38.0999984741211, 41.7000007629395, 44.7999992370605, 
44.7000007629395, 57.4000015258789, 52.7999992370605, 54.5999984741211, 
58.0999984741211, 61.4000015258789, 73.3000030517578, 83.4000015258789, 
93.0999984741211, 95.0999984741211, 108.599998474121, 116, 120, 
117.599998474121, 110.800003051758, 109.300003051758, 112.400001525879, 
110.199996948242, 113.400001525879, 117.300003051758, 117.5, 
117.400001525879, 118, 116.400001525879, 114.699996948242, 114.099998474121, 
112.5, 111, 108.5, 109, 104.800003051758, 42.2000007629395, 45.5, 
51.2999992370605, 50.5999984741211, 52.5, 54.5, 57.5999984741211, 
58.4000015258789, 61.7000007629395, 64.4000015258789, 67, 80.0999984741211, 
85.5999984741211, 95.5999984741211, 113.5, 118.599998474121, 
118.5, 122.699996948242, 129.699996948242, 155, 161.100006103516, 
156.300003051758, 154.699996948242, 151.300003051758, 147.600006103516, 
153, 153.300003051758, 155.5, 150.199996948242, 150.5, 152.600006103516, 
154.100006103516, 149.600006103516, 144, 144.5, 142.399993896484, 
141, 137.100006103516, 39, 41.2999992370605, 44.7000007629395, 
44, 44.2000007629395, 45.9000015258789, 50.0999984741211, 51.7000007629395, 
58.7000007629395, 60, 62.7000007629395, 66, 74.0999984741211, 
82, 91.0999984741211, 98.6999969482422, 105.199996948242, 111.400001525879, 
119.300003051758, 109.900001525879, 115.699996948242, 117, 119.800003051758, 
123.699996948242, 122.900001525879, 125.900001525879, 127.900001525879, 
130.600006103516, 131, 134, 131.699996948242, 131.199996948242, 
128.600006103516, 126.300003051758, 128.800003051758, 129, 129.300003051758, 
124.099998474121, 34.2999992370605, 35.7999992370605, 40.9000015258789, 
42.4000015258789, 42.4000015258789, 44.5, 47.9000015258789, 49.5, 
54.7000007629395, 56.5999984741211, 59.2999992370605, 62.5999984741211, 
67.8000030517578, 78.9000015258789, 86.8000030517578, 90.6999969482422, 
100.099998474121, 103.900001525879, 109.199996948242, 102.400001525879, 
108.5, 126.099998474121, 121.800003051758, 125.599998474121, 
123.300003051758, 125.099998474121, 125, 122.800003051758, 117.5, 
115.199996948242, 114.099998474121, 111.5, 111.300003051758, 
103.599998474121, 100.699996948242, 96.6999969482422, 95, 84.5, 
33.7999992370605, 33.5999984741211, 33.7000007629395, 36.2999992370605, 
38, 40.2999992370605, 42.5, 45.5999984741211, 51.5, 55.4000015258789, 
56.4000015258789, 59.2000007629395, 67.5999984741211, 76.5, 88.5, 
97.5999984741211, 99.9000015258789, 107.099998474121, 121.900001525879, 
124.800003051758, 125.599998474121, 126.599998474121, 124.400001525879, 
131.899993896484, 131.800003051758, 134.399993896484, 134, 136.699996948242, 
135.300003051758, 135.199996948242, 133, 130.699996948242, 127.900001525879, 
124, 121.599998474121, 118.199996948242, 109.5, 107.599998474121, 
41.4000015258789, 41.4000015258789, 41.9000015258789, 41, 41.9000015258789, 
45.2000007629395, 48.4000015258789, 49.4000015258789, 54.5999984741211, 
56.7999992370605, 60, 63.0999984741211, 69.5999984741211, 80.8000030517578, 
89.5999984741211, 96.6999969482422, 108.400001525879, 116.199996948242, 
124.099998474121, 134.600006103516, 139.300003051758, 149.199996948242, 
156, 159.600006103516, 162.399993896484, 166.600006103516, 173, 
150.899993896484, 148.899993896484, 146.899993896484, 148.5, 
147.699996948242, 143, 137.800003051758, 135.300003051758, 137.600006103516, 
134, 134, 30.6000003814697, 32.2000007629395, 32.5, 32.9000015258789, 
34.5, 36.7000007629395, 38.7000007629395, 40.5999984741211, 50, 
52.5, 53.7000007629395, 58.2999992370605, 65.0999984741211, 75.6999969482422, 
85.1999969482422, 88.8000030517578, 93.5999984741211, 100.099998474121, 
109.300003051758, 108.5, 108.400001525879, 109.400001525879, 
110.599998474121, 116.099998474121, 120.5, 124.400001525879, 
125.5, 127.099998474121, 124.199996948242, 124.599998474121, 
132.899993896484, 116.199996948242, 115.599998474121, 111.199996948242, 
109.400001525879, 104.099998474121, 101.099998474121, 100.199996948242, 
37.7000007629395, 38.5, 41.9000015258789, 41.9000015258789, 43.2000007629395, 
45.4000015258789, 47.7999992370605, 49.4000015258789, 54.5999984741211, 
56.4000015258789, 58.7999992370605, 61.4000015258789, 72.8000030517578, 
84, 93.3000030517578, 99.5, 104.800003051758, 117.099998474121, 
124.199996948242, 114, 102.800003051758, 111, 115.199996948242, 
118.599998474121, 123.400001525879, 127.699996948242, 127.900001525879, 
127.099998474121, 126.400001525879, 127.099998474121, 132, 130.899993896484, 
127.599998474121, 121.699996948242, 115.699996948242, 109.400001525879, 
105.199996948242, 103.199996948242, 34.2000007629395, 38.9000015258789, 
38.7999992370605, 39.2999992370605, 40.2000007629395, 42.7000007629395, 
46.5999984741211, 48.0999984741211, 52.5999984741211, 54.7999992370605, 
58.2999992370605, 59.7999992370605, 65.0999984741211, 77, 91, 
97.5, 103, 114.5, 123.900001525879, 155.800003051758, 163.5, 
179.399993896484, 201.899993896484, 212.399993896484, 223, 230.899993896484, 
229.399993896484, 224.699996948242, 214.899993896484, 215.300003051758, 
209.699996948242, 210.600006103516, 201.100006103516, 183.199996948242, 
182.399993896484, 179.800003051758, 171.199996948242, 173.199996948242, 
28.2999992370605, 30.1000003814697, 30.6000003814697, 30.6000003814697, 
31.5, 33.2999992370605, 36, 36.9000015258789, 41.4000015258789, 
43.4000015258789, 46.2999992370605, 49.4000015258789, 56.2999992370605, 
66.4000015258789, 75.4000015258789, 79.3000030517578, 85.4000015258789, 
90.5, 94.4000015258789, 115.900001525879, 119.800003051758, 125.300003051758, 
126.699996948242, 129.899993896484, 133.600006103516, 139.600006103516, 
140, 142.699996948242, 140.100006103516, 143.800003051758, 144, 
143.899993896484, 133.699996948242, 128.899993896484, 125, 121.199996948242, 
116.5, 110.900001525879, 34.2999992370605, 39.2999992370605, 
40, 39.9000015258789, 41.5999984741211, 44.2999992370605, 48.0999984741211, 
48.9000015258789, 54.2000007629395, 57.0999984741211, 60, 62.5999984741211, 
70.3000030517578, 80.6999969482422, 90.6999969482422, 103, 105.099998474121, 
117.800003051758, 120.400001525879, 128.5, 133.199996948242, 
136.5, 138, 142.100006103516, 140.699996948242, 144.899993896484, 
145.600006103516, 143.899993896484, 138.5, 141.199996948242, 
138.899993896484, 139.5, 135.399993896484, 135.5, 127.900001525879, 
119, 125, 125, 38, 38.7999992370605, 41.5, 41, 41.7999992370605, 
46.7000007629395, 49.9000015258789, 50.9000015258789, 55, 54.5, 
59, 62.9000015258789, 69.6999969482422, 80.8000030517578, 93.6999969482422, 
98.0999984741211, 112.699996948242, 121.199996948242, 129, 104.300003051758, 
116.400001525879, 96.8000030517578, 106.800003051758, 110.599998474121, 
111.5, 116.699996948242, 117.199996948242, 118.900001525879, 
118.300003051758, 117.699996948242, 120.800003051758, 119.400001525879, 
113.199996948242, 110.800003051758, 113, 104.300003051758, 108.800003051758, 
94.0999984741211, 39.0999984741211, 40.0999984741211, 45.2000007629395, 
45.5999984741211, 47, 49.4000015258789, 52.0999984741211, 53.0999984741211, 
57.9000015258789, 60.9000015258789, 63, 65.8000030517578, 71.6999969482422, 
87.3000030517578, 99.1999969482422, 101.5, 116.300003051758, 
120.099998474121, 141.699996948242, 93.4000015258789, 105.400001525879, 
112.099998474121, 115, 117.099998474121, 116.800003051758, 120.900001525879, 
122.099998474121, 124.900001525879, 123.900001525879, 127, 125.300003051758, 
125.800003051758, 122.300003051758, 116.400001525879, 115.300003051758, 
113.199996948242, 110, 109, 36.2000007629395, 37.5, 37.4000015258789, 
37.2999992370605, 41.4000015258789, 43, 46.4000015258789, 48.7999992370605, 
53.5999984741211, 56.5, 59.7000007629395, 63, 69.1999969482422, 
78.5999984741211, 89, 96.4000015258789, 106, 115.800003051758, 
122.599998474121, 121.300003051758, 127.599998474121, 130, 132.100006103516, 
135.399993896484, 135.600006103516, 139.5, 140.800003051758, 
141.800003051758, 140.199996948242, 142.100006103516, 140.5, 
139.699996948242, 134.100006103516, 130, 129.199996948242, 128.800003051758, 
128.699996948242, 127.400001525879, 36, 36.7999992370605, 37.7000007629395, 
37.7000007629395, 38, 43.5, 44.7000007629395, 45.9000015258789, 
49.9000015258789, 52.2000007629395, 57.2999992370605, 59.9000015258789, 
64.6999969482422, 74.8000030517578, 84.8000030517578, 93.6999969482422, 
101.900001525879, 108.5, 114.599998474121, 111.199996948242, 
115.599998474121, 122.199996948242, 119.900001525879, 121.900001525879, 
123.699996948242, 124.900001525879, 127, 127.199996948242, 120.300003051758, 
122, 121.099998474121, 122.400001525879, 113.699996948242, 110.099998474121, 
103.599998474121, 97.8000030517578, 91.6999969482422, 87.0999984741211, 
34, 34.7000007629395, 40.0999984741211, 40.9000015258789, 41.7999992370605, 
43.7000007629395, 45.2999992370605, 47.5999984741211, 51.9000015258789, 
53.7000007629395, 56.7000007629395, 60.4000015258789, 65.6999969482422, 
77.1999969482422, 91.3000030517578, 95.5, 102, 106.199996948242, 
115.300003051758, 108.099998474121, 108.599998474121, 104.900001525879, 
106.599998474121, 110.5, 114.099998474121, 118.099998474121, 
117.699996948242, 117.400001525879, 116.099998474121, 116.300003051758, 
117, 117.099998474121, 110.800003051758, 107.699996948242, 105.099998474121, 
103.099998474121, 101.300003051758, 92.9000015258789, 33.9000015258789, 
34.7000007629395, 41.0999984741211, 41.2000007629395, 42, 44.5999984741211, 
46.7999992370605, 48.0999984741211, 53.5999984741211, 55.4000015258789, 
59.5, 60.9000015258789, 69.6999969482422, 83.6999969482422, 94.8000030517578, 
95.8000030517578, 104, 113.699996948242, 123.300003051758, 189.5, 
190.5, 198.600006103516, 201.5, 204.699996948242, 205.199996948242, 
201.399993896484, 190.800003051758, 187, 183.300003051758, 177.699996948242, 
171.899993896484, 165.100006103516, 159.199996948242, 136.600006103516, 
146.699996948242, 142.600006103516, 147.699996948242, 141.899993896484, 
38.9000015258789, 44, 40.5999984741211, 40.2999992370605, 41.9000015258789, 
44.5, 44.9000015258789, 49.2999992370605, 54.2999992370605, 57.0999984741211, 
63.0999984741211, 63.2999992370605, 71.5999984741211, 81.9000015258789, 
99.8000030517578, 109.300003051758, 106.599998474121, 114, 129.600006103516, 
265.700012207031, 278, 296.200012207031, 279, 269.799987792969, 
269.100006103516, 290.5, 278.799987792969, 269.600006103516, 
254.600006103516, 247.800003051758, 245.399993896484, 239.800003051758, 
232.899993896484, 215.100006103516, 201.100006103516, 195.899993896484, 
195.100006103516, 180.399993896484, 31.3999996185303, 34.0999984741211, 
36.0999984741211, 36.9000015258789, 37.9000015258789, 40.7999992370605, 
43.9000015258789, 45, 49.7000007629395, 53.2000007629395, 55.2999992370605, 
58.4000015258789, 67, 74.6999969482422, 90.5, 89.1999969482422, 
100, 102, 113.5, 90, 92.5999984741211, 99.3000030517578, 98.9000015258789, 
100.300003051758, 103.099998474121, 102.400001525879, 102.400001525879, 
103.099998474121, 101, 102.699996948242, 103, 97.5, 96.3000030517578, 
88.9000015258789, 88, 88.1999969482422, 82.3000030517578, 77.6999969482422, 
39.7000007629395, 41.7000007629395, 41.0999984741211, 41.7999992370605, 
43.7000007629395, 46.2999992370605, 49.5, 51.5999984741211, 56, 
57.5999984741211, 62.5999984741211, 63, 69.4000015258789, 79.5999984741211, 
90.1999969482422, 97.5, 101.199996948242, 110.199996948242, 113.699996948242, 
172.399993896484, 187.600006103516, 214.100006103516, 226.5, 
227.300003051758, 226, 230.199996948242, 217, 205.5, 197.300003051758, 
187.800003051758, 179.300003051758, 179, 169.800003051758, 160.600006103516, 
156.300003051758, 154.399993896484, 150.5, 146, 27.2999992370605, 
29.3999996185303, 28.7000007629395, 28.8999996185303, 30.1000003814697, 
32.9000015258789, 35.7999992370605, 36.5999984741211, 41.7999992370605, 
43.7000007629395, 47.2999992370605, 50, 55.5, 66, 75, 78.9000015258789, 
83.6999969482422, 90.5999984741211, 96, 93.8000030517578, 98.5, 
103.800003051758, 108.699996948242, 110.5, 117.900001525879, 
125.400001525879, 122.199996948242, 121.900001525879, 121.300003051758, 
123.699996948242, 125.699996948242, 126.800003051758, 119.599998474121, 
109.400001525879, 103.199996948242, 99.8000030517578, 92.3000030517578, 
87.0999984741211, 37.2999992370605, 38.9000015258789, 38.9000015258789, 
39.4000015258789, 39.9000015258789, 42.5999984741211, 45.9000015258789, 
47.4000015258789, 53.2000007629395, 55, 59.5999984741211, 62, 
67.8000030517578, 77.9000015258789, 94.4000015258789, 100.599998474121, 
104.199996948242, 110.300003051758, 123.300003051758, 121.599998474121, 
124.599998474121, 124.400001525879, 120.5, 122.099998474121, 
122.5, 124.599998474121, 127.300003051758, 131.300003051758, 
130.899993896484, 133.5, 132.800003051758, 134, 130, 127.099998474121, 
126.699996948242, 126.300003051758, 124.599998474121, 122.400001525879, 
36.5999984741211, 38.0999984741211, 38.4000015258789, 42, 42.9000015258789, 
46, 48.5, 49.7999992370605, 53.9000015258789, 56.2999992370605, 
58.7000007629395, 61.4000015258789, 68.3000030517578, 82.5, 89.1999969482422, 
92.1999969482422, 98.0999984741211, 102.199996948242, 108.400001525879, 
108.400001525879, 115.400001525879, 121.699996948242, 124.099998474121, 
130.5, 132.899993896484, 138.600006103516, 140.399993896484, 
143.600006103516, 141.600006103516, 141.600006103516, 143.699996948242, 
147, 140, 128.100006103516, 124.199996948242, 119.900001525879, 
113.099998474121, 103.599998474121, 38.4000015258789, 39.7999992370605, 
39.7999992370605, 40.4000015258789, 41, 43.5999984741211, 46.4000015258789, 
47.9000015258789, 53.0999984741211, 55.5, 62.9000015258789, 65.8000030517578, 
71.6999969482422, 83.9000015258789, 93.3000030517578, 95.0999984741211, 
104.599998474121, 114.400001525879, 122.599998474121, 107.300003051758, 
106.300003051758, 109, 110.699996948242, 114.199996948242, 114.599998474121, 
118.800003051758, 120.099998474121, 122.300003051758, 122.599998474121, 
124, 125.199996948242, 123.300003051758, 125.300003051758, 115.300003051758, 
115.800003051758, 113.900001525879, 110.599998474121, 107.599998474121, 
38.4000015258789, 44.7000007629395, 44.7000007629395, 44.9000015258789, 
46.5999984741211, 49.7999992370605, 52.2999992370605, 53.2999992370605, 
57.4000015258789, 60.5999984741211, 61.2999992370605, 64.8000030517578, 
69.8000030517578, 81.6999969482422, 97.6999969482422, 100.099998474121, 
104.900001525879, 110, 112.300003051758, 123.900001525879, 123.199996948242, 
134.399993896484, 142, 146.100006103516, 154.699996948242, 150.199996948242, 
148.800003051758, 146.800003051758, 145.800003051758, 149.300003051758, 
151.199996948242, 146.300003051758, 135.800003051758, 136.899993896484, 
133.399993896484, 136.300003051758, 124.400001525879, 138, 39.2999992370605, 
40.2000007629395, 41.5999984741211, 40.5999984741211, 41.2999992370605, 
44.2999992370605, 52.2000007629395, 52.2999992370605, 56.2999992370605, 
58.7000007629395, 60, 64.5, 71.5999984741211, 84, 94.8000030517578, 
100.300003051758, 101.800003051758, 113.5, 121.5, 103.599998474121, 
115, 118.699996948242, 125.5, 129.699996948242, 130.5, 136.800003051758, 
137.199996948242, 140.399993896484, 135.699996948242, 138.300003051758, 
136.100006103516, 136, 131.100006103516, 127, 125.400001525879, 
126.599998474121, 126.599998474121, 124.400001525879, 32.5, 34.2999992370605, 
34.0999984741211, 33.5, 35.2000007629395, 38.0999984741211, 41, 
42.2000007629395, 49.2000007629395, 50.2000007629395, 52.2999992370605, 
54.7000007629395, 61.9000015258789, 72.4000015258789, 81.3000030517578, 
83, 88.6999969482422, 95.3000030517578, 99.9000015258789, 92.6999969482422, 
96.6999969482422, 103, 103.5, 108.400001525879, 113.5, 116.699996948242, 
115.599998474121, 116.900001525879, 117.400001525879, 114.699996948242, 
115.699996948242, 113, 109.800003051758, 105.699996948242, 104.400001525879, 
97, 95.8000030517578, 91.9000015258789, 38.5, 38.5, 39.0999984741211, 
39.5999984741211, 40.4000015258789, 42.7999992370605, 45, 46.4000015258789, 
53.2000007629395, 54.0999984741211, 58.7999992370605, 62.2999992370605, 
68, 78.8000030517578, 89.8000030517578, 92.3000030517578, 108.5, 
113.699996948242, 124.699996948242, 99.8000030517578, 106.300003051758, 
111.5, 109.699996948242, 114.800003051758, 117.400001525879, 
121.699996948242, 124.599998474121, 127.300003051758, 127.199996948242, 
130.399993896484, 129.100006103516, 131.399993896484, 129, 125.099998474121, 
128.699996948242, 129, 130.600006103516, 125.300003051758, 39.9000015258789, 
41.5999984741211, 41.5999984741211, 40.7999992370605, 42.5, 45.2999992370605, 
48.2999992370605, 49.5999984741211, 54.7999992370605, 57.2999992370605, 
60.2999992370605, 63.7000007629395, 68.3000030517578, 79.0999984741211, 
88.3000030517578, 92.5, 98.8000030517578, 103.5, 112.099998474121, 
106.400001525879, 108.900001525879, 108.599998474121, 110.400001525879, 
114.699996948242, 116, 121.400001525879, 124.199996948242, 126.599998474121, 
126.400001525879, 129.699996948242, 129, 131.199996948242, 126.400001525879, 
117.199996948242, 115.900001525879, 113.699996948242, 105.800003051758, 
96.5, 40.4000015258789, 42, 46.9000015258789, 46.4000015258789, 
47.5, 50.5999984741211, 53.2999992370605, 53.2999992370605, 59.0999984741211, 
62.2000007629395, 63.7000007629395, 66.9000015258789, 73.8000030517578, 
84.0999984741211, 93.8000030517578, 102.099998474121, 105.5, 
114.400001525879, 128, 65.5, 67.6999969482422, 71.3000030517578, 
72.6999969482422, 75.5999984741211, 75.8000030517578, 77.9000015258789, 
78, 79.5999984741211, 79.0999984741211, 74.8000030517578, 77.5999984741211, 
73.5999984741211, 69, 66.3000030517578, 66.5, 64.4000015258789, 
67.6999969482422, 55, 34.5999984741211, 36.5999984741211, 37.2000007629395, 
36.5, 37.7999992370605, 40.5, 43.4000015258789, 44.7000007629395, 
49.5, 53.7000007629395, 57.2000007629395, 62.7000007629395, 68.0999984741211, 
82, 95.3000030517578, 104.599998474121, 103.5, 108.599998474121, 
122.900001525879, 122.599998474121, 124.400001525879, 138, 146.800003051758, 
151.800003051758, 155.5, 171.100006103516, 169.399993896484, 
162.399993896484, 160.899993896484, 161.600006103516, 163.800003051758, 
162.300003051758, 153.800003051758, 144.300003051758, 144.5, 
131.199996948242, 128.300003051758, 128.699996948242, 37.7000007629395, 
39.5, 40, 39.7999992370605, 41.2999992370605, 41.7999992370605, 
47.0999984741211, 47, 52.5, 54.7999992370605, 58.9000015258789, 
61, 66.8000030517578, 77, 90.5999984741211, 95.5, 104.900001525879, 
113.800003051758, 123.699996948242, 124.300003051758, 128.399993896484, 
137, 143.100006103516, 149.600006103516, 152.699996948242, 158.100006103516, 
157.699996948242, 155.899993896484, 151.800003051758, 148.899993896484, 
149.899993896484, 147.399993896484, 144.699996948242, 136.800003051758, 
134.600006103516, 135.800003051758, 133, 129.5, 28.7999992370605, 
30.2000007629395, 29.8999996185303, 30.1000003814697, 31.2999992370605, 
33.5999984741211, 37.9000015258789, 38.4000015258789, 42.7999992370605, 
45.7999992370605, 48.5, 51.7999992370605, 56.4000015258789, 68.8000030517578, 
76, 83.5999984741211, 91.3000030517578, 94.5999984741211, 102.099998474121, 
114.5, 111.5, 117.5, 116.599998474121, 119.900001525879, 123.199996948242, 
129.699996948242, 133.899993896484, 131.600006103516, 122.099998474121, 
122.300003051758, 120.5, 119.800003051758, 115.699996948242, 
111.900001525879, 109.099998474121, 112.099998474121, 107.5, 
109.099998474121, 33.7000007629395, 41.5999984741211, 41.2999992370605, 
39.9000015258789, 42, 45.2000007629395, 48.4000015258789, 48.9000015258789, 
53.9000015258789, 62.4000015258789, 64.3000030517578, 66.1999969482422, 
75.0999984741211, 88.1999969482422, 97.1999969482422, 103.199996948242, 
104.099998474121, 112.800003051758, 122.199996948242, 106.400001525879, 
105.400001525879, 108.800003051758, 109.5, 111.800003051758, 
113.5, 115.400001525879, 117.199996948242, 116.699996948242, 
117.099998474121, 117.599998474121, 119.900001525879, 115.599998474121, 
106.300003051758, 105.599998474121, 107, 105.400001525879, 106, 
102.599998474121, 38.5, 40.2000007629395, 40.2999992370605, 42.5999984741211, 
43.9000015258789, 46.5999984741211, 51.2999992370605, 52.0999984741211, 
57.0999984741211, 58.7000007629395, 61.2000007629395, 64.9000015258789, 
75, 92, 100.800003051758, 106.800003051758, 110.800003051758, 
116.300003051758, 128.600006103516, 132.199996948242, 131.699996948242, 
140, 141.199996948242, 145.800003051758, 160.699996948242, 161.5, 
160.399993896484, 160.300003051758, 168.600006103516, 158.100006103516, 
163.100006103516, 157.699996948242, 141.199996948242, 128.899993896484, 
125.699996948242, 124.800003051758, 110.400001525879, 114.300003051758, 
34.0999984741211, 34.4000015258789, 34.4000015258789, 34.4000015258789, 
35.7999992370605, 38.5999984741211, 42.5999984741211, 43.4000015258789, 
49.7999992370605, 51.7000007629395, 55.2999992370605, 55.9000015258789, 
64.3000030517578, 71, 81.6999969482422, 87.4000015258789, 97.8000030517578, 
102.699996948242, 112.900001525879), .Dim = c(38L, 38L), .Dimnames = list(
    NULL, c("1", "2", "4", "5", "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 
    "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", 
    "32", "33", "34", "35", "36", "37", "38", "39")))

This may be a rounding/precision issue.这可能是一个舍入/精度问题。 In the R implementation if you add a rounding option to the loss function:在 R 实现中,如果向损失函数添加舍入选项:

loss = function(W, digits = 20){
  return(round(sqrt(mean(y - X %*% W)^2)),digits)
}

and leave everything else the same you get the same results as the Python implementation:并保持其他所有内容相同,您将获得与 Python 实现相同的结果:

> m$value
[1] 2.314992
> round(m$par,3)
 [1] 0.000 0.000 0.000 0.085 0.000 0.000 0.000 0.000
 [9] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[17] 0.000 0.000 0.000 0.113 0.105 0.457 0.000 0.000
[25] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[33] 0.240 0.000 0.000 0.000 0.000 0.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM