Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lbar becoming larger than Rbar in SliceSampler causing max iterations reached #245

Closed
DominiqueMakowski opened this issue Jul 2, 2024 · 23 comments

Comments

@DominiqueMakowski
Copy link

I've been having issues with running pigeons() on a Turing model and the following error keeps popping up:

ERROR: Maximum number of iterations 1024 reached. Dumping info:
        - Lbar   = -319.26678452802236
        - Rbar   = -319.2667845280223
        - new_lp = 1.1133451220013476e26
        - z      = 1.1133451220013476e26

How do we increase the number of iterations?

I tried adding max_iter argument to the pigeons() call but it seems like it doesn't work

thanks!

@miguelbiron
Copy link
Collaborator

Hi -- that output means that the slicer is stuck trying to find a point with log probability higher than 10^26, which is basically inf. In turn, this suggests that there may be an issue with the log potential. Increasing the iterations likely won't help. It is better to understand why there exist a point with seemingly infinite log potential.

If you post a reproducible example we might be able to help.

@trevorcampbell
Copy link
Collaborator

@miguelbiron can you make a PR to improve that error message? It is certainly confusing if it only says "max iterations reached" -- not clear what "iterations" it's talking about here. Probably want to say something like "slicer maxed out iters while trying to ....". Also want to include a helpful hint about what is going on and how one might fix.

@miguelbiron
Copy link
Collaborator

@trevorcampbell ok that sounds good!

@alexandrebouchard
Copy link
Member

Looking at the Blang implementation I see I had observed cases where due to precision errors the shrinkage can fail, and so I had a check that if the left and right end point are approximately equal, just stay at the old point. (https://github.com/UBC-Stat-ML/blangSDK/blob/b8642c9c2a0adab8a5b6da96f2a7889f1b81b6cc/src/main/java/blang/mcmc/RealSliceSampler.java#L111) I'll do a PR to do the same thing in Pigeons unless there are objections...

@alexandrebouchard alexandrebouchard changed the title How to increase the number of iterations? Lbar becoming larger than Rbar in SliceSampler causing max iterations reached Aug 13, 2024
@DominiqueMakowski
Copy link
Author

DominiqueMakowski commented Aug 14, 2024

If you post a reproducible example we might be able to help.

Sorry I missed this.
Here's a MWE:

using CSV
using DataFrames
using Turing
using Pigeons
using SequentialSamplingModels
using Downloads
using LinearAlgebra
using Statistics



function data_poly(x, degree=2; orthogonal=false)
    if orthogonal
        z = x .- mean(x)  # Center the data by subtracting its mean
        X = hcat([z .^ deg for deg in 1:degree]...)  # Create the matrix of powers up to 'degree'
        QR = qr(X)  # Perform QR decomposition
        X = Matrix(QR.Q)  # Extract the orthogonal matrix Q
    else
        X = hcat([x .^ deg for deg in 1:degree]...)  # Create the matrix of powers up to 'degree'
    end
    return X
end

@model function model_exgaussian(data; min_rt=minimum(data.rt), isi=nothing)

    # Transform ISI into polynomials
    isi = data_poly(isi, 2; orthogonal=true)

    # Priors for coefficients
    drift_intercept ~ Normal(0, 1)
    drift_isi1 ~ Normal(0, 1)
    drift_isi2 ~ Normal(0, 1)

    σ ~ Normal(0, 1)
    τ ~ Normal(log(0.2), 1)

    for i in 1:length(data)
        drift = drift_intercept
        drift += drift_isi1 * isi[i, 1]
        drift += drift_isi2 * isi[i, 2]
        data[i] ~ ExGaussian(exp(drift), exp(σ), exp(τ))
    end
end


df = CSV.read(Downloads.download("https://raw.githubusercontent.com/RealityBending/DoggoNogo/main/study1/data/data_game.csv"), DataFrame)

fit = model_exgaussian(df.RT, min_rt=minimum(df.RT), isi=df.ISI)
pt = pigeons(target=TuringLogPotential(fit);
    record=[Pigeons.traces],
    n_rounds=5,
    n_chains=10,
    # checkpoint=true,
    multithreaded=true,
    seed=123)
────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ)
────────── ────────── ────────── ────────── ────────── ────────── ──────────
        2       4.97        195          0      0.447          1          1 
        4       2.48   6.49e+03   1.01e-46      0.724          1          1 
        8        3.6    8.5e+03  1.93e-111        0.6      0.992      0.999 
