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

**URL:** <https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213>\
**Category:** Package Announcements\
**Tags:** package, announcement, statistics, hep\
**Created:** [July 23, 2021, 9:51pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213 "2021-07-23T21:51:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 23, 2021, 9:51pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/1 "2021-07-23T21:51:50Z")

</div>

Hi,

I am happy to announce [AlgebraPDF.jl](https://github.com/mmikhasenko/AlgebraPDF.jl), a package that defines operations on parametric functions and keeps track of parameters.

```julia
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)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/1/e14933ace27a262b496a9b2ab10926c211dabe47.png)

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:

```julia
# 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).

```julia
# 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")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/3/0374d0bf930825ef6d8dec456df079fc44e4a18f.png)

Now, the function can be fit the data using maximum likelihood method,  
or extended maximum likelihood if the function is not normalized by construction.

```julia
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")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/f/3f7a166884872b65f221920115f4a89edd46c247.png)

Parameters can be fixed for the purpose of the fitting, and many more. See documentation at [package repository](https://github.com/mmikhasenko/AlgebraPDF.jl)

AlgebraPDF.jl just entered the 3 days waiting period for the registration.  
Currently can be installed with

```julia
] add https://github.com/mmikhasenko/AlgebraPDF.jl

```

Looking forward to your feedback.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [July 23, 2021, 10:26pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/2 "2021-07-23T22:26:41Z")

</div>

Congratulations. Very exciting. 😀 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.

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 23, 2021, 10:41pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/3 "2021-07-23T22:41:14Z")

</div>

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](https://juliastats.org/Distributions.jl/latest/mixture/#Estimation). 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.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 24, 2021, 12:56am UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/4 "2021-07-24T00:56:46Z")

</div>

See also

[https://github.com/cscherrer/MeasureTheory.jl](https://github.com/cscherrer/MeasureTheory.jl)

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [July 24, 2021, 5:27am UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/5 "2021-07-24T05:27:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 24, 2021, 8:08am UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/6 "2021-07-24T08:08:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 24, 2021, 8:14am UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/7 "2021-07-24T08:14:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 24, 2021, 1:10pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/8 "2021-07-24T13:10:02Z")

</div>

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](https://github.com/cscherrer/KeywordCalls.jl). Parameterized measures end up looking like

```julia
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](https://pretalx.com/juliacon2021/talk/U7AM33/).

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 24, 2021, 5:53pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/9 "2021-07-24T17:53:25Z")

</div>

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.

```julia
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](https://pretalx.com/juliacon2021/talk/TRMZFB/).

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [July 25, 2021, 12:58pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/10 "2021-07-25T12:58:30Z")

</div>

Is there potential for integration/compatibility with [Functors.jl](https://github.com/FluxML/Functors.jl)?

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 25, 2021, 1:06pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/11 "2021-07-25T13:06:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [July 25, 2021, 3:34pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/12 "2021-07-25T15:34:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [July 25, 2021, 8:53pm UTC](https://discourse.julialang.org/t/ann-announcing-algebrapdf-jl-operations-on-functions-with-parameters/65213/13 "2021-07-25T20:53:44Z")

</div>

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

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

```

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