最小化带有代数约束和边界的最小二乘 [英] Minimizing Least Squares with Algebraic Constraints and Bounds

查看:49
本文介绍了最小化带有代数约束和边界的最小二乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据一些矢量求和来最小化最小二乘之和.简而言之,我正在创建一个方程,该方程采用理想矢量,用确定的系数对它们进行加权,然后对加权后的矢量求和.一旦将该和与观察到的实际矢量测量值进行比较,就会出现最小二乘之和.

I'm attempting to minimize a sum of least squares based on some vector summations. Briefly, I'm creating an equation that takes ideal vectors, weights them with a determined coefficient, and then sums the weighted vectors. The sum of least squares comes in once this sum is compared to the actual vector measurements found for some observation.

举个例子:

# Observation A has the following measurements:
A = [0, 4.1, 5.6, 8.9, 4.3]

# How similar is A to ideal groups identified by the following:
group1 = [1, 3, 5, 10, 3]
group2 = [6, 3, 2, 1, 10]
group3 = [3, 3, 4, 2, 1]

# Let y be the predicted measurement for A with coefficients s1, s2, and s3:
y = s1 * group1 + s2 * group2 + s3 * group3

# y will be some vector of length 5, similar to A
# Now find the sum of least squares between y and A
sum((y_i - A_i)** 2 for y_i in y for A_i in A)

必要的界限和约束

Necessary bounds and constraints

0< = s1,s2,s3< = 1

0 <= s1, s2, s3 <= 1

s1 + s2 + s3 = 1

s1 + s2 + s3 = 1

y = s1 *组1 + s2 *组2 + s3 *组3

y = s1 * group1 + s2 * group2 + s3 * group3

我想最小化y和A的最小平方和,以获得系数s1,s2,s3,但是我很难确定scipy.optimize的正确选择.似乎那里的最小二乘和最小化函数不能处理代数变量约束.我正在使用的数据是这些矢量化测量的成千上万个观测值.任何想法或想法将不胜感激!

This sum of least squares for y and A is what I'd like to minimize to get the coefficients s1, s2, s3, but I'm having difficulties identifying what the proper choice in scipy.optimize might be. It doesn't seem like the functions there for minimizing sum of least squares can handle algebraic variable constraints. The data I'm working with is thousands of observations with these vectorized measurements. Any thoughts or ideas would be greatly appreciated!

推荐答案

对于您的情况,您可以使用

For your case you can use minimize() from scipy.optimize like this:

minimize(fun=obj_fun, args=argtpl x0=xinit, bounds=bnds, constraints=cons)

其中, obj_fun(x,* args)是您的目标函数, argtpl 是目标函数 xinit 初始点, bnds 是变量边界的元组列表,而 cons 是约束的字典列表.

where obj_fun(x, *args) is your objective function, argtpl a tuple of (optional) arguments for you objective function, xinit a initial point, bnds a list of tuples for the bounds of your variables and cons a list of dicts for your constraints.

import numpy as np
from scipy.optimize import minimize

# Observation A has the following measurements:
A = np.array([0, 4.1, 5.6, 8.9, 4.3])
# How similar is A to ideal groups identified by the following:
group1 = np.array([1, 3, 5, 10, 3])
group2 = np.array([6, 3, 2, 1, 10])
group3 = np.array([3, 3, 4, 2, 1])

# Define the objective function
# x is the array containing your wanted coefficients
def obj_fun(x, A, g1, g2, g3):
    y = x[0] * g1 + x[1] * g2 + x[2] * g3
    return np.sum((y-A)**2)

# Bounds for the coefficients
bnds = [(0, 1), (0, 1), (0, 1)]
# Constraint: x[0] + x[1] + x[2] - 1 = 0
cons = [{"type": "eq", "fun": lambda x: x[0] + x[1] + x[2] - 1}]

# Initial guess
xinit = np.array([1, 1, 1])
res = minimize(fun=obj_fun, args=(A, group1, group2, group3), x0=xinit, bounds=bnds, constraints=cons)
print(res.x)

您的示例解决方案:

array([9.25609756e-01, 7.43902439e-02, 6.24242179e-12])

这篇关于最小化带有代数约束和边界的最小二乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