ERROR: TaskFailedException

    nested task error: Maximum number of iterations 1024 reached. Dumping info:
            - Lbar   = -1.4338880315454723
            - Rbar   = -1.433888031545472
            - new_lp = 5.805675213659945e191
            - z      = 5.805675213659945e191

    Stacktrace:
      [1] error(s::String)
        @ Base .\error.jl:35
      [2] slice_shrink!(h::SliceSampler, replica::Pigeons.Replica{…}, z::Float64, L::Float64, R::Float64, lp_L::Float64, lp_R::Float64, pointer::Base.RefArray{…}, log_potential::InterpolatedLogPotential{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\explorers\SliceSampler.jl:172
      [3] slice_sample_coord!
        @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\explorers\SliceSampler.jl:93 [inlined]
      [4] slice_sample!(h::SliceSampler, state::Vector{…}, log_potential::InterpolatedLogPotential{…}, cached_lp::Float64, replica::Pigeons.Replica{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\explorers\SliceSampler.jl:49
      [5] slice_sample!
        @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\ext\PigeonsDynamicPPLExt\state.jl:67 [inlined]
      [6] step!(explorer::SliceSampler, replica::Pigeons.Replica{DynamicPPL.TypedVarInfo{…}, @NamedTuple{…}}, shared::Shared{...})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\explorers\SliceSampler.jl:28
      [7] explore!(pt::PT{...}, replica::Pigeons.Replica{DynamicPPL.TypedVarInfo{…}, @NamedTuple{…}}, explorer::SliceSampler)
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:107
      [8] macro expansion
        @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:84 [inlined]
      [9] (::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}})(tid::Int64; onethread::Bool)
        @ Pigeons .\threadingconstructs.jl:215
     [10] #206#threadsfor_fun
        @ .\threadingconstructs.jl:182 [inlined]
     [11] (::Base.Threads.var"#1#2"{Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, Int64})()
        @ Base.Threads .\threadingconstructs.jl:154
Stacktrace:
  [1] threading_run(fun::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, static::Bool)
    @ Base.Threads .\threadingconstructs.jl:172
  [2] macro expansion
    @ .\threadingconstructs.jl:220 [inlined]
  [3] explore!
    @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:82 [inlined]
  [4] macro expansion
    @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:50 [inlined]
  [5] macro expansion
    @ .\timing.jl:503 [inlined]
  [6] run_one_round!(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:49
  [7] pigeons(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\pt\pigeons.jl:18
  [8] pigeons(pt_arguments::Inputs{TuringLogPotential{...}, Nothing, Nothing, Nothing, Nothing}, ::Pigeons.ThisProcess)
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\api.jl:19
  [9] #pigeons#166
    @ C:\Users\domma\.julia\packages\Pigeons\nJHDB\src\api.jl:16 [inlined]
 [10] top-level scope
    @ Untitled-1:48
Some type information was truncated. Use `show(err)` to see complete types.

I'm fairly convinced that the model is tightly parametrized. @itsdfish could you correct me if I'm wrong?

@alexandrebouchard
Copy link
Member

Good news, the PR I created to address this issue, PR #268, appears to fix that issue. Running your example I now get:

────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ) 
────────── ────────── ────────── ────────── ────────── ────────── ──────────
        2       4.94   1.01e+29          0      0.451      0.909      0.976 
        4       3.02  2.22e+166  1.29e-107      0.665      0.935      0.984 
        8       5.43  2.15e+297   1.79e-29      0.397          1          1 
       16       7.74  8.27e+296          0       0.14      0.992      0.998 
       32       7.86  7.11e+296          0      0.127      0.992      0.998 
────────────────────────────────────────────────────────────────────────────

Thank you so much for pointing this out!

Relatedly, we have started working on a collection of difficult posterior distributions to help test and improve pigeons. (see beta version here). Would you be interested in having the above model part of this collection? If so, we would just need a bib reference entry to provide credits.

@DominiqueMakowski
Copy link
Author

Using the latest #main version, Pigeons didn't error 🎉 with the following model (feel free to add it to your model zoo but all credits should go to @itsdfish and SequentialSamplingModels):

However, it often spat this new message Warning: The set of successful reports changed

And the traces look funny:

image

MWE:

using CSV
using DataFrames
using Turing
using Pigeons
using SequentialSamplingModels
using Downloads
using LinearAlgebra
using Statistics
using StatsPlots



function data_poly(x, degree=2; orthogonal=false)
    if orthogonal
        z = x .- mean(x)  # Center the data by subtracting its mean
        X = hcat([z .^ deg for deg in 1:degree]...)  # Create the matrix of powers up to 'degree'
        QR = qr(X)  # Perform QR decomposition
        X = Matrix(QR.Q)  # Extract the orthogonal matrix Q
    else
        X = hcat([x .^ deg for deg in 1:degree]...)  # Create the matrix of powers up to 'degree'
    end
    return X
end

@model function model_ExGaussian(rt; min_rt=minimum(data.rt), isi=nothing)

    # Transform ISI into polynomials
    isi = data_poly(isi, 2; orthogonal=true)

    # Priors for coefficients
    μ_intercept ~ Normal(0, 1)
    μ_isi1 ~ Normal(0, 1)
    μ_isi2 ~ Normal(0, 1)

    σ ~ Normal(0, 1)
    τ ~ Normal(log(0.2), 1)

    for i in 1:length(rt)
        μ = μ_intercept
        μ += μ_isi1 * isi[i, 1]
        μ += μ_isi2 * isi[i, 2]
        rt[i] ~ ExGaussian(μ, exp(σ), exp(τ))
    end
end


df = CSV.read(Downloads.download("https://raw.githubusercontent.com/RealityBending/DoggoNogo/main/study1/data/data_game.csv"), DataFrame)

fit = model_ExGaussian(df.RT[1:100, :], min_rt=minimum(df.RT), isi=df.ISI[1:100, :])
pt = pigeons(target=TuringLogPotential(fit);
    record=[Pigeons.traces],
    n_rounds=9,
    n_chains=10,
    multithreaded=true,
    seed=123)

posteriors = Chains(pt)
StatsPlots.plot(posteriors; size=(800, 800))
────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ)
────────── ────────── ────────── ────────── ────────── ────────── ──────────
        2          2       30.4   3.46e-07      0.778      0.968      0.993
        4       3.18    6.4e+56      0.026      0.646      0.814      0.966 
        8       2.75  5.93e+293      0.187      0.695      0.812      0.918
       16       4.18  1.04e+294      0.125      0.536      0.861      0.958
       32       2.44  2.49e+294          0      0.729      0.875       0.96
┌ Warning: The set of successful reports changed
└ @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\report.jl:46
──────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)
────────── ────────── ────────── ────────── ──────────
       64          2  2.98e+294          0      0.778
┌ Warning: The set of successful reports changed
└ @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\report.jl:46
────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ)
────────── ────────── ────────── ────────── ────────── ────────── ──────────
      128       1.98  2.99e+294          0       0.78          1          1
      256          2  2.99e+294          0      0.778          1          1
      512       1.46  2.99e+294          0      0.838          1          1
