Giter VIP home page Giter VIP logo

litehf.jl's Introduction

LiteHF.jl

Dev Build Status Codecov DOI

TODO

  • Implement teststatistics helper functions
  • Re-structre the PyHFModel such that the POI component can be swapped out.

Load pyhf JSON:

using LiteHF, Optim

dict = load_pyhfjson("./test/sample.json");

pyhfmodel = build_pyhf(dict);
LL = pyhf_logjointof(pyhfmodel)

@show Optim.maximizer(maximize(LL, pyhfmodel.inits))
# 2-element Vector{Float64}:
#   1.3064374172547253
#  -0.060413406717672286
@show pyhfmodel.prior_names
# (:mu, :theta)

pyhf JSON + Turing.jl:

using LiteHF, Turing, Optim

dict = load_pyhfjson("./test/sample.json");

const pyhfmodel = build_pyhf(dict);
# unpack `NamedTuple` into just an array of prior distributions
const priors_array = collect(values(priors(pyhfmodel)))

@model function mymodel(observed)
    αs ~ arraydist(priors_array)
    expected = pyhfmodel.expected(αs)
    @. observed ~ Poisson(expected)
end

observed_data = [34,22,13,11];
@show optimize(mymodel(observed_data), MAP(), inits(pyhfmodel))
#ModeResult with maximized lp of -13.51
# 2-element Named Vector{Float64}
# A               │ 
# ────────────────┼───────────
# Symbol("αs[1]") │    1.30648
# Symbol("αs[2]") │ -0.0605151

pyhf JSON + BAT.jl:

using LiteHF, BAT

pydict = load_pyhfjson("./test/sample.json");

pyhfmodel = build_pyhf(pydict);

LL = pyhf_logjointof(pyhfmodel)

mylikelihood(αs) = BAT.LogDVal(LL(αs))
posterior = PosteriorDensity(mylikelihood, priors(pyhfmodel))

@show bat_findmode(posterior).result
# (mu = 1.3064647047644158, theta = -0.06049852104383994)

Manual Example

using Turing, LiteHF, Optim

###### Dummy data ######
const v_data = [34,22,13,11] # observed data
const v_sig = [2,3,4,5] # signal
const v_bg = [30,19,9,4] # BKG
const variations = [1,2,3,3]

###### Background and Signal modifier definitions ######
const bkgmodis =[
                 Histosys(v_bg .+ variations, v_bg .- variations),
                 Normsys(1.1, 0.9)
                ]
const bkgexp = ExpCounts(v_bg, ["theta1", "theta2"], bkgmodis)

const sigmodis = [Normfactor()];
const sigexp = ExpCounts(v_sig, ["mu"], sigmodis);


###### Expected counts as a function of μ and θs
function expected_bincounts2(μ, θs)
    sigexp((mu = μ, )) + bkgexp((theta1=θs[1], theta2=θs[2]))
end

###### Turing.jl models
@model function binned_b(bincounts)
    μ ~ Turing.Flat()
    θs ~ filldist(Normal(), 2)

    expected = expected_bincounts2(μ, θs)
    @. bincounts ~ Poisson(expected)
end

###### Feed observed data to model to construct a posterior/likelihood object
const mymodel = binned_b(v_data);

###### Inference
chain_map = optimize(mymodel, MAP(), [1,1,1]) # initial guesses
display(chain_map)

Result:

ModeResult with maximized lp of -13.23
3-element Named Vector{Float64}
A               │ 
────────────────┼───────────
:μ              │     1.0383
Symbol("θs[1]") │   0.032979
Symbol("θs[2]") │ -0.0352236⏎  

litehf.jl's People

Contributors

github-actions[bot] avatar grabanal avatar moelf avatar oschulz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

grabanal

litehf.jl's Issues

Asimov `q0` doesn't agree with pyhf

Using the sample file:

with open("./test/pyhfjson/sample_normsys.json") as serialized:
    spec = json.load(serialized)
 
workspace = pyhf.Workspace(spec)

pdf_pyhf = workspace.model(modifier_settings={
            'normsys': {'interpcode': 'code1'},
             'histosys': {'interpcode': 'code0'}}
)

data_pyhf = workspace.data(pdf_pyhf)

calc = pyhf.infer.calculators.AsymptoticCalculator(data_pyhf, pdf_pyhf, test_stat="q0")

In [21]: asimov_data = pyhf.infer.calculators.generate_asimov_data(1.0, calc.data, calc.pdf, calc
    ...: .init_pars, calc.par_bounds, calc.fixed_params)

In [26]: asimov_data
Out[26]: array([33.24940695, 22.79129107, 13.37482208,  9.16658759,  0.15552048])

In [23]: pyhf.infer.test_statistics.q0(0.0, calc.data, calc.pdf, calc.init_pars, calc.par_bounds,
    ...:  calc.fixed_params)
Out[23]: array(6.57480397)

In [24]: pyhf.infer.test_statistics.q0(0.0, asimov_data, calc.pdf, calc.init_pars, calc.par_bound
    ...: s, calc.fixed_params)
Out[24]: array(4.15381162)

In Julia

julia> using LiteHF, Optim

julia> RR = build_pyhf(load_pyhfjson("./test/pyhfjson/sample_normsys.json"));

julia> original_LL = pyhf_logjointof(RR.expected, RR.observed, RR.priors);

julia> function condLL(rest)
           original_LL(vcat(1.0, rest)) #q0 implies Asimov_mu == 1
       end

julia> Asimov_para = vcat(1.0, Optim.maximizer(LiteHF.opt_maximize(condLL, RR.inits[2:end])))
2-element Vector{Float64}: # agrees with pyhf
 1.0
 0.15529785156250003

julia> Asimov_data = RR.expected(Asimov_para) # agrees with pyhf
([33.247581721021554, 22.790135089980318, 13.374274516306468, 9.16634422946954],)

julia> LiteHF.get_q0(original_LL, RR.inits)(0.0)
6.574804029034912 #agrees with pyhf

julia> Asimov_L = pyhf_logjointof(RR.expected, Asimov_data, RR.priors);

julia> LiteHF.get_q0(Asimov_L, RR.inits)(0.0)
4.352907587323511 #wtf?

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.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Add CITATION.cff

Hi. Can you please a CITATION.cff file with information on how you want LiteHF cited? This would be quite useful for properly referencing and citing the code.

[FR] shape-only fits

apparently there's a small set of EFT / SUSY model where the internal is conserved by physics and only the shape is different.

and somehow this may require special likelihood terms to capture

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.