# DynamicHMC: Reached maximum number of iterations while bisecting interval for epsilon

**URL:** https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595
**Category:** General Usage
**Created:** [June 26, 2021, 8:29am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595 "2021-06-26T08:29:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [June 26, 2021, 8:29am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/1 "2021-06-26T08:29:25Z")

</div>

@Tamas_Papp

I saw there is a [previous thread](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations/24721) discussing this problem, but I don’t quite get the solution so I’m starting a new one.

“Reached maximum number of iterations while bisecting interval for ϵ.”

I’ve seen this error message before and for that problem it turned out that my gradients were wrong. However, for this new problem, I’ve triple-checked my log density and gradients and don’t find any problem with them. Moreover, the parameter values from these iterations are quite close to the truth, which suggests to me that it kinda works, but I still get this error message. I also noticed that two things may happen to the parameter values, the log density, and the gradients: they either do not change after the first few iterations, or they oscillate between two sets of values. Then after many iterations this error message pops up.

I wonder if you have suggestions on what the issue might be?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 26, 2021, 8:39am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/2 "2021-06-26T08:39:09Z")

</div>

It would be much easier to investigate what happens with a self-contained example I can run.

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [June 28, 2021, 2:46am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/3 "2021-06-28T02:46:44Z")

</div>

Unfortunately much of the code is unpublished work, but I’ll try to come up with a small example.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 28, 2021, 4:04am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/4 "2021-06-28T04:04:14Z")

</div>

You can send it privately and I will treat it confidential.

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [July 4, 2021, 5:28am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/5 "2021-07-04T05:28:12Z")

</div>

Thank you for offering to help and I really appreciate it.

I found the problem: I need to create a gradient vector every time the function `logdensity_and_gradient` is called.

Previously I defined the problem as

```julia
struct MyProblem{T <: Real}
    ...
    ∇ :: Vector{T} # gradient
end

```

and calculated the gradient using

```julia
function LogDensityProblems.logdensity_and_gradient(problem::MyProblem{T}, pars) where T <: Real
    @unpack ..., ∇ = problem
    ...
    logl = ...
    copyto!(∇, ...)
    logl, ∇
end

```

But this won’t work. Instead, I need to use

```julia
function LogDensityProblems.logdensity_and_gradient(problem::MyProblem{T}, pars) where T <: Real
    ...
    logl = ...
    ∇ = fill(T(0), p)
    copyto!(∇, ...)
    logl, ∇
end

```

Although it runs now, I wonder if there is a way to avoid repeatedly allocating the gradient vector?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 5, 2021, 7:39am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/6 "2021-07-05T07:39:32Z")

</div>

> [@kai](#):
>
> Although it runs now, I wonder if there is a way to avoid repeatedly allocating the gradient vector?

Not really, the API expects a fresh one. This keeps the code of DynamicHMC really clean in a functional style, at a small cost (for small dimensions, you can use `SVector`, for large ones, the cost of AD should dominate except for trivial problems).

That said, this was not explicitly documented, so I did

> <https://github.com/tpapp/LogDensityProblems.jl/pull/77>
>
> Cf \[discussion\](https://discourse.julialang.org/t/dynamichmc-reached-maximum-num…ber-of-iterations-while-bisecting-interval-for-epsilon/63595).

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [July 5, 2021, 1:41pm UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/7 "2021-07-05T13:41:41Z")

</div>

Got it. Thank you very much for the explanation.

---

<div class="post-metadata">

### Author: ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)
#### Post date: [August 19, 2021, 12:06am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/8 "2021-08-19T00:06:30Z")

</div>

To see how much speed-up I could get by avoiding the repeated allocation (problem dimension `p=500,000`), I tried defining the gradient vector as a global variable (`global \nabla = zeros(p)`) and updating it in the `logdensity_and_gradient` function. However, the gradient doesn’t seem to be updated correctly.

Is this expected?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 19, 2021, 10:56am UTC](https://discourse.julialang.org/t/dynamichmc-reached-maximum-number-of-iterations-while-bisecting-interval-for-epsilon/63595/9 "2021-08-19T10:56:11Z")

</div>

Possibly, but hard to say anything concrete without an MWE.

That said, with a dimension that high GC may be the least of your worries; a dimension that high may run into issues with NUTS (except for some special cases, independence or near-normality).
