Singular Value Decomposition Tutorial

In this section, you learn how to do a singular value decomposition (SVD) with a Financial Model Builder workflow. This workflow uses one of the following packages:

These packages are provided by Financial Model Builder.

Template - SVD

Run the fpp-library_tutorial_SVD workflow configuration.

Fig. 65: fpp-library_tutorial_SVD workflow configuration.

Fig. 65: fpp-library_tutorial_SVD workflow configuration.

The result is displayed in fig. 66 and corresponds to the factorization of the matrix \(A\) so that:

\[\begin{equation} A = USV^* \end{equation}\]

with \(V^*\) as the conjugate transpose of \(V\)

Fig. 66: fpp-library_tutorial_SVD workflow output using LAPACK.

Fig. 66: fpp-library_tutorial_SVD workflow output using LAPACK.

SVD Files

SVD Input

The input file includes the matrix A, that is decomposed, and the selected method, LAPACK or APACHE:

{
  "documents": [
    {
      "A": [
        [
          0.705547511577606,
          0.533424019813538,
          0.579518616199493,
          0.289562463760376,
          0.301948010921478
        ],
        [
          0.774740099906921,
          0.014017641544342,
          0.76072359085083,
          0.814490020275116,
          0.709037899971008
        ],
        [
          4.53527569770813E-02,
          0.414032697677612,
          0.862619340419769,
          0.790480017662048,
          0.373536169528961
        ],
        [
          0.961953163146973,
          0.871445834636688,
          5.62368631362915E-02,
          0.949556648731232,
          0.364018678665161
        ],
        [
          0.524868428707123,
          0.767111659049988,
          5.35045266151428E-02,
          0.592458248138428,
          0.468700110912323
        ]
      ],
      "type": "LAPACK"
    }
  ]
}

The LAPACK package is more efficient on large matrices, taking full advantage of the parallel computing power provided by FPP.

SVD Workflow and Box

In the template workflow, the fpp-library_tutorial_SVD box is called with the input request and three outputs U, V and S:

Fig. 67: The fpp-library_tutorial_SVD workflow.

Fig. 67: The fpp-library_tutorial_SVD workflow.

The content of the fpp-library_tutorial_SVD box is listed below:

// input management
def request(input){
    // create a jackson Object mapper
    ObjectMapper MAPPER = new ObjectMapper();
    // read the input, put it in an ObjectNode node
    ObjectNode node = (ObjectNode) MAPPER.readTree(input);
    // extract the document part from node
    doc = node["documents"][0]
}

// get the package to use
def operation = doc.get("type").textValue()

// get matrix A
def heightA = doc.get("A").size()
def widthA = doc.get("A")[0].size()
final double[][] A = new double[heightA][widthA];
for (int i = 0; i < heightA; ++i)
    for (int j = 0; j < widthA; ++j)
        A[i][j] = doc.get("A")[i][j].doubleValue()

// main treatment
if (operation == "LAPACK"){
    // initialize a Lapack object
    final Lapack lapack = getLapack()
    // perform a Singular Value Decomposition of A
    final SVDecompositionResult result = lapack.svDecomposition(A);
    // output results
    output("U", result.u().toString())
    output("S", result.s().toString())
    output("V", result.v().toString())
}
else {  // use Apache Common Math
    def m = new Array2DRowRealMatrix(A)
    // perform a Singular Value Decomposition of A
    def svd = new SingularValueDecomposition(m)
    // output results
    output("U", svd.getU().getData().toString())
    output("S", svd.getSingularValues().toString())
    output("V", svd.getV().getData().toString())
}

The Jackson library is leveraged to manipulate the JSON files. To see the library version, go to Imported Packages and Dependencies.

SVD Results

The result contains the three outputs matrices \(U\), \(S\) and \(V\) from the LAPACK singular value decomposition.

Fig. 68: fpp-library_tutorial_SVD workflow output using LAPACK.

Fig. 68: fpp-library_tutorial_SVD workflow output using LAPACK.

Run the Workflow Valuation Plugin

To run the workflow
  1. Download the following Excel file: tutorial_workflowSVD.xlsm and open it.
  2. Click Decompose A.
  3. Click View Results.
Fig. 69: fpp-library_tutorial_SVD workflow in Excel.

Fig. 69: fpp-library_tutorial_SVD workflow in Excel.

You obtain the same results as in The SVD results section..

Fig. 70: fpp-library_tutorial_SVD workflow result in Excel.

Fig. 70: fpp-library_tutorial_SVD workflow result in Excel.

You can view the matrices \(U\), \(S\) and \(V\) separately.