Hi,
I am happy to announce AlgebraPDF.jl, a package that defines operations on parametric functions and keeps track of parameters.
f1 = FunctionWithParameters((x;p)->cos(p.a*x+p.b), (a=5.0, b=2.0))
f2 = FunctionWithParameters((x;p)->p.c - x^2, (c=4,))
f3 = f1*f2
pars(f3) # (a = 5.0, b = 2.0, c = 4)
#
using Plots
theme(:wong2, frame=:box, lab="", grid=false)
plot(f3, -3,2)
plot!(updatepar(f3,:a,15), -3,2)
The package is mostly for quick model construction from customary functions that are usually not in the standard packages, however, still, some common functions are predefined for convenience:
# there are several predefined functions
f4 = FGauss((μ=-1.5,σ=1.1))
# several common operations abs, log, addition
f5 = abs2(f1) * (α1=0.5,) + f4 * (α4=10.2,)
pars(f5) # returns a tuple of all parameters
# (a = 5.0, b = 2.0, μ = -1.5, σ = 1.1, α1 = 0.5, α4 = 10.2)
One can make a pdf out of the function by just passing to Normalized
constructor together with the limits (support).
# pdf out of the function with dynamic normalization (p)
pdf5 = Normalized(f5,(-3,2))
# can generate events
Nev = 1000
data = rand(pdf5, Nev)
bins=range(lims(pdf5)...,length=100)
#
stephist(data;bins, lab="Data")
plot!(pdf5, Nev*scaletobinneddata(bins), lw=2, lab="Model")
Now, the function can be fit the data using maximum likelihood method,
or extended maximum likelihood if the function is not normalized by construction.
fit_summary = fit(pdf5, data)
fit_summary.measurements # gives a tuple with
# a = 5.041 ± 0.028
# b = 2.012 ± 0.044
# μ = -1.487 ± 0.018
# σ = 1.105 ± 0.017
# αs...
plot!(fit_summary.best_model, Nev*scaletobinneddata(bins), lw=2, lab="Fit")
Parameters can be fixed for the purpose of the fitting, and many more. See documentation at package repository
AlgebraPDF.jl just entered the 3 days waiting period for the registration.
Currently can be installed with
] add https://github.com/mmikhasenko/AlgebraPDF.jl
Looking forward to your feedback.