[英]Linear optimization of difference subject to constraints
I have a vector of values (x1,x2,x3,x4,x5,x6,x7) and i want to create a vector which minimizes a new unknown vector (y1,y2,y3,y4,y5,y6,y7) such that I can minimize ||xy||^2. 我有一个值(x1,x2,x3,x4,x5,x6,x7)的向量,并且我想创建一个向量来最小化一个新的未知向量(y1,y2,y3,y4,y5,y6,y7)这样我可以最小化|| xy || ^ 2。 I also want to create this new vector subject to the constraints that x1+x2+x3+x4+x5=x6 and x1+x2+x3+x4=x7. 我还想根据x1 + x2 + x3 + x4 + x5 = x6和x1 + x2 + x3 + x4 = x7的约束来创建这个新向量。 I tried to use constrOptim but I do not think I have the right inputs. 我尝试使用constrOptim,但我认为输入的内容不正确。 Any help would be greatly appreciated! 任何帮助将不胜感激!
Would it be best to come up with a set of values and then use a nls model to predict them? 最好提出一组值,然后使用nls模型进行预测吗? How would I do that? 我该怎么做?
Thank you!! 谢谢!!
We assume that what the question actually intended was that y
is known and we want to get x with the indicated constraints. 我们假设问题的实际意图是y
是已知的,并且我们希望获得具有指示约束的x。
Note that nls does not work for zero residual problems and since no data was provided in the question we don't know whether that is the case here or not so we first present two solutions that can handle that and then finally we show an nls
for the non-zero residual case. 请注意,nls不适用于零残留问题,并且由于该问题中未提供数据,因此我们不知道这里是否存在这种情况,因此我们首先提供可以解决该问题的两个解决方案,然后最后显示nls
非零残差情况。 We use y
shown below in (1) as our test input for (1) and (2) and it does have zero residuals. 我们使用以下(1)中所示的y
作为(1)和(2)的测试输入,它的残差为零。 For (3), the nls
solution, we use a different y which does not lead to zero residuals. 对于(3)的nls
解,我们使用了一个不导致零残差的y。
Here are some alternative solutions: 以下是一些替代解决方案:
1) lm We define x5_to_x7
which maps the first 5 components of x
to the entire 7-element vector. 1)lm我们定义x5_to_x7
,它将x
的前5个分量映射到整个7元素向量。 Because x5_to_x7
is a linear operator it corresponds to a matrix X
which we form and then use in lm
: 因为x5_to_x7
是线性运算符,所以它对应于我们形成然后在lm
使用的矩阵X
:
# test data
y <- c(1:5, sum(1:5), sum(1:4))
x5_to_x7 <- function(x5) c(x5, sum(x5), sum(x5[1:4]))
X <- apply(diag(5), 1, x5_to_x7)
fm <- lm(y ~ X + 0)
giving: 赠送:
coef(fm)
## X1 X2 X3 X4 X5
## 1 2 3 4 5
all.equal(x5_to_x7(coef(fm)), y)
## [1] TRUE
2) optim Alternatively we can use optim
by defining a residual sum of squares function and solve it using optim
where y
and x5_to_x7
are as above: 2)可替换地的Optim我们可以使用optim
通过定义平方函数的残差平方和,并使用解决它optim
其中y
和x5_to_x7
如上:
rss <- function(x) sum((y - x5_to_x7(x))^2)
result <- optim(numeric(5), rss, method = "BFGS")
giving: 赠送:
> result
$par
[1] 1 2 3 4 5
$value
[1] 5.685557e-20
$counts
function gradient
18 12
$convergence
[1] 0
$message
NULL
> all.equal(x5_to_x7(result$par), y)
[1] TRUE
3) nls If y were such that the residuals are not zero then it would be possible to use nls
as suggested in the question. 3)nls如果y使残差不为零,则可以按照问题中的建议使用nls
。
y <- 1:7
fm1 <- lm(y ~ X + 0)
fm2 <- nls(y ~ x5_to_x7(x), start = list(x = numeric(5)))
all.equal(coef(fm1), coef(fm2), check.attributes = FALSE)
## [1] TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.