|
163 | 163 | @test sol.retcode == ReturnCode.Success |
164 | 164 | end |
165 | 165 |
|
166 | | - @testset "ScipyLeastSquares" begin |
167 | | - xdata = collect(0:0.1:1) |
168 | | - ydata = 2.0 * xdata .+ 1.0 .+ 0.1 * randn(length(xdata)) |
169 | | - function residuals(params, p=nothing) |
170 | | - a, b = params |
171 | | - return ydata .- (a .* xdata .+ b) |
172 | | - end |
173 | | - x0_ls = [1.0, 0.0] |
174 | | - prob = NonlinearLeastSquaresProblem(residuals, x0_ls) |
175 | | - sol = solve(prob, ScipyLeastSquaresTRF()) |
176 | | - @test sol.retcode == ReturnCode.Success |
177 | | - @test sol.u[1] ≈ 2.0 atol=0.5 |
178 | | - @test sol.u[2] ≈ 1.0 atol=0.5 |
179 | | - sol = solve(prob, ScipyLeastSquaresDogbox()) |
180 | | - @test sol.retcode == ReturnCode.Success |
181 | | - @test sol.u[1] ≈ 2.0 atol=0.5 |
182 | | - sol = solve(prob, ScipyLeastSquaresLM()) |
183 | | - @test sol.retcode == ReturnCode.Success |
184 | | - @test sol.u[1] ≈ 2.0 atol=0.5 |
185 | | - prob_bounded = NonlinearLeastSquaresProblem(residuals, x0_ls; lb = [0.0, -2.0], ub = [5.0, 3.0]) |
186 | | - sol = solve(prob_bounded, ScipyLeastSquaresTRF()) |
187 | | - @test sol.retcode == ReturnCode.Success |
188 | | - @test 0.0 <= sol.u[1] <= 5.0 |
189 | | - @test -2.0 <= sol.u[2] <= 3.0 |
190 | | - for loss in ["linear", "soft_l1", "huber", "cauchy", "arctan"] |
191 | | - sol = solve(prob, ScipyLeastSquares(method="trf", loss=loss)) |
192 | | - @test sol.retcode == ReturnCode.Success |
193 | | - end |
194 | | - ydata_outliers = copy(ydata) |
195 | | - ydata_outliers[5] = 10.0 |
196 | | - function residuals_outliers(params, p=nothing) |
197 | | - a, b = params |
198 | | - return ydata_outliers .- (a .* xdata .+ b) |
199 | | - end |
200 | | - prob_outliers = NonlinearLeastSquaresProblem(residuals_outliers, x0_ls) |
201 | | - sol_robust = solve(prob_outliers, ScipyLeastSquares(method="trf", loss="huber")) |
202 | | - @test sol_robust.retcode == ReturnCode.Success |
203 | | - end |
204 | | - |
205 | 166 | @testset "ScipyRootScalar" begin |
206 | 167 | f_root(x, p) = x[1]^3 - 2*x[1] - 5 |
207 | 168 | x0_root = [2.0] |
|
0 commit comments