-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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 SO(N) rotation matrix generator #5622
Conversation
""" | ||
size = int(size) | ||
if size > 1: | ||
return np.array([ self.rvs(dim, size=1, random_state=random_state) for i in range(size) ]) |
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 use 4-space indents.
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.
Yes, I noticed that after submitting it. Easily enough fixed.
On 23 December 2015 at 18:35, Robert Kern notifications@github.com wrote:
In scipy/stats/_multivariate.py
#5622 (comment):
Random size N-dimensional matrices, dimension (size, dim, dim)
Notes
Return a random rotation matrix, drawn from the Haar distribution
(the only uniform distribution on SO(n)).
The algorithm is described in the paper
Stewart, G.W., "The efficient generation of random orthogonal
matrices with an application to condition estimators", SIAM Journal
on Numerical Analysis, 17(3), pp. 403-409, 1980.
For more information see
http://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization
"""
size = int(size)
if size > 1:
return np.array([ self.rvs(dim, size=1, random_state=random_state) for i in range(size) ])
We use 4-space indents.
—
Reply to this email directly or view it on GitHub
https://github.com/scipy/scipy/pull/5622/files#r48387247.
linking to the related mailing list thread: https://mail.scipy.org/pipermail/scipy-dev/2015-October/021021.html This is not such a constructive thought, but I think it would be cool if scipy bundled something like MatrixDepot as its source of test matrices. MatrixDepot has several test matrix tags, one of which is 'random' which looks like it already includes random correlation matrices: |
@@ -2642,3 +2644,104 @@ def entropy(self): | |||
method_frozen.__doc__ = doccer.docformat( | |||
method.__doc__, wishart_docdict_noparams) | |||
method.__doc__ = doccer.docformat(method.__doc__, wishart_docdict_params) | |||
|
|||
class rotation_gen(multi_rv_generic): |
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.
rotation
is a much too general name, can you propose something more specific?
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.
How about special_ortho? And ortho for O(N).
Sent from my BlackBerry device on the Rogers Wireless Network
-----Original Message-----
From: Ralf Gommers notifications@github.com
Date: Wed, 23 Dec 2015 23:21:15
To: scipy/scipyscipy@noreply.github.com
Reply-To: scipy/scipy reply@reply.github.com
Cc: philipdeboerphilip.deboer@gmail.com
Subject: Re: [scipy] Add SO(N) rotation matrix generator (#5622)
@@ -2642,3 +2644,104 @@ def entropy(self):
method_frozen.doc = doccer.docformat(
method.doc, wishart_docdict_noparams)
method.doc = doccer.docformat(method.doc, wishart_docdict_params)
+
+class rotation_gen(multi_rv_generic):
rotation
is a much too general name, can you propose something more specific?
Reply to this email directly or view it on GitHub:
https://github.com/scipy/scipy/pull/5622/files#r48399833
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.
special_ortho
sounds fine to me. ortho
is fairly generic as well, but I don't have a better suggestion.
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.
Bikeshed alert: special_ortho_group
and ortho_group
? Or if brevity is at all a virtue: so_n_group
and o_n_group
?
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.
+1 I like those, especially the long versions
Aim for decent test coverage with tests that in well under one second. If really needed you can also add a slower test (say a few seconds), that is marked with |
Your proposed tests make sense - they demonstrate the functionality. In addition I would suggest to test against one hardcoded value, so the reproducibility of the random numbers for a given seed is checked. |
5fdd446
to
2052654
Compare
dim : scalar | ||
Dimension of matrices | ||
|
||
TODO: frozen object not implemented yet. |
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.
leftover?
BTW, I'm not sure if a frozen version is that useful. If you've implemented it already, fine.
As an idea for an additional test. The plane rotations, SO(2), should be essentially univariate. So one can do a KS test for the rotation angle (cf https://github.com/scipy/scipy/blob/master/scipy/stats/tests/test_continuous_basic.py#L352 ). Re remaining test failures: pep8 bot is annoying but easy to appease. Refguide-check failure asks you to add an entry to the list at https://github.com/scipy/scipy/blob/master/scipy/stats/__init__.py#L116 so that this is included into the html and pdf docs. Finally, there are syntax errors on python 2.6. We are likely to drop support for python 2.6 in a foreseeable future. Still, it'd be nice to fix the syntax if that's not too taxing. |
If all tests pass, I will move on to ortho_group. Should that be a separate PR, or shall I tack it on here? |
A separate one would be better IMO. When all tests pass, it'd be good to squash the commits and reformat the commit message to start with ENH prefix. If you're not sure how to |
@@ master #5622 diff @@
======================================
Files 234 234
Stmts 43140 43176 +36
Branches 8156 8159 +3
Methods 0 0
======================================
+ Hit 33456 33496 +40
+ Partial 2604 2602 -2
+ Missed 7080 7078 -2
|
I have been using the in-place build and just running Is there a way to run these other tests like that? |
Which essentially does an in-place build and runs tests in the specified files. |
I had done I will try the full in-place build, but it will take a few hours I think. That turns into days since I only run my laptop for 40min twice a day on the train. |
I stand corrected; it is done already. |
I use |
1e3fe1a
to
d7266a2
Compare
|
""" | ||
|
||
if dim is None or not np.isscalar(dim) or dim <= 1: | ||
raise ValueError("Dimension of rotation must be specified, and must be a scalar greater than 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.
Please split this line.
Yes, it'd be good to rebase on master (the conflict is most likely just in THANKS.txt). While you're at it, would you please split a those too long lines so that they are under 80 characters. This could also use a line in the release notes for scipy 0.18. If it were my PR, I'd branch the |
d7266a2
to
44d4092
Compare
I'm pretty sure the |
Add SO(N) rotation matrix generator
Agreed it'd be useful. Sounds like a cool project (GSoC even maybe?). |
Hi @philipdeboer, nice work—I just noticed your work in the release notes. A quick question: QR decomposition uses Householder reflections, and I believe you could use QR out of the box, did you try? Check out this implementation I've played around with some time ago. cf also tests. |
I did not try it myself, but Mezzadri mentions it in his paper. The problem It would be fun to reproduce his results and see that this implementation On 26 July 2016 at 13:50, Anton Akhmerov notifications@github.com wrote:
|
Sure it's not uniquely defined, but you can deal with that by post-processing the result (essentially dividing out by some phases). |
Generally when a numerical algorithm loses resolution, it cannot be On 27 July 2016 at 02:27, Anton Akhmerov notifications@github.com wrote:
|
Actually Mezzadri does exactly this for U(N). I will have to take another from scipy import * On 27 July 2016 at 02:27, Anton Akhmerov notifications@github.com wrote:
|
Feel free to adapt my version if you like it (it also has other symmetric spaces). |
Just happened to be taking another look at this today. One thing I noticed
is that the existing implementation uses one random number for each degree
of freedom [ n*(n-1)/2 ], while the qr approach would, I think, require a
starting matrix with n**2 random numbers.
…On 27 July 2016 at 07:44, Anton Akhmerov ***@***.***> wrote:
Feel free to adapt my version if you like it (it also has other symmetric
spaces).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5622 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AJ_rHrtceAzWeuiyNFF1wKjA-omTtunFks5qZ0SbgaJpZM4G61Kf>
.
|
One could imagine generating a random triangular matrix and symmetrizing it well, but since the whole algorithm scales as N^3 regardless of implementation details, I always considered that part to be unimportant. |
I am looking for feedback on this code. It generates random rotations, ie elements of SO(N). I plan to add O(N) as well, but I'd like to be sure of the format first as the two algorithms are very similar.
I have only implemented rvs. The N is specified by dim. You can ask for multiple samples using size, and I return those in a list. Is that acceptable? Mathematically the natural way to embed n results would be as diagonal matrices in an n*N space. Would that be preferable to a list? One argument against it is that it is easier to construct the block-diagonal matrix than to decompose it.
Is it important to have a frozen version as well?
I have not submitted any tests. What are some good ones? A good test that this is generating the samples from the Haar distribution is to
a. generate n samples from SO(N)
b. compute the eigenvalues (N*n of them) and describe their distribution
c. then rotate all n samples by another element of SO(N)
d. update the eigenvalue distribution
e. show that the two eigenvalue distributions are similar
You could repeat step c, but I would find a single sample convincing.
Would we fix n and N? N = 5, n = 1000? Are there timing benchmarks to keep beneath?
Additional tests:
2. Show determinant == 1 for a single sample in each N in range(10)
3. Show x x.T == eye(N) for each sample in test 2