Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Berend Hasselman

> On 20 Oct 2015, at 15:35, Duncan Murdoch  wrote:
> 
> On 20/10/2015 6:58 AM, Andy Yuan wrote:
>> Hello
>> 
>> Please could you help me to select the most appropriate/fastest function to 
>> use for the following constraint optimisation issue?
> 
> Just project S into the space orthogonal to B, i.e. compute the
> residuals when you regress S on B (with no intercept).  For example,
> 
> X <- lsfit(B, S, intercept=FALSE)$residuals
> 

And  you can use an alternative like this with package nloptr using functions

library(nloptr)

f1 <- function(x) sum(crossprod(x-S))

heq <- function(x) sum(B * x)

# Check lsfit solution
f1(X)
heq(X)

slsqp(c(0,0,0),fn=f1,heq=heq)


Berend

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Correction.

Yes, it's the projection of S onto the subspace orthogonal to B which is:

X <- S - (B%o%B) %*% S/ sum(B*B)

and is also implied by Duncan's solution since that is what the residuals
of linear regression are.

On Tue, Oct 20, 2015 at 1:11 PM, Gabor Grothendieck  wrote:

> Yes, it's the projection of S onto the subspace orthogonal to B which is:
>
> X <- S - B%*%B / sum(B*B)
>
> and is also implied by Duncan's solution since that is what the residuals
> of linear regression are.
>
> On Tue, Oct 20, 2015 at 1:00 PM, Paul Smith  wrote:
>
>> On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
>> >
>> > Please could you help me to select the most appropriate/fastest
>> function to use for the following constraint optimisation issue?
>> >
>> > Objective function:
>> >
>> > Min: Sum( (X[i] - S[i] )^2)
>> >
>> > Subject to constraint :
>> >
>> > Sum (B[i] x X[i]) =0
>> >
>> > where i=1…n and S[i] and B[i] are real numbers
>> >
>> > Need to solve for X
>> >
>> > Example:
>> >
>> > Assume n=3
>> >
>> > S <- c(-0.5, 7.8, 2.3)
>> > B <- c(0.42, 1.12, 0.78)
>> >
>> > Many thanks
>>
>> I believe you can solve *analytically* your optimization problem, with
>> the Lagrange multipliers method, Andy. By doing so, you can derive
>> clean and closed-form expression for the optimal solution.
>>
>> Paul
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Gabor Grothendieck
Yes, it's the projection of S onto the subspace orthogonal to B which is:

X <- S - B%*%B / sum(B*B)

and is also implied by Duncan's solution since that is what the residuals
of linear regression are.

On Tue, Oct 20, 2015 at 1:00 PM, Paul Smith  wrote:

> On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
> >
> > Please could you help me to select the most appropriate/fastest function
> to use for the following constraint optimisation issue?
> >
> > Objective function:
> >
> > Min: Sum( (X[i] - S[i] )^2)
> >
> > Subject to constraint :
> >
> > Sum (B[i] x X[i]) =0
> >
> > where i=1…n and S[i] and B[i] are real numbers
> >
> > Need to solve for X
> >
> > Example:
> >
> > Assume n=3
> >
> > S <- c(-0.5, 7.8, 2.3)
> > B <- c(0.42, 1.12, 0.78)
> >
> > Many thanks
>
> I believe you can solve *analytically* your optimization problem, with
> the Lagrange multipliers method, Andy. By doing so, you can derive
> clean and closed-form expression for the optimal solution.
>
> Paul
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Paul Smith
On Tue, Oct 20, 2015 at 11:58 AM, Andy Yuan  wrote:
>
> Please could you help me to select the most appropriate/fastest function to 
> use for the following constraint optimisation issue?
>
> Objective function:
>
> Min: Sum( (X[i] - S[i] )^2)
>
> Subject to constraint :
>
> Sum (B[i] x X[i]) =0
>
> where i=1…n and S[i] and B[i] are real numbers
>
> Need to solve for X
>
> Example:
>
> Assume n=3
>
> S <- c(-0.5, 7.8, 2.3)
> B <- c(0.42, 1.12, 0.78)
>
> Many thanks

I believe you can solve *analytically* your optimization problem, with
the Lagrange multipliers method, Andy. By doing so, you can derive
clean and closed-form expression for the optimal solution.

Paul

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] Most appropriate function for the following optimisation issue?

2015-10-20 Thread Duncan Murdoch
On 20/10/2015 6:58 AM, Andy Yuan wrote:
> Hello
>  
> Please could you help me to select the most appropriate/fastest function to 
> use for the following constraint optimisation issue?

Just project S into the space orthogonal to B, i.e. compute the
residuals when you regress S on B (with no intercept).  For example,

X <- lsfit(B, S, intercept=FALSE)$residuals

>  
> Objective function:
>  
> Min: Sum( (X[i] - S[i] )^2) 
>  
> Subject to constraint :
>  
> Sum (B[i] x X[i]) =0
>  
> where i=1��n and S[i] and B[i] are real numbers
>  
> Need to solve for X
>  
> Example:
>  
> Assume n=3
>  
> S <- c(-0.5, 7.8, 2.3)
> B <- c(0.42, 1.12, 0.78)
>  
> Many thanks
> AY
> 
> 
> 
>   [[alternative HTML version deleted]]
> 
> 
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.