[R] minimization function

Patrick Burns pburns at pburns.seanet.com
Sun Apr 11 18:01:38 CEST 2010


You have merely found a little used
entry point into Circle 1 of 'The R Inferno'.

The third element of your answer is zero
to the precision of the QP algorithm. So it
is obeying the non-negative constraints that
you put on the problem to the best of its
ability.  You should not expect numerical
exactness.

You might consider using the 'zapsmall'
function on your 'w' vector.

On 11/04/2010 16:31, li li wrote:
> Hi,
>   thanks!
>
> I added meq=1 and it did not seem to work. The result is the same as before.
>
>> x<- 2*c(1,0.5,0.8,0.5,1,0.9, 0.8,0.9,1)
>> Dmat<- matrix(x, byrow=T, nrow=3, ncol=3)
>> dvec<- numeric(3)
>> Amat<- matrix(0,3,4)
>> Amat[,1 ]<- c(1,1,1)
>> Amat[,2:4 ]<- t(diag(3))
>> bvec<- c(3,0,0,0)
>>
>> solve.QP(Dmat,dvec,Amat,bvec=bvec, meq=1)
> $solution
> [1]  1.500000e+00  1.500000e+00 -8.881784e-16
> $value
> [1] 6.75
> $unconstrained.solution
> [1] 0 0 0
> $iterations
> [1] 3 0
> $Lagrangian
> [1] 4.5 0.0 0.0 0.6
> $iact
> [1] 1 4
>>
>
>
> 2010/4/11 Gabor Grothendieck<ggrothendieck at gmail.com>
>
>> Add meq=1 to the arguments.
>>
>> On Sun, Apr 11, 2010 at 9:50 AM, li li<hannah.hlx at gmail.com>  wrote:
>>> Hi, thank you very much for the reply!
>>>
>>> Consider minimize quadratic form w'Aw with A be the following matrix.
>>>> Dmat/2
>>>       [,1] [,2] [,3]
>>> [1,]  1.0  0.5  0.8
>>> [2,]  0.5  1.0  0.9
>>> [3,]  0.8  0.9  1.0
>>> I need to find w=(w1,w2,w3), a 3 by 1 vector, such that sum(w)=3, and
>> wi>=0
>>> for all i.
>>>
>>> Below is the code I wrote, using the function solve.QP , however, the
>>> solution for w still have a
>>> negtive component. Can some one give me some suggestions?
>>>
>>> Thank you very much!
>>>
>>>> x<- 2*c(1,0.5,0.8,0.5,1,0.9, 0.8,0.9,1)
>>>> Dmat<- matrix(x, byrow=T, nrow=3, ncol=3)
>>>> dvec<- numeric(3)
>>>> Amat<- matrix(0,3,4)
>>>> Amat[,1 ]<- c(1,1,1)
>>>> Amat[,2:4 ]<- t(diag(3))
>>>> bvec<- c(3,0,0,0)
>>>>
>>>> solve.QP(Dmat,dvec,Amat,bvec=bvec)
>>> $solution
>>> [1]  1.500000e+00  1.500000e+00 -8.881784e-16
>>> $value
>>> [1] 6.75
>>> $unconstrained.solution
>>> [1] 0 0 0
>>> $iterations
>>> [1] 3 0
>>> $Lagrangian
>>> [1] 4.5 0.0 0.0 0.6
>>> $iact
>>> [1] 1 4
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> 2010/4/10 Gabor Grothendieck<ggrothendieck at gmail.com>
>>>>
>>>> Check out the quadprog package.
>>>>
>>>> On Sat, Apr 10, 2010 at 5:36 PM, li li<hannah.hlx at gmail.com>  wrote:
>>>>> Hi, thanks for the reply.
>>>>>    A will be a given matrix satisfying condition 1. I want to find the
>>>>> vector w that minimizes the
>>>>> quadratic form. w satisfies condition 2.
>>>>>
>>>>>
>>>>> 2010/4/10 Paul Smith<phhs80 at gmail.com>
>>>>>
>>>>>> On Sat, Apr 10, 2010 at 5:13 PM, Paul Smith<phhs80 at gmail.com>
>> wrote:
>>>>>>>>     I am trying to minimize the quardratic form w'Aw, with certain
>>>>>>>> constraints.
>>>>>>>> In particular,
>>>>>>>>     (1) A=(a_{ij}) is n by n matrix and it is symmetric positive
>>>>>> definite,
>>>>>>>>         a_{ii}=1 for all i;
>>>>>>>>         and 0<a_{ij}<1 for i not equal j.
>>>>>>>>     (2) w'1=n;
>>>>>>>>     (3) w_{i}>=0
>>>>>>>>
>>>>>>>> Analytically, for n=2, it is easy to come up with a result. For
>>>>>>>> larger
>>>>>> n, it
>>>>>>>> seems
>>>>>>>> difficult to obtain the result.
>>>>>>>>
>>>>>>>> Does any one know whether it is possible to use R to numerically
>>>>>>>> compute
>>>>>> it?
>>>>>>>
>>>>>>> And your decision variables are? Both w[i] and a[i,j] ?
>>>>>>
>>>>>> In addition, what do you mean by "larger n"? n = 20 is already large
>>>>>> (in your sense)?
>>>>>>
>>>>>> Paul
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide
>>>>>>
>>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> <http://www.r-project.org/posting-guide.html>
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>
>>>>>
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>
>>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')



More information about the R-help mailing list