[ANN] Announcing AlgebraPDF.jl - operations on functions with parameters

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.

14 Likes

Congratulations. Very exciting. :grinning: Could you say a little more about how it relates to other existing packages. For instance, what can you do with AlgebraPDF that you couldn’t do with the other packages that provide PDFs.

1 Like

hi, thanks.

Sure:

  • quick (one line) construction of customary pdf (I am not aware of any package offering this)
  • easy management of parameters fix/release (I am not aware of any package offering this, generally)
  • fractions of components in the mixture model can be fitted (Distributions.jl cannot. Likely GaussianMixtures.jl can do it, but only with Gaussian functions(?))
  • extended log-likelihood fit (must be implemented somewhere, idk)
  • interface to Minuit (there is IMinuit.jl, not yet registered)
  • arithmetic operations of functions (to be honest, I am not aware of other implementation).

It is a good idea to collect references to related packages. So feel free to drop notes on overlapping functionality.

5 Likes

See also

https://github.com/cscherrer/MeasureTheory.jl

2 Likes

Just a side note from an ignorant guy not focused on stochastic/statistics: Your package title made me wonder what kind of algebraic approach to PDF files you are proposing… bad coincidence of abbreviations

4 Likes

Hm, thanks. Indeed, I did not think about this “PDF”.

1 Like

thanks for pointing the MeasureTheory.jl. It looks great and seems to have similar goals as AlgebraPDF.jl according to what I quickly read. I would really like to learn more about what it can do.

@cscherrer could you please comment on the example and the list above?

1 Like

Thanks @jzr for pointing out MeasureTheory, and thanks @misha_mikhasenko for the ping.

This is a nice looking package! I need to dig in some more, but think the overlap with MeasureTheory might be fairly small.

MeasureTheory gets its parameterization capabilities from KeywordCalls.jl. Parameterized measures end up looking like

struct Normal{N,T} <: ParameterizedMeasure{N}
    par :: NamedTuple{N,T}
end

So we can then dispatch on Normal{N}.

It seems like AlgebraPDF might be more about having a single parameterization and working with it in a very flexible way. I’d be interested to explore, for example, using this to make it very easy to add a new parameterization for an existing measure.

Shameless self-promotion: Here’s my upcoming JuliaCon talk on MeasureTheory.

2 Likes

thanks for the reply.
I see.
I keep the type of parameters also parametric to be able to “extend” it with flag value if I need to mark some parameters fixed.

standardgauss(x,σ) = exp(-x^2/(2*σ^2)) / sqrt(2π) / σ
# 
struct FGauss{P} <: AbstractFunctionWithParameters
    p::P
end
function func(d::FGauss, x::NumberOrTuple; p=pars(d))
    μ,σ = (getproperty(p,s) for s in keys(d.p))
    standardgauss(x-μ,σ)
end

thanks for the link to the talk. I will definitely try to tune in.
I have a short talk on JuliaCon on Wednesday.

2 Likes

Is there potential for integration/compatibility with Functors.jl?

Hm, Functors.jl looks very relevant, thanks for pointing. What do you think can be a use case?

AFAIK, the main use case of Functors.jl is making parameters trainable/optimizable. So it does provide similar functionality to AlgebraPDF.jl in regard to keeping track of parameters. Maybe AlgebraPDF.jl could be based on Functors.jl? I haven’t looked deep enough - but if so, it could make AlgebraPDF.jl functions compatible with the Julia ML ecosystem (Flux.jl & friends are designed to trains things that are compatible with Functors.jl).

As far as I see, Functors flatten complex structures. In AlgebraPDF, you can use any parameter structure as you like (e.g. NamedTupleShape :wink:), but the call

f(x::Number, p::AbstractArray)

will only work if parameters are scalars. That is something to improve. Maybe functors can help with it somehow.

1 Like