# Custom Likelihood - sum of weighted exponentials with transformed data

**URL:** <https://discourse.pymc.io/t/custom-likelihood-sum-of-weighted-exponentials-with-transformed-data/14063>\
**Category:** version agnostic\
**Tags:** modeling\
**Created:** [March 20, 2024, 4:34pm UTC](https://discourse.pymc.io/t/custom-likelihood-sum-of-weighted-exponentials-with-transformed-data/14063 "2024-03-20T16:34:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Umair](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/muhammad_umair/32/7893_2.png) [@Muhammad\_Umair](https://discourse.pymc.io/u/Muhammad_Umair)\
**Post date:** [March 20, 2024, 4:34pm UTC](https://discourse.pymc.io/t/custom-likelihood-sum-of-weighted-exponentials-with-transformed-data/14063/1 "2024-03-20T16:34:50Z")

</div>

Hi,

I am working on a research problem where I have a random variable R that can take values in range R+. I have a sequence of (already known) values w1,w2,…wT. The size of this sequence may increase over time i.e., we may obtain w\_{T+1}. The likelihood of a single observation of the random variable R is given as:

P(r | w1, … wT, lambda) = \sum{t=1}\_{T} [phi(w1,…wt) \* lambda \* e^{-lambda (r - t)}]

The above likelihood consists of two parts. The first phi(w1,…wt) is a function that takes in the sequence up to the current item t and returns a scalar positive value. The second is an exponential distribution that takes produces f(r-t | lambda), where r -t is the data minus the current t value. For simplicity, we assume that the lambda parameter is shared across the exponentials. In the future, we may want this to be distinct for each exponential.

Is there a way to implement this above function as a custom likelihood in pymc such that the posterior distribution of lambda may be generated? The goal is to plot the P(r|w1,…wT, lambda) for all T values.

Any help is greatly appreciated.

Thank you!

---

<div class="post-metadata">

**Author:** ![iavicenna](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/iavicenna/32/7923_2.png) [@iavicenna](https://discourse.pymc.io/u/iavicenna)\
**Post date:** [March 21, 2024, 1:00pm UTC](https://discourse.pymc.io/t/custom-likelihood-sum-of-weighted-exponentials-with-transformed-data/14063/2 "2024-03-21T13:00:28Z")

</div>

Is it possible to model \phi(w\_1,w\_2,...,w\_t) for each t with a hyperprior of some sorts that depends on priors of w\_1,…,w\_n and sums upto 1 (after multiplying with lambda)? If so then I assume this could be achieved via

[https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.Mixture.html](https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.Mixture.html)

If however not and you need to define your custom distribution, then there is the CustomDist class which you may try to use. An example of it can be found here (which uses the approach of defining the logp for the custom dist which might be what you need here):

> [@Modelling a distribution with a portion removed from it](https://discourse.pymc.io/t/modelling-a-distribution-with-a-portion-removed-from-it/13847):
>
> I am an archaeologist studying an ancient quarry comprised of a number of blocks. I have measurements of the size of these blocks. Generally these measurements should follow a LogNormal or Weibull distribution (i.e. positive, bigger blocks rarer etc.). I have reason to believe a portion of the observed distribution of blocks is missing, these are the blocks that were deemed useful or correct by past quarriers and thus removed from the quarry. I would like to infer the portion of missing blocks a…

as well as other examples here

[https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.CustomDist.html](https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.CustomDist.html)