────────────────────────────────────────────────────────────────────────────

Any pointers as to what I'm doing wrong?

@alexandrebouchard
Copy link
Member

For the "set of report has changed", this should be fixed in commit 1e0484f which I pushed earlier today.

The underlying phenomenon is the following:

  • think of the slice sampler has an automatic Gibbs sampling, informally speaking
  • the added robustness is to cover cases where the conditional distribution is locally degenerate or almost degenerate up to numerical precision, i.e. given all the other random variables, the set of potential configs for the variable being resampled is an extremely small neighbour. In such case, instead of crashing it only stays at current value.
  • what could be the underlying cause is one of two things: the target actually sits on a submanifold, e.g. such as in sampling all K parameters of a K-Dirichlet. Or, as the number of data points increase, the mass of the posterior concentrates more and more on a lower dimensional manifold.

It's helpful to know which of these 2 you are facing. Such target is intrinsically difficult. Contrary to popular belief, HMC often struggle on those as well.

@alexandrebouchard
Copy link
Member

To find which of the 2 you are facing, you can try running on a small subset of the data...

@miguelbiron
Copy link
Collaborator

@itsdfish do you think this issue might be related to the numerical accuracy problem we found here in UtilityModels?

@itsdfish
Copy link
Contributor

@miguelbiron, good question. Here is the logpdf function. I'm not quite sure if anything is problematic. The function returns -Inf when tau=0 because of division by zero and the exponential component of the exgaussian is undefined at zero. Does anything look like an issue to you?

One potential issue might be numberical instability here ExGaussian(μ, exp(σ), exp(τ)) with numbers of large magnitude going into exp. If I understand correctly, that is the problem with UtilityModels. Maybe one way to test is to use truncated distributions as priors?

