-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add rand
for ProbabilitySimplex
#604
Conversation
Codecov Report
@@ Coverage Diff @@
## master #604 +/- ##
=======================================
Coverage 99.01% 99.01%
=======================================
Files 104 106 +2
Lines 10138 10187 +49
=======================================
+ Hits 10038 10087 +49
Misses 100 100
... and 3 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
sorry should have squashed all those first. had to turn off the random tangent test since I think pr is ok now? let me know |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your PR – contributions are always happily welcome :)
I have two small remarks – and never mind several commits – for a PR that is always good to see also a bit the development, we squash-merge PRs anyways.
The easiest way to handle this is setting |
Good point to keep in mind for #548 . |
src/manifolds/ProbabilitySimplex.jl
Outdated
_r = sort(vcat(rand(n - 1), [0.0, 1.0])) | ||
pX .= (circshift(_r, -1) .- _r)[1:n] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could make fewer allocations and avoid the O(n*log(n))
sorting by using the fact that Dirichlet(alpha=ones(n))
is the uniform distribution on the probability simplex, so the usual Gamma distribution (see https://en.wikipedia.org/wiki/Dirichlet_distribution#Random_variate_generation) approach for drawing from Dirichlet reduces to drawing from Exponential(1)
:
_r = sort(vcat(rand(n - 1), [0.0, 1.0])) | |
pX .= (circshift(_r, -1) .- _r)[1:n] | |
Random.randexp!(pX) | |
LinearAlgebra.normalize!(pX, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! That should also be reflected in the docs.
src/manifolds/ProbabilitySimplex.jl
Outdated
_r = randn(n) .* σ | ||
pX .= _r .- mean(_r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the allocation to r_
.
_r = randn(n) .* σ | |
pX .= _r .- mean(_r) | |
Random.randn!(pX) | |
pX .= (pX .- mean(pX)) .* σ |
src/manifolds/ProbabilitySimplex.jl
Outdated
_r = randn(n) .* σ | ||
pX .= _r .- mean(_r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the allocation to r_
.
_r = randn(n) .* σ | |
pX .= _r .- mean(_r) | |
Random.randn!(pX) | |
pX .= (pX .- mean(pX)) .* σ |
end | ||
return pX | ||
end | ||
function Random.rand!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know elsewhere in this package we write separate methods for with and without rng
, but this was a mistake. In general, we should implement sampling methods with rng
and then the one without rng
should just dispatch to this by passing Random.default_rng()
as the first argument. This is what Random itself does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not aware f that we should then slowly improve our code :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make a PR that improves this.
With your current tolerance factor of 1 you are still checking for machine precision zero, which might be a bit tough (that is why 1.6 failed, maybe raise the factor to |
From my side this nearly looks good now, just that the doc string could maybe include a math formula or a reference where to find more information (maybe Seth's wikipedia remark)? |
There's a reference here I haven't read: https://stackoverflow.com/a/67202070 |
Cool, maybe adding that as a footnote reference would be cool (at some point we should aim for some bib package in the docs, let's hope some package like that soon magically appears :D ) |
thank you for adding the reference. we should probably merge one of this PR or #605 first, then rebase the other? I think as-is they are slightly incompatible |
Oh I might have crashed formatting, sorry for that (editing here has its disadvantages). But I‘ll activate Docs rendering as well now :) There seems to be a space too much in the footnote (i.e. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My points are clarified. Thanks for the contribution and the good further ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor docstring fix. Also, this should get a patch version bump. Otherwise, LGTM.
I think I will merge this and bump version in #605 -- thank you for your contribution 🙂 |
But we already discussed, that this is w.r.t. the „wrong” metric, i.e. not our default one but the Euclidean (with respect to which the manifold is not complete). |
Yes, it's not uniform wrt. our metric but we never claimed to always use uniform distributions (for manifolds of finite volume). And we can change it later. |
Sure, I would still prefer if we could switch this one to be the one with the metric manifold / euclidean metric? |
Wait... that's actually a good question. I assumed |
that commit is just a rebase on master, diff shouldn't have changed |
We usually merge, that is easier (for PRs we squash-merge, which makes the master branch relatively clean again). I will check the change of metric soon. |
Hm, I am now a bit confused, since the documentation of exp for example follows https://arxiv.org/pdf/1603.05285.pdf, Proposition 2.4, but the implementation – at least as far as I understand it – does something completely different. The metric conversion seems wrong for now, I might have stayed too close to what is done on the positive numbers (element wise), will check for a correction. The change of representer seems right, though. edit: Ah no, the change representer is also wrong, but Proposition (2.3) provides the right formula, but then I can probably derive the change metric from that as well. - eh, oh! That formula includes projection which we do before, added that note to the docs, but change representer is correct. Will check how to adapt that though. |
HM, that's a tough one to solve, because it seems there are only n degrees of freedom (for the change metric) but I currently seem to have n+1 conditions. |
I have to dismiss my approval since there still seems to be work to be done
I've discussed with @kellertuer what is wrong with |
Yeah, thanks for the discussion it seems there might be a change metric :) |
|
* Add a few exports, mainly reexports from ManifoldsBase. * Improve caching. i.e. doing it correctly. * 📖🩹 at least a bit, * 📖🩹 2 * export FisherRaoMetric.
I had tried also to add some tests for the |
Hm, I think you can just take exp!(::MetricManifold{ℝ,<:ProbabilitySimplex,<:EuclideanMetric}, q, p, X) = (q .= p .+ X)
exp!(::MetricManifold{ℝ,<:ProbabilitySimplex,<:EuclideanMetric}, q, p, X, t::Number) = (q .= p .+ t .* X) and it should more or less work. |
g2g? |
Co-authored-by: Seth Axen <seth@sethaxen.com>
But then we should also register 0.8.71 soon, because I think we were just waiting for this PR. |
Yes, I've even forgotten we have a few changes on master without a new registered version. |
uniformly samples from probability simplex if no point given. otherwise returns a mean-zero vector of gaussians.