-
Notifications
You must be signed in to change notification settings - Fork 29k
SPARK-5017 [MLlib] - Use SVD to compute determinant and inverse of covariance matrix #3871
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fd9784c
SPARK-5017 - Use SVD to compute determinant and inverse of covariance…
tgaloppo 6f11b6d
SPARK-5017 - Use SVD to compute determinant and inverse of covariance…
tgaloppo b4415ea
Merge branch 'spark-5017' of https://github.com/tgaloppo/spark into s…
tgaloppo 1989be0
SPARK-5017 - Fixed to use SVD to compute determinant and inverse of c…
tgaloppo d448137
Added test suite for MultivariateGaussian, including test for degener…
tgaloppo dc3d0f7
Catch potential exception calculating pseudo-determinant.
tgaloppo 629d9d0
Moved some test values from var to val.
tgaloppo a5b8bc5
Added additional points to tests in test suite.
tgaloppo 383b5b3
MultivariateGaussian - minor optimization in density calculation
tgaloppo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
mllib/src/test/scala/org/apache/spark/mllib/stat/impl/MultivariateGaussianSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.mllib.stat.impl | ||
|
|
||
| import org.scalatest.FunSuite | ||
|
|
||
| import breeze.linalg.{ DenseVector => BDV, DenseMatrix => BDM } | ||
|
|
||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
| import org.apache.spark.mllib.util.TestingUtils._ | ||
|
|
||
| class MultivariateGaussianSuite extends FunSuite with MLlibTestSparkContext { | ||
| test("univariate") { | ||
| val x1 = new BDV(Array(0.0)) | ||
| val x2 = new BDV(Array(1.5)) | ||
|
|
||
| val mu = new BDV(Array(0.0)) | ||
| val sigma1 = new BDM(1, 1, Array(1.0)) | ||
| val dist1 = new MultivariateGaussian(mu, sigma1) | ||
| assert(dist1.pdf(x1) ~== 0.39894 absTol 1E-5) | ||
| assert(dist1.pdf(x2) ~== 0.12952 absTol 1E-5) | ||
|
|
||
| val sigma2 = new BDM(1, 1, Array(4.0)) | ||
| val dist2 = new MultivariateGaussian(mu, sigma2) | ||
| assert(dist2.pdf(x1) ~== 0.19947 absTol 1E-5) | ||
| assert(dist2.pdf(x2) ~== 0.15057 absTol 1E-5) | ||
| } | ||
|
|
||
| test("multivariate") { | ||
| val x1 = new BDV(Array(0.0, 0.0)) | ||
| val x2 = new BDV(Array(1.0, 1.0)) | ||
|
|
||
| val mu = new BDV(Array(0.0, 0.0)) | ||
| val sigma1 = new BDM(2, 2, Array(1.0, 0.0, 0.0, 1.0)) | ||
| val dist1 = new MultivariateGaussian(mu, sigma1) | ||
| assert(dist1.pdf(x1) ~== 0.15915 absTol 1E-5) | ||
| assert(dist1.pdf(x2) ~== 0.05855 absTol 1E-5) | ||
|
|
||
| val sigma2 = new BDM(2, 2, Array(4.0, -1.0, -1.0, 2.0)) | ||
| val dist2 = new MultivariateGaussian(mu, sigma2) | ||
| assert(dist2.pdf(x1) ~== 0.060155 absTol 1E-5) | ||
| assert(dist2.pdf(x2) ~== 0.033971 absTol 1E-5) | ||
| } | ||
|
|
||
| test("multivariate degenerate") { | ||
| val x1 = new BDV(Array(0.0, 0.0)) | ||
| val x2 = new BDV(Array(1.0, 1.0)) | ||
|
|
||
| val mu = new BDV(Array(0.0, 0.0)) | ||
| val sigma = new BDM(2, 2, Array(1.0, 1.0, 1.0, 1.0)) | ||
| val dist = new MultivariateGaussian(mu, sigma) | ||
| assert(dist.pdf(x1) ~== 0.11254 absTol 1E-5) | ||
| assert(dist.pdf(x2) ~== 0.068259 absTol 1E-5) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
U D^-1 * U.t => U D^(-1/2) D^(-1/2) U.t => (U D^(-1/2)) (D^(-1/2) U.t)
...both are U and D are symmetric, so...
(U D^(-1/2)) = (U.t (D^(-1/2)).t) => (D^(-1/2) U).t
and
(D^(-1/2) U.t) = (D^(-1/2) U)
thus
U D^-1 U.t => (D^(-1/2) U).t (D^(-1/2) U)
... bringing in the delta we get
delta.t (D^(-1/2) U).t (D^(-1/2) U) delta
=> ((D^(-1/2) U) delta).t (D^(-1/2) U) delta = norm(D^(-1/2) U delta)^2
as indicated by @mengxr
(phew, hope I did that OK! :) )
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.
@jkbradley Hmm. I am going to punt on this one. Perhaps @mengxr can point out my error. FWIW - Changing the code to U * D^(-1/2) causes the unit tests to fail.
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 agree; I see no reason why U should always be symmetric (as you have demonstrated). Before I roll back the change, I just want to make sure that I did not misinterpret @mengxr , and/or that we are not missing something that he sees.
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.
(u, d) is the eigendecomposition of sigma, so sigma = u * diag(d) * u^-1 ... but we have a special case since covariance matrices are always symmetric and positive semi-definite, in which case u * u.t = I, making it equivalent to the singular value decomposition... so sigma = u * diag(d) * u.t ... so in svd terms, v.t = u.t, then the inverse is v * inv(diag(d)) * u.t = u * inv(diag(d)) * u.t ...
Have I lost my bearings?
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.
Ugh, no, I have. I was confused about which were inverses, and what you wrote looks perfectly fine. Sorry for the trouble! I'll remove the comments.d