title: Questions about boundary constraints with L-BFGS-B method in optim() in R
body: <p>I am trying to use L-BFGS-B method in optim() to find out the minimum value of the following function:</p>
  <pre><code>ip&lt;-function(x) log(mean(exp(return*x))) , where &quot;return&quot; is a series of constants.
  </code></pre>
  <p>First, I gave no boundary constraints: <code>rst1&lt;-optim (-1,ip,method=&quot;L-BFGS-B&quot;)</code>, and it provided a reasonable answer <code>(x=-118.44,ip.min=-0.00017)</code>, which could be justified by both theory and excel calculation. The given message in the result was</p>
  <blockquote>
  <p>CONVERGENCE: NORM OF PROJECTED GRADIENT &lt;= PGTOL.</p>
  </blockquote>
  <p>As x must be less than zero in theory, I then added boundary constraints to the optimizer: <code>rst2&lt;-optim (-1,ip,method=&quot;L-BFGS-B&quot;,lower=-Inf,upper=0)</code>. However, this time it only provided an answer calculated by the initial parameter (-1), which is obviously not the minimum value. The given message in the result was</p>
  <blockquote>
  <p>CONVERGENCE: REL_REDUCTION_OF_F &lt;= FACTR*EPSMCH.</p>
  </blockquote>
  <p>I then tried other boundary constraints, and no matter what they were, as long as boundary constraints were added here, it would always provided an answer calculated by the initial parameter, and failed to find the minimum value.</p>
  <p>Does anyone know why this happens? Many thanks.</p>
  <hr />
  <h1>example</h1>
  <p>rtntxt&lt;-&quot;
  return
  9.15051E-05
  9.67217E-07
  1.34187E-05
  -0.000105801
  0.000111004
  0.000228786
  3.84068E-06
  0.000388639
  -0.000122291
  -7.73028E-05
  4.97595E-05
  -3.97503E-05
  1.86449E-05
  -0.000137739
  -0.000180709
  -1.07254E-05
  3.89723E-05
  &quot;</p>
  <p>rtn&lt;-read.table(text=rtntxt,header=TRUE)</p>
  <p>ip&lt;-function(x) log(mean(exp(rtn$return*x)))</p>
  <p>rst1&lt;-optim(-1,ip,method=&quot;L-BFGS-B&quot;)  #no boundaries</p>
  <p>rst2&lt;-optim(-1,ip,method=&quot;L-BFGS-B&quot;,lower=-Inf,upper=0)  #with boundaries</p>
  <h1>plot</h1>
  <p>x&lt;- -10000:10000</p>
  <p>n&lt;-length(x)</p>
  <p>s&lt;-numeric(n)</p>
  <p>for(i in 1:n) s[i]&lt;-ip(x[i])</p>
  <p>plot(x,s)</p>
  <p>x[which(s==min(s))] #rst1(no boundaries) is correct</p>
  <p>min(s)</p>