How to match shapes with one RV and one matrix


I have a matrix with shape (18793, 12). I’m trying to do matrix multiplication with my beta RVs but getting a shape mismatch error. The model is as follows:

coords = {
    'fourier': ['monthly_cos_1', 'monthly_cos_2', 'monthly_cos_3', 'monthly_cos_4','monthly_cos_5', 'monthly_cos_6', 'monthly_sin_1', 'monthly_sin_2',
       'monthly_sin_3', 'monthly_sin_4', 'monthly_sin_5', 'monthly_sin_6'],
    'lags': ['1', '2', '3', '4', '5', '6']
with pm.Model(coords=coords) as linear_model:
    fourier = pm.Data('fourier', fourier_array, mutable = True)
    pm_time_idx = pm.Data('time_idx', time_idx, mutable=True)
    obs = pm.Data('obs_array', obs_array, mutable = True)

    beta = pm.Normal('beta', mu=0, sigma = .1, dims = ['fourier'])
    alpha = pm.Normal('alpha', mu=0, sigma = 1)

    mu = alpha + fourier[pm_time_idx] @ beta
    sigma = pm.HalfCauchy('sigma', beta=0.1)

    obs = pm.Normal('obs', mu = mu, sigma = sigma, observed = obs)

Below is a graphical representation:

This is the error:

ValueError: shape mismatch: objects cannot be broadcast to a single shape.  Mismatch is between arg 0 with shape (18793, 12) and arg 1 with shape (18793,).
Apply node that caused the error: normal_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FEE4CCE6340>), MakeVector{dtype='int64'}.0, 11, Add.0, sigma)
Toposort index: 12
Inputs types: [RandomGeneratorType, TensorType(int64, shape=(2,)), TensorType(int64, shape=()), TensorType(float64, shape=(None,)), TensorType(float64, shape=())]
Inputs shapes: ['No shapes', (2,), (), (18793,), ()]
Inputs strides: ['No strides', (8,), (), (8,), ()]
Inputs values: [Generator(PCG64) at 0x7FEE4CCE6340, array([18793,    12]), array(11), 'not shown', array(0.07665108)]
Outputs clients: [['output'], ['output']]

HINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

It looks like there is something wrong with my time index but I haven’t been able to wrangle it to the right shape.

Evaluate your shape on mu.
I imagine you’re not getting what you expect out of the matrix multiplication.


You needed to transpose the fourier array:


1 Like