@itsdfish
Copy link
Contributor

I truncated the priors for sigma and tau:

    σ ~ truncated(Normal(0, 1), 0, Inf)
    τ ~ truncated(Normal(.2, 1), 0, Inf)

I had success with NUTS:

Iterations        = 501:1:1500
Number of chains  = 4
Samples per chain = 1000
Wall duration     = 7.68 seconds
Compute duration  = 24.37 seconds
parameters        = μ_intercept, μ_isi1, μ_isi2, σ, τ
internals         = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size

Summary Statistics
   parameters      mean       std      mcse    ess_bulk    ess_tail      rhat   ess_per_sec 
       Symbol   Float64   Float64   Float64     Float64     Float64   Float64       Float64 

  μ_intercept    0.2506    0.0051    0.0001   2549.6779   2659.4532    1.0003      104.6193
       μ_isi1    0.1195    0.0302    0.0005   3219.7258   2257.5923    1.0010      132.1130
       μ_isi2   -0.1628    0.0412    0.0008   2978.4462   2513.9622    1.0006      122.2127
            σ    0.0126    0.0035    0.0001   2131.8138   1610.1513    1.0004       87.4734
            τ    0.0536    0.0068    0.0001   2725.9081   2470.5110    1.0015      111.8505

Quantiles
   parameters      2.5%     25.0%     50.0%     75.0%     97.5% 
       Symbol   Float64   Float64   Float64   Float64   Float64 

  μ_intercept    0.2411    0.2471    0.2504    0.2539    0.2609
       μ_isi1    0.0617    0.0994    0.1183    0.1389    0.1811
       μ_isi2   -0.2423   -0.1909   -0.1627   -0.1354   -0.0822
            σ    0.0065    0.0101    0.0122    0.0147    0.0202
            τ    0.0412    0.0489    0.0531    0.0578    0.0681

The ratio log(z_1 / z_0) was very large for Pigeons.jl. I suspect this is indicative of a problem.

────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ) 
────────── ────────── ────────── ────────── ────────── ────────── ──────────
        2       3.99       44.7    4.8e-08      0.557      0.968      0.996 
        4        3.9        143   0.000512      0.566      0.984      0.998 
        8       4.27  1.07e+134      0.252      0.525      0.964      0.995 
       16       4.25  5.89e+289      0.161      0.528      0.963      0.989 
       32       5.15  1.82e+294     0.0625      0.428      0.966      0.988 
       64       2.28  2.72e+294          0      0.747      0.878      0.946 
      128          2   2.9e+294          0      0.778          1          1 

@DominiqueMakowski
Copy link
Author

Apologies for the bump, but is it possible that #273 will also help this case or is it completely unrelated?

@miguelbiron
Copy link
Collaborator

miguelbiron commented Aug 21, 2024

Hi @DominiqueMakowski -- I don't know if @itsdfish has ruled out yet numerical instabilities as the issues here. But if they are not -- and I have a feeling that they might not be at least the main culprit, given my comment here -- then it is possible that the models from this package tend to have likelihoods with stricter smaller support than the prior.

If that's the case, then you don't need to wait for #273 to test that; you can just define a custom deterministic initialization for your particular target like so:

fit = model_ExGaussian(df.RT[1:100, :], min_rt=minimum(df.RT), isi=df.ISI[1:100, :])
target = TuringLogPotential(fit)
Pigeons.initialization(::typeof(target), ::AbstractRNG, ::Int64) = zeros(DIMENSION)

where you would need to manually provide the dimension. It does not have to be just zeros either. Just remember that these values are assumed to be in unconstrained space.

@miguelbiron
Copy link
Collaborator

Oh but you know what, your issue is occuring at a different point, after the initialization check so disregard the previous comment.

@miguelbiron
Copy link
Collaborator

You can also try switching the explorer to AutoMALA.

@DominiqueMakowski
Copy link
Author

I can confirm that adding explorer=AutoMALA() to the pigeons() call seems to have done the trick! thanks a lot @miguelbiron that's very helpful ☺️

(note that using Enzyme as in the docs fails for my usecase but hopefully this will get fixed as Enzyme continues to be integrated)

@miguelbiron
Copy link
Collaborator

I'm glad it worked! Yes, enzyme is not working yet in Turing, that's a work in progress on their end. You may get better performance with ReverseDiff, but make sure that you get the same results as woth forwarddiff first.

@DominiqueMakowski
Copy link
Author

DominiqueMakowski commented Aug 21, 2024

