Matrix Inverse using Optimizer

Options
AnkitGupta
edited December 2022 in Best Practices

In the following article, we will discuss how to use Anaplan Optimizer to compute Matrix Inverse. Matrix Inverse as a functionality can be used to solve a Multi Linear Regression Problem in Anaplan. Using Matrix Inverse, we are going to solve the following Multi Linear Regression equation:

Y = b0 + b1x1 + b2x2 + b3x3 + b4x4 + b5x5 + b6x6 + b7x7 + b8x8

  • x1 to x8 are called Independent Variables.
  • Y is called a dependent variable whose value we are trying to find.
  • b0 to b8 are called coefficients for the constant and the independent variables (x1  to x8).

Y = X * B

X(T)*Y = (X(T)*X) * B

Taking X(T) * X to other side

(X(T) *X)-1 X(T)*Y = B

Multi Linear Regression can be used to find out the factors which are impacting your dependent variable (ex: Causal Forecasting).

You can use the following steps to calculate Matrix Inverse in Anaplan using optimizer and then use that to complete your Multi Linear Regression problem.

Theory used to solve Matrix inverse is M* M^-1 = I.

Matrix * Matrix inverse = Identity Matrix.

We know the values of Matrix and Identity Matrix, and we are going to use Optimizer to compute the variable which is our Matrix Inverse. This trick is going to help us compute MMULT.

And Optimizer will do the heavy lifting for us and compute Matrix Inverse for us.

The following are the steps:

Lists

Create four lists I9x9 (Rows), J9x9 (Columns), K9x9 (Dummy List), Item and Populate them as follows:

  • R1 to R9 as list members in I9x9  -> Make a number property to hold row number (R1 =1...R9=9)
  • C1 to C9 as list members in J9x9 -> Make a number property to hold column number (C1 =1...C9=9)
  • K1 to K9 as list members in K9x9 -> Make a number property to hold column number (K1 =1...K9=9)
  • Item-1 to Item-xxx as list members in Item list (These are number of items for which we want to calculate the matrix inverse)
  • Include Top Level for all the above lists

Module

Create a Module with the following line items:

-- Input --

Input Matrix -> This is Input Matrix for Matrix

Identity Matrix -> If row number=column number Then 1 else 0

-- Variables --
VAR 01: Matrix Inverse -> This is the Matrix Inverse Optimizer is solving for
VAR 02: MMULT: Matrix * Matrix Inverse -> Intermediate variable to calculate Matrix *Matrix Inverse
VAR 03: MMULT RxC -> Changing Dimensions of the MMULT result above from IxJxK to IxJ

-- Constraints --
CONST 01: CALC MMULT = Matrix * Matrix Inverse -> Calculating Matrix * Matrix Inverse
CONST 02: CONVERT MMULT RxC -> Changing dimension of MMULT result above from IxJxK to IxJ
CONST 03: MMULT <= Identity -> Constraining VAR 03: MMULT RxC<= Identity Matrix

-- Objective --
Objective -> Maximizing VAR 03: MMULT RxCso VAR 03: MMULT RxC = IDentity Matrix whenever a Matrix inverse exists (If Determinant of a Matrix is 0 then Matrix inverse does not exist!)

Formula and Dimensions

Use this to configure the formulas and dimensions for these line items. 

Line Item Formulas.jpg

 

 

 

 

 

 

 

 

 

Optimizer

Use this to configure Optimizer

Once you run the optimizer then you will be able to see the value of Matrix inverse in VAR 01: Matrix Inverse.

Optimizer Configuration.jpg

 

 

 

 

 

 

 

 

 

 

Please Note: While testing the solution, ensure that your Input Matrix has numbers put in such a way that its Determinant is not Zero (because a matrix with zero determinant will not have an Inverse)

Contributing authors: Vinay Varadaraj Mirajkar and Anne-Julie Balsamo.

Comments

  • Hello AnkitGupta,

    Very interesting approach to use the Optimiser! Could you explain further what you are doing after having the Inverse Matrix calculated? I mean how to technically proceed to finally solving the Multi Linear Regression Problem?

    Best regards,

    Maciej

  • Hi Anaplan Team,

    I have tried to replicate this process. Somehow it is not giving the matrix inverse. I am not sure where i am going wrong.

    Attachment SS


    Regards,
    Manjunath

  • hendersonmj
    edited January 3
    Options

     @ManjunathKN 
    In your solution, you've limited the values of Variable 1, 2, & 3 to positive numbers (your minima are 0). In the original post, @AnkitGupta has left the MIN inputs blank, allowing the solver to use negative values for variables.

  • @hendersonmj If I remove the zeros in Min Values, The optimizer is throwing an error.

  • hendersonmj
    edited January 3
    Options

    @ManjunathKN 
    This is working for me:
    Lists I9x9, J9x9, and K9x9 have nine items and a top level member.
    Each of these lists has a Property named "Serial" with a formula: CUMULATE(1,False,<list name>).
    The Item list has a dozen items and a Subset named "SS Items: Optimizer". I used a subset to allow the flexibility to easily change the number of matrices that I am inverting.
    The Identity Matrix uses a formula: IF 'I9x9'.Serial = 'J9x9'.Serial THEN 1 ELSE 0
    The Identity Matrix shows values of 1 on the diagonal (r1c1, r2c2, r3c3…) and 0 in all other cells.

    The constraint formulas are:
    'VAR 02: MMULT: Matrix * Matrix Inverse' - Input Matrix * 'VAR 01: Matrix Inverse' = 0
    'VAR 03: MMULT RxC' - 'VAR 02: MMULT: Matrix * Matrix Inverse' = 0
    'VAR 03: MMULT RxC' <= Identity Matrix

    It seems to get the right answer: The correct answer for this input matrix has the reciprocals (1/X) of the values from the input diagonals on the Inverted Matrix.

  • hendersonmj
    edited January 5
    Options

    @ManjunathKN
    Happy to help and I'm glad you brought this to my attention. I have taken this forward to use the matrix inverter as part of the process for performing multiple linear regression with matrices. So far, I have fully recreated in Anaplan the Excel method in this video. I'm hoping to push that forward to include time series forecasting with publicly available location-specific data like demographics and economic factors.