GitHub user hl475 opened a pull request:

    https://github.com/apache/spark/pull/16556

    [SPARK-19184][MLlib] Improve numerical stability for method tallSkinnyQR.

    ## What changes were proposed in this pull request?
    
    In method tallSkinnyQR, the final Q is calculated by A * inv(R). When the 
upper triangular matrix R is ill-conditioned, computing the inverse of R can 
result in catastrophic cancellation. Instead, we should consider using a 
forward solve for solving Q such that Q * R = A.
    ## How was this patch tested?
    
    unit tests and the following codes in Spark Shell:
    ```
    import org.apache.spark.mllib.linalg.{Matrices, Vector, Vectors}
    import org.apache.spark.mllib.linalg.distributed.RowMatrix
    
    // Create RowMatrix A.
    val mat = Seq(Vectors.dense(1,1,1,1), Vectors.dense(0, 1E-5, 1,1), 
Vectors.dense(0,0,1E-10,1), Vectors.dense(0,0,0,1E-14))
    val denseMat = new RowMatrix(sc.parallelize(mat, 2))
    
    // Apply tallSkinnyQR to A.
    val result = denseMat.tallSkinnyQR(true)
    
    // Print the calculated Q and R.
    result.Q.rows.collect.foreach(println)
    result.R
    
    // Calculate Q*R. Ideally, this should be close to A.
    val reconstruct = result.Q.multiply(result.R)
    reconstruct.rows.collect.foreach(println)
    
    // Calculate Q'*Q. Ideally, this should be close to the identity matrix.
    result.Q.computeGramianMatrix()
    
    System.exit(0)
    ```
    I first create a 4 by 4 RowMatrix A = 
(1,1,1,1;0,1E-5,0,0;0,0,1E-10,1;0,0,0,1E-14), and then I apply method 
tallSkinnyQR to A to find RowMatrix Q and Matrix R such that A = Q*R. In this 
case, A is ill-conditioned and so is R.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/hl475/spark tallSkinnyQR

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/16556.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #16556
    
----
commit db737275d563b6057a5b4a2f116badc1f362f2f0
Author: Huamin Li <huami...@huamins-mbp.wireless.yale.internal>
Date:   2017-01-12T02:13:07Z

    modify tallSkinnyQR so it will use forward solve instead of computing the 
inverse of upper triangular matrix R.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to