Code Monkey home page Code Monkey logo

dynamichmcmodels.jl's People

Contributors

github-actions[bot] avatar goedman avatar torkar avatar tpapp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dynamichmcmodels.jl's Issues

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

chimpanzees, preview of DynamicHMC 2.0 API

@goedman I am continuing our discussion at tpapp/DynamicHMC.jl#42 here.

First, I think that the new and refactored DynamicHMC, which will be 2.0, samples this model just fine.

Second, since you are one of the main users, I wanted to get early feedback on the new API, and perhaps an example is the best way to do that.

This is the code, it requires the development branch of DynamicHMC, and of course the data:

#####
##### IMPORTANT: API is WIP, make sure you use this on DynamicHMC#tp/major-api-rewrite-2.0
#####

using DynamicHMC, LogDensityProblems, TransformVariables, StatsFuns, Distributions,
    Parameters, CSV, DataFrames, Random, StanDump, StanRun, StanSamples, PGFPlotsX, StatsBase
import Flux

data = DataFrame(CSV.File("chimpanzees.csv"; delim = ';'))

Base.@kwdef struct Chimpanzees
    N_actors::Int
    pulled_left::Vector{Int}
    prosoc_left::Vector{Int}
    condition::Vector{Int}
    actor::Vector{Int}
end

function make_transformation(model::Chimpanzees)
    as((a = as(Vector, model.N_actors), bp = asℝ, bpC = asℝ))
end

stan_data = (N = length(data.pulled_left), P = data.prosoc_left, C = data.condition,
             L = data.pulled_left, N_chimps = maximum(data.actor), chimp = data.actor)
stan_model = StanModel(joinpath(pwd(), "chimpanzees.stan"))
# just one chain from Stan
stan_chain = first(stan_sample(stan_model, stan_data, 1))
stan_samples = read_samples(first(stan_chain))

model = Chimpanzees(; N_actors = maximum(data.actor), pulled_left = data.pulled_left,
                    prosoc_left = data.prosoc_left, condition = data.condition,
                    actor = data.actor)

function (model::Chimpanzees)(θ)
    @unpack a, bp, bpC = θ
    @unpack pulled_left, prosoc_left, condition, actor = model
    ℓ_likelihood = mapreduce(+, actor, condition, prosoc_left,
                             pulled_left) do actor, condition, prosoc_left, pulled_left
                                 p = logistic(a[actor] + (bp + bpC * condition) * prosoc_left)
                                 logpdf(Bernoulli(p), pulled_left)
                             end
    P = Normal(0, 10)
    ℓ_prior = logpdf(P, bpC) + logpdf(P, bp) + sum(a -> logpdf(P, a), a)
    ℓ_prior + ℓ_likelihood
end

P = TransformedLogDensity(make_transformation(model), model)
∇P = ADgradient(:Flux, P)
results = mcmc_with_warmup(Random.GLOBAL_RNG, ∇P, 1000)
posterior = P.transformation.(results.chain)

function comparison_plot(xlabel, dhmc_values, stan_values)
    @pgf Axis({ xlabel = xlabel, ylabel = "ecdf", legend_pos = "south east" },
              Plot({ no_marks, red }, Table(ecdf(dhmc_values))),
              LegendEntry("DynamicHMC"),
              Plot({ no_marks, blue }, Table(ecdf(stan_values))),
              LegendEntry("Stan"))
end

###
### plots will show up interactively
###

comparison_plot("bp", getfield.(posterior, :bp), stan_samples.bp)
comparison_plot("bpC", getfield.(posterior, :bpC), stan_samples.bpc)
p7 = [comparison_plot("a[$(i)]", getindex.(getfield.(posterior, :a), Ref(i)),
                      stan_samples.a_chimp[i, :]) for i in 1:7]

p7[1]
p7[2]
p7[3]
p7[4]
p7[5]
p7[6]
p7[7]

NUTS_statistics(results.tree_statistics)

EBFMI(results.tree_statistics)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.