Damn it I rejoiced too fast, it warked on a subset of the data but on the whole data it fails with:

ERROR: TaskFailedException

    nested task error: AssertionError: AutoMALA can only be called on a configuration of positive density.
    Stacktrace:
      [1] auto_mala!(rng::SplittableRandoms.SplittableRandom, explorer::AutoMALA{…}, target_log_potential::InterpolatedAD{…}, state::Vector{…}, recorders::@NamedTuple{…}, chain::Int64, use_mh_accept_reject::Bool)
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:130
      [2] _extract_commons_and_run!(explorer::AutoMALA{…}, replica::Pigeons.Replica{…}, shared::Shared{…}, log_potential::InterpolatedLogPotential{…}, state::Vector{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:92
      [3] step!
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\GradientBasedSampler.jl:15 [inlined]
      [4] step!(explorer::AutoMALA{…}, replica::Pigeons.Replica{…}, shared::Shared{…}, vi::DynamicPPL.TypedVarInfo{…})
        @ PigeonsDynamicPPLExt C:\Users\domma\.julia\packages\Pigeons\EJbhL\ext\PigeonsDynamicPPLExt\state.jl:75
      [5] step!
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\GradientBasedSampler.jl:9 [inlined]
      [6] explore!(pt::PT{…}, replica::Pigeons.Replica{…}, explorer::AutoMALA{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:107
      [7] macro expansion
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:84 [inlined]
      [8] (::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}})(tid::Int64; onethread::Bool)
        @ Pigeons .\threadingconstructs.jl:215
      [9] #206#threadsfor_fun
        @ .\threadingconstructs.jl:182 [inlined]
     [10] (::Base.Threads.var"#1#2"{Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, Int64})()
        @ Base.Threads .\threadingconstructs.jl:154
Stacktrace:
  [1] threading_run(fun::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, static::Bool)
    @ Base.Threads .\threadingconstructs.jl:172
  [2] macro expansion
    @ .\threadingconstructs.jl:220 [inlined]
  [3] explore!
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:82 [inlined]
  [4] macro expansion
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:50 [inlined]
  [5] macro expansion
    @ .\timing.jl:503 [inlined]
  [6] run_one_round!(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:49
  [7] pigeons(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:18
  [8] pigeons(pt_arguments::Inputs{TuringLogPotential{…}, Nothing, AutoMALA{…}, Nothing, Nothing}, ::Pigeons.ThisProcess)
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\api.jl:19
  [9] #pigeons#166
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\api.jl:16 [inlined]
 [10] sample_and_save(m::typeof(model_ExGaussian), df::DataFrame, filename::String; pt::Bool)
    @ Main c:\Users\domma\Dropbox\RECHERCHE\Studies\DoggoNogo\study1\analysis\1_models_make.jl:104
 [11] top-level scope
    @ c:\Users\domma\Dropbox\RECHERCHE\Studies\DoggoNogo\study1\analysis\1_models_make.jl:122
Some type information was truncated. Use `show(err)` to see complete types.

I'll try the initialization trick, but could you clarify what you mean by : where you would need to manually provide the dimension what does the dimension correspond to?

@miguelbiron
Copy link
Collaborator

Its the total number of parameters that you're inferring.

@miguelbiron
Copy link
Collaborator

Ah no wait the code I gave above won't work as is, it requires a special construction for turing models. I'm busy now but I can provide an example during the day.

@miguelbiron
Copy link
Collaborator

Ok this should work

fit = model_ExGaussian(df.RT[1:100, :], min_rt=minimum(df.RT), isi=df.ISI[1:100, :])
fit_target = TuringLogPotential(fit)
function Pigeons.initialization(target::typeof(fit_target), rng::AbstractRNG, ::Int64)
    vi = DynamicPPL.VarInfo(rng, target.model, DynamicPPL.SampleFromPrior(), DynamicPPL.PriorContext())
    DynamicPPL.link!!(vi, DynamicPPL.SampleFromPrior(), target.model)    
    d = length(DynamicPPL.getall(vi)) # get dimension
    new_vals = zeros(d)
    DynamicPPL.setall!(vi, new_vals)
    lp = DynamicPPL.logjoint(target.model, vi) # recompute logjoint with new values
    @assert isfinite(lp)
    return vi
end

@DominiqueMakowski
Copy link
Author

Unfortunately despite adding that function it fails with a new error after some time:

────────────────────────────────────────────────────────────────────────────
  scans        Λ      log(Z₁/Z₀)   min(α)     mean(α)    min(αₑ)   mean(αₑ) 
────────── ────────── ────────── ────────── ────────── ────────── ──────────
        2          5  5.39e+230          0      0.445          0      0.364 
        4       2.93  7.85e+230   1.14e-10      0.675      0.389      0.469 
        8       6.28  6.15e+230          0      0.302      0.322      0.571 
       16       7.63  4.56e+230          0      0.152        0.5      0.601 
       32       8.11  3.96e+230          0     0.0985       0.22      0.568 
       64        8.1  3.61e+230          0     0.0996      0.195      0.564 
ERROR: TaskFailedException

    nested task error: Could not find a positive step size with diff > -0.7729812632294719
    Stacktrace:
      [1] error(s::String)
        @ Base .\error.jl:35
      [2] shrink_step_size(log_joint_difference::Pigeons.var"#result#154"{…}, step_size::Float64, lower_bound::Float64)
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:244
      [3] auto_step_size(target_log_potential::InterpolatedAD{…}, diag_precond::Vector{…}, state::Vector{…}, momentum::Vector{…}, recorders::@NamedTuple{…}, chain::Int64, step_size::Float64, lower_bound::Float64, upper_bound::Float64)
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:207
      [4] auto_mala!(rng::SplittableRandoms.SplittableRandom, explorer::AutoMALA{…}, target_log_potential::InterpolatedAD{…}, state::Vector{…}, recorders::@NamedTuple{…}, chain::Int64, use_mh_accept_reject::Bool)
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:159
      [5] _extract_commons_and_run!(explorer::AutoMALA{…}, replica::Pigeons.Replica{…}, shared::Shared{…}, log_potential::InterpolatedLogPotential{…}, state::Vector{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\AutoMALA.jl:92
      [6] step!
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\GradientBasedSampler.jl:15 [inlined]
      [7] step!(explorer::AutoMALA{…}, replica::Pigeons.Replica{…}, shared::Shared{…}, vi::DynamicPPL.TypedVarInfo{…})
        @ PigeonsDynamicPPLExt C:\Users\domma\.julia\packages\Pigeons\EJbhL\ext\PigeonsDynamicPPLExt\state.jl:75
      [8] step!
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\explorers\GradientBasedSampler.jl:9 [inlined]
      [9] explore!(pt::PT{…}, replica::Pigeons.Replica{…}, explorer::AutoMALA{…})
        @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:107
     [10] macro expansion
        @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:84 [inlined]
     [11] (::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}})(tid::Int64; onethread::Bool)
        @ Pigeons .\threadingconstructs.jl:215
     [12] #206#threadsfor_fun
        @ .\threadingconstructs.jl:182 [inlined]
     [13] (::Base.Threads.var"#1#2"{Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, Int64})()
        @ Base.Threads .\threadingconstructs.jl:154
Stacktrace:
  [1] threading_run(fun::Pigeons.var"#206#threadsfor_fun#117"{Pigeons.var"#206#threadsfor_fun#116#118"{…}}, static::Bool)
    @ Base.Threads .\threadingconstructs.jl:172
  [2] macro expansion
    @ .\threadingconstructs.jl:220 [inlined]
  [3] explore!
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:82 [inlined]
  [4] macro expansion
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:50 [inlined]
  [5] macro expansion
    @ .\timing.jl:503 [inlined]
  [6] run_one_round!(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:49
  [7] pigeons(pt::PT{...})
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\pt\pigeons.jl:18
  [8] pigeons(pt_arguments::Inputs{TuringLogPotential{…}, Nothing, AutoMALA{…}, Nothing, Nothing}, ::Pigeons.ThisProcess)
    @ Pigeons C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\api.jl:19
  [9] #pigeons#166
    @ C:\Users\domma\.julia\packages\Pigeons\EJbhL\src\api.jl:16 [inlined]
 [10] sample_and_save(m::typeof(model_ExGaussian), df::DataFrame, filename::String; pt::Bool)
    @ Main c:\Users\domma\Dropbox\RECHERCHE\Studies\DoggoNogo\study1\analysis\1_models_make.jl:114
 [11] top-level scope
    @ c:\Users\domma\Dropbox\RECHERCHE\Studies\DoggoNogo\study1\analysis\1_models_make.jl:134
Some type information was truncated. Use `show(err)` to see complete types.

Note that the same model gets sampled nicely within a few seconds using regular MCMC so again it does seem to me like it's a model specification issue...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants