Re: [R] adding overall constraint in optim()
typo: lambda * (sum(wgt.wect) - 1) On Sun, May 6, 2018 at 10:51 AM, Eric Berger wrote: > Hi Michael, > A few comments > 1. To add the constraint sum(wgt.vect=1) you would use the method of > Lagrange multipliers. > What this means is that in addition to the w_i (the components of the > weight variables) you would add an additional variable, call it lambda. > Then you would modify your optim.fun() function to add the term > lambda * (sum(wgt.vect - 1) > 2. Are you sure that you have defined Mo.vect correctly? It is a scalar > the way you have written it. > 3. Similarly your definition of wgt.vect creates a scalar. > > HTH, > Eric > > > On Fri, May 4, 2018 at 5:18 AM, Joshua Ulrich > wrote: > >> On Thu, May 3, 2018 at 2:03 PM, Michael Ashton >> wrote: >> > Thanks Bert. But everyone on that forum wants to use finance tools >> rather than general optimization stuff! And I am not optimizing a >> traditional Markowitz mean-variance problem. Plus, smarter people here. :-) >> > >> I'm very confused by these statements. Most of the "finance tools" >> use general-purpose global and/or stochastic optimization packages >> (e.g. rugarch uses nloptr and Rsolnp, PortfolioAnalytics uses DEoptim, >> pso, GenSA). And most (all?) of those optimization packages have ways >> to specify box, equality, and nonlinear inequality constraints. >> >> And I can't recall the last time someone emailed the list about >> optimizing a traditional Markowitz mean-variance problem... maybe 10 >> years ago? >> >> >> On May 3, 2018, at 3:01 PM, Bert Gunter >> wrote: >> >> >> >> You can't -- at least as I read the docs for ?optim (but I'm pretty >> >> ignorant about this, so maybe there's a way to tweak it so you can). >> >> >> >> See here: https://cran.r-project.org/web/views/Optimization.html >> >> for other R optimization capabilities. >> >> >> >> Also, given your credentials, the r-sig-finance list might be a >> >> better place for you to post your query. >> >> >> >> Cheers, >> >> Bert >> >> >> >> >> >> Bert Gunter >> >> >> >> "The trouble with having an open mind is that people keep coming along >> >> and sticking things into it." >> >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> >> >> >> >> On Thu, May 3, 2018 at 10:52 AM, Michael Ashton >> >> wrote: >> >>> Hi โ >> >>> >> >>> This is giving me a headache. Iโm trying to do a relatively simple >> optimization โ actually trying to approximate the output from the Excel >> Solver function but at roughly 1000x the speed. ๐ >> >>> >> >>> The optimization parameters look like this. The only trouble is that >> I want to add a constraint that sum(wgt.vect)=1, and I canโt figure out how >> to do that in optim. >> >>> >> >>> Mo.vect <- as.vector(tail(head(mo,i),1)) >> >>> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) >> >>> cov.mat <- cov(tail(head(morets,i+12),12)) >> >>> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / >> (t(wgt.vect) %*% (cov.mat %*% wgt.vect)) >> >>> >> >>> LowerBounds<-c(0.2,0.05,0.1,0,0,0) >> >>> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) >> >>> >> >>> OptimSolution<-optim(wgt.vect, fn=opt.fun, >> method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) >> >>> >> >>> >> >>> Any thoughts are appreciated! >> >>> >> >>> Mike >> >>> >> >>> Michael Ashton, CFA >> >>> Managing Principal >> >>> >> >>> Enduring Investments LLC >> >>> W: 973.457.4602 >> >>> C: 551.655.8006 >> >>> >> >>> >> >>>[[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/posti >> ng-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/posti >> ng-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> >> >> -- >> Joshua Ulrich | about.me/joshuaulrich >> FOSS Trading | www.fosstrading.com >> R/Finance 2018 | www.rinfinance.com >> >> __ >> 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/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[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
Re: [R] adding overall constraint in optim()
Hi Michael, A few comments 1. To add the constraint sum(wgt.vect=1) you would use the method of Lagrange multipliers. What this means is that in addition to the w_i (the components of the weight variables) you would add an additional variable, call it lambda. Then you would modify your optim.fun() function to add the term lambda * (sum(wgt.vect - 1) 2. Are you sure that you have defined Mo.vect correctly? It is a scalar the way you have written it. 3. Similarly your definition of wgt.vect creates a scalar. HTH, Eric On Fri, May 4, 2018 at 5:18 AM, Joshua Ulrich wrote: > On Thu, May 3, 2018 at 2:03 PM, Michael Ashton > wrote: > > Thanks Bert. But everyone on that forum wants to use finance tools > rather than general optimization stuff! And I am not optimizing a > traditional Markowitz mean-variance problem. Plus, smarter people here. :-) > > > I'm very confused by these statements. Most of the "finance tools" > use general-purpose global and/or stochastic optimization packages > (e.g. rugarch uses nloptr and Rsolnp, PortfolioAnalytics uses DEoptim, > pso, GenSA). And most (all?) of those optimization packages have ways > to specify box, equality, and nonlinear inequality constraints. > > And I can't recall the last time someone emailed the list about > optimizing a traditional Markowitz mean-variance problem... maybe 10 > years ago? > > >> On May 3, 2018, at 3:01 PM, Bert Gunter wrote: > >> > >> You can't -- at least as I read the docs for ?optim (but I'm pretty > >> ignorant about this, so maybe there's a way to tweak it so you can). > >> > >> See here: https://cran.r-project.org/web/views/Optimization.html > >> for other R optimization capabilities. > >> > >> Also, given your credentials, the r-sig-finance list might be a > >> better place for you to post your query. > >> > >> Cheers, > >> Bert > >> > >> > >> Bert Gunter > >> > >> "The trouble with having an open mind is that people keep coming along > >> and sticking things into it." > >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > >> > >> > >> On Thu, May 3, 2018 at 10:52 AM, Michael Ashton > >> wrote: > >>> Hi โ > >>> > >>> This is giving me a headache. Iโm trying to do a relatively simple > optimization โ actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. ๐ > >>> > >>> The optimization parameters look like this. The only trouble is that I > want to add a constraint that sum(wgt.vect)=1, and I canโt figure out how > to do that in optim. > >>> > >>> Mo.vect <- as.vector(tail(head(mo,i),1)) > >>> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > >>> cov.mat <- cov(tail(head(morets,i+12),12)) > >>> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / > (t(wgt.vect) %*% (cov.mat %*% wgt.vect)) > >>> > >>> LowerBounds<-c(0.2,0.05,0.1,0,0,0) > >>> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > >>> > >>> OptimSolution<-optim(wgt.vect, fn=opt.fun, method="L-BFGS-B",lower= > LowerBounds,upper=UpperBounds) > >>> > >>> > >>> Any thoughts are appreciated! > >>> > >>> Mike > >>> > >>> Michael Ashton, CFA > >>> Managing Principal > >>> > >>> Enduring Investments LLC > >>> W: 973.457.4602 > >>> C: 551.655.8006 > >>> > >>> > >>>[[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. > > > > -- > Joshua Ulrich | about.me/joshuaulrich > FOSS Trading | www.fosstrading.com > R/Finance 2018 | www.rinfinance.com > > __ > 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. > [[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] adding overall constraint in optim()
Here is what you do for your problem: require(BB) Mo.vect <- as.vector(tail(head(mo,i),1)) wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) cov.mat <- cov(tail(head(morets,i+12),12)) opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) %*% (cov.mat %*% wgt.vect)) LowerBounds<-c(0.2,0.05,0.1,0,0,0) UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) spgSolution <- spg(wgt.vect, fn=opt.fun, lower=LowerBounds, upper=UpperBounds, project="projectLinear", projectArgs=list(A=matrix(1, 1, length(wgt.vect)), b=1, meq=1))) Ravi From: Ravi Varadhan Sent: Saturday, May 5, 2018 12:31 PM To: m.ash...@enduringinvestments.com; r-help@r-project.org Subject: adding overall constraint in optim() Hi, You can use the projectLinear argument in BB::spg to optimize with linear equality/inequality constraints. Here is how you implement the constraint that all parameters sum to 1. require(BB) spg(par=p0, fn=myFn, project="projectLinear", projectArgs=list(A=matrix(1, 1, length(p0)), b=1, meq=1)) Hope this is helpful, Ravi [[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] adding overall constraint in optim()
Hi, You can use the projectLinear argument in BB::spg to optimize with linear equality/inequality constraints. Here is how you implement the constraint that all parameters sum to 1. require(BB) spg(par=p0, fn=myFn, project="projectLinear", projectArgs=list(A=matrix(1, 1, length(p0)), b=1, meq=1)) Hope this is helpful, Ravi [[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] adding overall constraint in optim()
On Thu, May 3, 2018 at 2:03 PM, Michael Ashton wrote: > Thanks Bert. But everyone on that forum wants to use finance tools rather > than general optimization stuff! And I am not optimizing a traditional > Markowitz mean-variance problem. Plus, smarter people here. :-) > I'm very confused by these statements. Most of the "finance tools" use general-purpose global and/or stochastic optimization packages (e.g. rugarch uses nloptr and Rsolnp, PortfolioAnalytics uses DEoptim, pso, GenSA). And most (all?) of those optimization packages have ways to specify box, equality, and nonlinear inequality constraints. And I can't recall the last time someone emailed the list about optimizing a traditional Markowitz mean-variance problem... maybe 10 years ago? >> On May 3, 2018, at 3:01 PM, Bert Gunter wrote: >> >> You can't -- at least as I read the docs for ?optim (but I'm pretty >> ignorant about this, so maybe there's a way to tweak it so you can). >> >> See here: https://cran.r-project.org/web/views/Optimization.html >> for other R optimization capabilities. >> >> Also, given your credentials, the r-sig-finance list might be a >> better place for you to post your query. >> >> Cheers, >> Bert >> >> >> Bert Gunter >> >> "The trouble with having an open mind is that people keep coming along >> and sticking things into it." >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> >> On Thu, May 3, 2018 at 10:52 AM, Michael Ashton >> wrote: >>> Hi โ >>> >>> This is giving me a headache. Iโm trying to do a relatively simple >>> optimization โ actually trying to approximate the output from the Excel >>> Solver function but at roughly 1000x the speed. ๐ >>> >>> The optimization parameters look like this. The only trouble is that I want >>> to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do >>> that in optim. >>> >>> Mo.vect <- as.vector(tail(head(mo,i),1)) >>> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) >>> cov.mat <- cov(tail(head(morets,i+12),12)) >>> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) >>> %*% (cov.mat %*% wgt.vect)) >>> >>> LowerBounds<-c(0.2,0.05,0.1,0,0,0) >>> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) >>> >>> OptimSolution<-optim(wgt.vect, fn=opt.fun, >>> method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) >>> >>> >>> Any thoughts are appreciated! >>> >>> Mike >>> >>> Michael Ashton, CFA >>> Managing Principal >>> >>> Enduring Investments LLC >>> W: 973.457.4602 >>> C: 551.655.8006 >>> >>> >>>[[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. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2018 | www.rinfinance.com __ 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] adding overall constraint in optim()
This looks like what I call a sumscale problem i.e., some sort of simple function of the parameters sums to a constant. I've done some work on these, but don't have it with me just now. There are several approaches, but they can be quite tricky. Will send some info in about a week or so if you are still stuck and contact me offline. JN (you'll see my name on 3/5 of optim routines via ?optim) On 2018-05-03 01:52 PM, Michael Ashton wrote: > Hi โ > > This is giving me a headache. Iโm trying to do a relatively simple > optimization โ actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. ๐ > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! > > Mike > > Michael Ashton, CFA > Managing Principal > > Enduring Investments LLC > W: 973.457.4602 > C: 551.655.8006 > > > [[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.
Re: [R] adding overall constraint in optim()
> On May 3, 2018, at 10:52 AM, Michael Ashton > wrote: > > Hi โ > > This is giving me a headache. Iโm trying to do a relatively simple > optimization โ actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. ๐ > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! Have you reviewed the documentation for constrOptim {in pkg-stats}? > > > [[alternative HTML version deleted]] I was surprised that emoji made it through. Nonetheless it is usually much safer to posting in plain-text. Didn't seem to be a problem in this case but code and data are often mangled. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law __ 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] adding overall constraint in optim()
Thanks Bert. But everyone on that forum wants to use finance tools rather than general optimization stuff! And I am not optimizing a traditional Markowitz mean-variance problem. Plus, smarter people here. :-) > On May 3, 2018, at 3:01 PM, Bert Gunter wrote: > > You can't -- at least as I read the docs for ?optim (but I'm pretty > ignorant about this, so maybe there's a way to tweak it so you can). > > See here: https://cran.r-project.org/web/views/Optimization.html > for other R optimization capabilities. > > Also, given your credentials, the r-sig-finance list might be a > better place for you to post your query. > > Cheers, > Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Thu, May 3, 2018 at 10:52 AM, Michael Ashton > wrote: >> Hi โ >> >> This is giving me a headache. Iโm trying to do a relatively simple >> optimization โ actually trying to approximate the output from the Excel >> Solver function but at roughly 1000x the speed. ๐ >> >> The optimization parameters look like this. The only trouble is that I want >> to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do >> that in optim. >> >> Mo.vect <- as.vector(tail(head(mo,i),1)) >> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) >> cov.mat <- cov(tail(head(morets,i+12),12)) >> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) >> %*% (cov.mat %*% wgt.vect)) >> >> LowerBounds<-c(0.2,0.05,0.1,0,0,0) >> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) >> >> OptimSolution<-optim(wgt.vect, fn=opt.fun, >> method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) >> >> >> Any thoughts are appreciated! >> >> Mike >> >> Michael Ashton, CFA >> Managing Principal >> >> Enduring Investments LLC >> W: 973.457.4602 >> C: 551.655.8006 >> >> >>[[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.
Re: [R] adding overall constraint in optim()
You can't -- at least as I read the docs for ?optim (but I'm pretty ignorant about this, so maybe there's a way to tweak it so you can). See here: https://cran.r-project.org/web/views/Optimization.html for other R optimization capabilities. Also, given your credentials, the r-sig-finance list might be a better place for you to post your query. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, May 3, 2018 at 10:52 AM, Michael Ashton wrote: > Hi โ > > This is giving me a headache. Iโm trying to do a relatively simple > optimization โ actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. ๐ > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! > > Mike > > Michael Ashton, CFA > Managing Principal > > Enduring Investments LLC > W: 973.457.4602 > C: 551.655.8006 > > > [[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.
[R] adding overall constraint in optim()
Hi โ This is giving me a headache. Iโm trying to do a relatively simple optimization โ actually trying to approximate the output from the Excel Solver function but at roughly 1000x the speed. ๐ The optimization parameters look like this. The only trouble is that I want to add a constraint that sum(wgt.vect)=1, and I canโt figure out how to do that in optim. Mo.vect <- as.vector(tail(head(mo,i),1)) wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) cov.mat <- cov(tail(head(morets,i+12),12)) opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) %*% (cov.mat %*% wgt.vect)) LowerBounds<-c(0.2,0.05,0.1,0,0,0) UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) OptimSolution<-optim(wgt.vect, fn=opt.fun, method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) Any thoughts are appreciated! Mike Michael Ashton, CFA Managing Principal Enduring Investments LLC W: 973.457.4602 C: 551.655.8006 [[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.