tpapp / DynamicHMC.jl

Implementation of robust dynamic Hamiltonian Monte Carlo methods (NUTS) in Julia.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different behaviour when running code in the REPL or in Jupyter notebook

falbarelli opened this issue · comments

Hi, I also wrote a message on the julia discourse board..
I'm on the latest julia version (1.2 stable) and I pulled TransformVariables, LogDensityProblems and DynamicHMC from the master branch. I'm using Julia.
I have reproduced the problem both on Ubuntu 16.04 and macOS 10.14.6.

It's probably a very stupid problem, but I can't figure it out!

Self-contained example that demonstrates the problem

using LinearAlgebra
using StaticArrays
using TransformVariables
using LogDensityProblems
using DynamicHMC
using Parameters
using Random

function LogLikelihoodMin(ω,dyDep)
    
 dt = 0.1
 cD = [0 -1im ; 1im 0]
 H =/2.) * cD
 rho=[0.05 0 ; 0 0.95]
 M = I - ((cD'*cD)/2) * dt + (cD * dyDep) - 1im * H * dt   
 newRho = M * rho * M'
 lklhood = real(tr(newRho))-* dt/2)^2 
 return log(lklhood)

end

struct Experiment
      dyDep::Float64
end

function (problem::Experiment)((ω,)::NamedTuple{(:ω,)})
      @unpack dyDep  = problem        # extract the data
      LogLikelihoodMin(ω,dyDep)
end

dyDepObs=0.690691
p1 = Experiment(dyDepObs)

print(p1((ω=.4,)))

trans_single = as((ω=as(Real, 2, 4),))
P1 = TransformedLogDensity(trans_single, p1)
∇P1 = ADgradient(:ForwardDiff, P1)

results = mcmc_with_warmup(Random.GLOBAL_RNG, ∇P1, 1000)

Output, expected outcome, comparison to other samplers

Basically, if I run this code in the REPL or launch it as a script it runs correctly, while if I copy and paste it in a Jupyter notebook I get the error

ArgumentError: nothing should not be printed; use show, repr, or custom output instead.

See the whole stacktrace here.

I could not replicate this.

Please make sure that DynamicHMC is at 2.0.0, eg with a

import Pkg
Pkg.status()

cell in the notebook.

If this appears to be correct, please attach the Jupypter notebook as a file.

The version is DynamicHMC v2.0.0 #master.

Thanks again for the support and for your project.
I found this behaviour very weird and I am afraid I am doing something stupid.
Find the the notebook here.

Oh I see that you have opened a new issue.
Yes, I think the problem is the output of the interactive progress report, if I add reporter = NoProgressReport() it runs fine.

Yes, that's what I thought, but now I can confurm -- fixing #87 will provide a workaround for this.

The underlying issue in Julia appears to have been fixed in JuliaLang/julia#32148, but I will just clean up the reporting in the meantime.

BTW,

@info "foo" nothing

is an MWE.

Oh thanks, now I understand, I didn't realize it was a more general issue in Julia.

Thanks for reporting, I fixed it and will tag a release soon.