| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Thanks for this! Can you remove the changes to the normal and double_exponential if they're unrelated? Feel free to open a separate PR for them afterwards though! |
Sorry, something went wrong.
Jenkins Console Log Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: G++: Clang: |
Sorry, something went wrong.
|
I discarded any changes to the existing distributions |
Sorry, something went wrong.
Jenkins Console Log Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: G++: Clang: |
Sorry, something went wrong.
|
Sorry for a broken PR, I believed the tests pass, but apparently I made some mistake when running the tests locally. Would appreciate any help deciphering the error messages, it doesn't seem very understandable what the problem is exactly to me. But apparently it somehow has to do with the distribution being possibly non-differentiable at mu=y. The suspect line is: sign(diff) * beta_val * pow(scaled_abs_diff, beta_val - 1) * inv_alpha In case when mu=y, we get: sign(0) * beta_val * pow(0, beta_val - 1) * inv_alpha which I think should evaluate to 0, and also when beta=1 sign(0) * 1 * pow(0, 0) * inv_alpha should also evaluate to 0 I would expect? Is pow(0,0) actually undefined in STAN math? And what would be the recommended technique for special-case handling? |
Sorry, something went wrong.
No worries! It's often quite a miracle when these things pass first try. You can run the exact test that is given issues locally by running python runTests.py -j4 test/prob/generalized_normal/generalized_normal_0*_test.cpp
It's defined (#3031), but it's not twice differentiable: #2993 (comment), which I believe is the problem here. This second comment is by @andrjohns who can probably also give you suggestions about what to do in this case. It's possible that what you're doing in the code is fine and you just need to have the test call it somewhere else where it will be better behaved |
Sorry, something went wrong.
|
OK, so it may be just a testing problem. But this relates to something else: The case when beta=1 where the tests are apparently breaking is maybe just a tip of the iceberg: when beta<1, I write in the code comments the partials are undefined when y=mu, because we then get pow(0, negative_number). This is because the limit can diverge to both positive and negative infinity depending on whether we approach from y>mu or y<mu. I left this undefined intentionally, but is that actually an OK behavior for STAN? Or do we need to use some technique to e.g. artificially set the partial to 0 in theses cases (like sign(0)=0 is defined to be the mathematically non-existent derivative of abs(0)) |
Sorry, something went wrong.
There was a problem hiding this comment.
Just some starting Q comments.
I'm rather unfamiliar with this distribution so will need to do some reading before I can comment on any of the math here
Sorry, something went wrong.
|
Some notes on the distribution (I made some mistakes in the #3133 comments but here should be corrected): We have the density: $f(y|\mu,\alpha,\beta) \propto \exp\left(-\left(\frac{|y-\mu|}{\alpha}\right)^{\beta}\right)$ with normalization constant $\frac{1}{2\alpha\Gamma(1 + \frac{1}{\beta})} = \int_{-\infty}^{\infty} \exp\left(-\left(\frac{|y-\mu|}{\alpha}\right)^{\beta}\right) \mathrm{d}y$ (note: can set $\mu = 0$ WLOG to evaluate the integral) The parameters $\alpha, \beta$ must be positive. The log-likelihood is then: $\log f(y|\mu,\alpha,\beta) = -\left(\frac{|y-\mu|}{\alpha}\right)^{\beta} - \log(2) -\log(\alpha) - \log\Gamma(1 + \frac{1}{\beta})$ Wolfram Mathematica outputs the following partials: $\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial \mu} = \alpha^{-\beta}\beta|y-\mu|^{\beta-1} \mathrm{sign}\left(y-\mu\right)$ $\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial y} = -\alpha^{-\beta}\beta|y-\mu|^{\beta-1} \mathrm{sign}\left(y-\mu\right) = -\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial \mu}$ Using algebraic manipulations I change this to the form: $\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial \mu} = \alpha^{-1}\beta \left(\frac{|y-\mu|}{\alpha}\right)^{\beta-1} \mathrm{sign}\left(y-\mu\right)$ This is where I believe the test errors are comming from when testing the case $\beta = 1, y = 0, \mu=0$. The lpdf is defined, the first derivative is by convention 0 (since pow(0,0)=1 and sign(0)=0), but the second derivative undefined. And I am not even testing the case when $\beta < 1$ which I kept undefined for $y = \mu$ as the there is negative power of zero. The $\beta$ partial is: $\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial \beta} = \frac{\mathrm{digamma}(1 + \frac{1}{\beta})}{\beta^2} - \left(\frac{|y-\mu|}{\alpha}\right)^{\beta} \log(\frac{|y-\mu|}{\alpha})$ this one has a also potential boundary problem when $y=\mu$ because we get $\log(0)$ but then the limit goes to 0 and this should be solved by using multiply_log. Not sure about second derivatives tho, do they need to be considered here? The $\alpha$ partial I think is not problematic but just for completeness, I also used wolfram plus some algebra to obtain: $\frac{\partial \log f(y|\mu,\alpha,\beta)}{\partial \alpha} = \alpha^{-1}(\beta \left(\frac{|y-\mu|}{\alpha}\right)^{\beta} - 1 )$ I tend to believe that @WardBrian is right that this is perhaps a problem with testing the distribution at these boundaries, but idk what a good approach to this is. PDF plot from wikipedia for $\mu = 0, \alpha = 1$ illustrates the non-differentiability at $y=0$ for $\beta \le 1$: |
Sorry, something went wrong.
|
Incorporated changes requested by @SteveBronder although that doesn't solve the test errors. I think those are more like just performance+stylistic improvements. See the notes and discussion above. |
Sorry, something went wrong.
Jenkins Console Log Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: G++: Clang: |
Sorry, something went wrong.
|
Hi, @andrjohns! Could you please go through the above comments and provide me with any feedback on how to properly test this or resolve some other way? |
Sorry, something went wrong.
|
pushed some changes which should fix the failing tests. |
Sorry, something went wrong.
Jenkins Console Log Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: G++: Clang: |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
This is my take on the request #3133 I created a while ago.
The Generalized normal distribution a.k.a. exponential power distribution generalizes the normal, Laplace (double exponential) and in limit also uniform distribution. This makes it an interesting prior choice as it generalizes the corresponding MAP regularization coefficient to L_p norm and can also be interesting to model noise in regression problems for basically the same reasons.
This is my first attempt at implementing a distribution to STAN, I tried to follow the guide https://mc-stan.org/math/md_doxygen_2contributor__help__pages_2adding__new__distributions.html but there are likely some problems, so I welcome any comments.
So far I implemented only the lpdf, if other files like lcdf etc are strictly required, please let me know.
Tests
I added one test file analogical to the one for normal distribution.
Side Effects
I included some very minor code polishing to the related normal and double exponential distributions (I used those as a starting point) which shouldn't do anything and can be discarded if undesired.
Release notes
Added generalized_normal_lpdf
Checklist
Copyright holder: Me
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested