0

I am translating a code from MATLAB to Python and I have run into trouble with multiplication. I am writing some Bayesian econometric simulations and there is a lot of matrix multiplication. Some of the operations will yield a "scalar" at the end - a = [[6]], some, however, will yield a vector a = [[3],[2]]. Now, this number gets used later in another matrix (or vector) multiplication, but sometimes it would need to be taken as a scalar. However, it will get used as a matrix and throw an error since the dimensions will not match.

The problem is, I can not predict which expression will result in a scalar and which will remain a vector or a matrix. That depends on the input.

One of the equations looks something like this:

beta_1 = V_1 * (inv(V_0) * beta_0 + t(X) * X * b_OLS); 

And for some specifications, the b_OLS can be a vector, in others it can be a scalar. I might write it so that it works nicely for one script for the right constellation, but I need that function to be robust.

I have tried to create my own function for matrix multiplication that checks the input:

import numpy as np

def multiply(a, b):
    if type(a) is np.ndarray and type(b) is np.ndarray:
        if len(a.shape) == 2 and len(b.shape) == 2:    
            if a.shape != (1,1) and b.shape != (1,1):
                return np.dot(a,b)
            else:
                return np.multiply(a,b)
        else:
            print("Wrongly specified matrix or vector.")
    else:
        return np.multiply(a,b)

But this relies on the fact that EVERYTHING there is will be either int or numpy.ndarray which is a bit fragile and does not seem to work correctly anyway.

I will appreciate any advice regarding matrix multiplication.

max
  • 3,915
  • 2
  • 9
  • 25
Honzowski
  • 33
  • 4
  • 1
    `a = [[6]]` is not `a = 6`. If you need it to be an array, leave it as an array. I suspect an XY problem but it's hard to tell because a lot of specific information is missing. You _could_ manually check whether inputs are ints or arrays, but that usually isn't necessary if you do things right. – Andras Deak -- Слава Україні Oct 28 '20 at 19:30
  • 1
    What's you expected output when you multiply a matrix by `[3, 2]`? Do you want two matrix outputs? Sounds like you need to familiarise yourself your the [element-wise operations](https://uk.mathworks.com/help/matlab/ref/times.html). – Wolfie Oct 29 '20 at 08:20
  • Yes, it seems i have missed some important information from the math behind that. Andras was essentially correct. I have given that problem another thought and there are certain rules for the elements. Let's say we have vector `beta = [[1],[2]]` and a data set `x` in matrix form that has two columns and `n` observations. If you use `t(x) @ x @ beta ` this works fine and the result is correct. I thought that if beta was a single number i could just use `beta=1` but since the method is written to be able to get result from vector, it is important to store even scalars as a matrix 1x1: `[[1]]` – Honzowski Nov 02 '20 at 13:26
  • The sizes of `beta` and `x` are related - i.e. `beta` has to have as many rows as `x` has columns. I missed that while thinking strictly about the equations i had to rewrite. – Honzowski Nov 02 '20 at 13:31
  • Looking at [numpy.dot](https://numpy.org/doc/stable/reference/generated/numpy.dot.html?highlight=dot#numpy.dot) it seems to me that you already have a different behaviour if both a and b are matrices (it should perform matrix multiplication) – enrico_ay Nov 26 '20 at 18:55

0 Answers0