Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:22:45+00:00 2026-06-09T18:22:45+00:00

Is optimx() providing incorrect solution or am I missing a simple point? Thank you!

  • 0

Is optimx() providing incorrect solution or am I missing a simple point? Thank you!

I am trying to maximize a very simple likelihood. This is a non-parametric likelihood
in the sense that the distribution of F is not specified parametrically. Rather,
for each observed xi, f(xi)=pi and thus log(Likelihood)=Sum(log(f(xi)))=Sum(log(pi)).

The function I am trying to maximize is: sum(log(pi))+lamda(sum(pi-1))
where sum(pi)=1 (i.e. this is a constrained maximization problem which can be solved using Lagrange multiplier).

The answer to this problem is pi=1/n where n is the number of data points. However, optimx does not seem to give this solution. Does anybody have any idea. If n=2, the function I am maximizing is log(p1)+log(p2)+lamda(p1+p2-1).

Here is my code and output from R:

n=2
log.like=function(p)
{
  lamda=p[n+1]
  ll=0
  for(i in 1:n){
    temp = log(p[i])+lamda*p[i]-lamda/(n)
    ll=ll+temp
  }
  return(-ll)
}


mle = optimx(c(0.48,.52,-1.5),
             log.like,
             lower=c(rep(0.1,2),-3),
             upper=c(rep(.9,2),-1),
             method = "L-BFGS-B")

> mle
             par  fvalues   method fns grs itns conv  KKT1 KKT2 xtimes
1 0.9, 0.9, -1.0 1.010721 L-BFGS-B   8   8 NULL    0 FALSE   NA      0

The solution to the equation when n=2 is p1=p2=1/2 and lamda=-2. However, I do not get this when using optimx. Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:22:47+00:00Added an answer on June 9, 2026 at 6:22 pm

    Nothing wrong with optimx. Take a step back and look at the function you want to maximize: log(p1) + log(p2) + lamda*(p1+p2-1). It’s quite intuitive that the optimal solution is to make all variables as large as possible, no? See that optimx rightfully returned the upper bounds you specified.

    So what is wrong with your approach? When using Lagrange multipliers, critical points are saddle points of your function above, and not local minima like optimx would help you find. So you need to modify your problem in a such a way that these saddle points become local minima. This can be done by optimizing over the norm of the gradient, which is easy to compute analytically for your problem. There is a great example (with pictures) here:

    http://en.wikipedia.org/wiki/Lagrange_multiplier#Example:_numerical_optimization.

    For your problem:

    grad.norm <- function(x) {
      lambda <- tail(x, 1)
      p <- head(x, -1)
      h2 <- sum((1/p + lambda)^2) + (sum(p) - 1)^2
    }
    
    optimx(c(.48, .52, -1.5),
           grad.norm,
           lower = c(rep(.1, 2), -3),
           upper = c(rep(.9, 2), -1),
           method = "L-BFGS-B")
    
    #                               par      fvalues   method  fns grs [...]
    # 1 0.5000161, 0.5000161, -1.9999356 1.038786e-09 L-BFGS-B  13  13 [...]
    

    Follow up: If you do not want to or cannot compute the gradient yourself, you can let R compute a numerical approximation, for example:

    log.like <- function(x) {
      lambda <- tail(x, 1)
      p <- head(x, -1)
      return(sum(log(p)) + lambda*(sum(p) - 1))
    }
    
    grad.norm <- function(x) {
      require(numDeriv)
      return(sum(grad(log.like, x)^2))
    }
    
    optimx(c(.48, .52, -1.5),
           grad.norm,
           lower = c(rep(.1, 2), -3),
           upper = c(rep(.9, 2), -1),
           method = "L-BFGS-B")
    
    #                                par      fvalues   method fns grs [...]
    # 1 0.5000161, 0.5000161, -1.9999356 1.038784e-09 L-BFGS-B  13  13 [...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it's a simple issue about optim() function on R. I am trying to find
I've been trying to setup a simple replication system. 1 main mongo, 1 backup,
I am trying to optimise my likelihood function of R_j and R_m using optim
So this a new one for me. I'm trying to define a ConfigurationSection class
I am trying to estimate the shape and scale of a data set. I
I'm trying to have text spans pop up on a hover pseudo-class for different
Currently I am trying to compile native code for Android. The code is from
This is my first post on StackOverflow, so please bear with me. And I
I have a replica set that I am trying to upgrade the primary to
I am trying to set Padding Top to Specified View from another Layout Height

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.