Length of Elemwise

Hi, I am using PYMC3 to apply a MCMC for the :
ratio = (cos(a) * cos(b) / 0.34202014) **(k-1), iterating over k.
I made two functions one to have the function “ratio_attenuation_model” and another to run the MCMC, These are the functions:

def ratio_attenuation_model(data, k):
    obsang = data['ObsSatRange'].to_numpy()
    incang = data['SunIncAng'].to_numpy()
    mag = data['NewScaleMag_2_197'].to_numpy()
    
    # Convert angles to radians
    obsang = np.radians(obsang)
    incang = np.radians(incang)

    # Calculate ratio using the correct equation
    ratio = (np.cos(obsang) * np.cos(incang)) / 0.34202014

    # Reshape ratio for element-wise operation
    # ratio = ratio.flatten()

    # Raise the ratio to the power of k-1
    ratio_pow = ratio ** (k - 1)

    # Add to original magnitude and convert to NumPy array
    modeled_mag = mag.values + ratio_pow.values
    
    return modeled_mag
def ratio_attenu_mcmc(data):
    mag_data = data['NewScaleMag_2_197']

    with pm.Model() as model:
        # Prior distribution for k
        # k = pm.Uniform('k', lower=0., upper=1.)
        k = pm.Normal('k', mu=0, sd=2)

        # Likelihood function
        mu_test = ratio_attenuation_model(data, k)
        likelihood = pm.Normal('likelihood', mu=mu_test, sd=1, observed=mag_data)

        # Step method
        step = pm.step_methods.DEMetropolisZ()

        # Sample from posterior distribution
        trace = pm.sample(draws=1000, step=step)

    # Summary statistics
    summary = pm.summary(trace)

    # Extract trace of k
    k_trace = trace['k']
    k_df = pd.DataFrame(k_trace, columns=['k'])

    return k_df, summary

The code is generating this error the following error:

ValueError                                Traceback (most recent call last)
Input In [178], in <cell line: 1>()
----> 1 ratio_attenu_mcmc(ea01)

Input In [177], in ratio_attenu_mcmc(data)
      7 k = pm.Normal('k', mu=0, sd=10)
      9 # Likelihood function
---> 10 mu_test = ratio_attenuation_model(data, k)
     11 likelihood = pm.Normal('likelihood', mu=mu_test, sd=1, observed=mag_data)
     13 # Step method

Input In [175], in ratio_attenuation_model(data, k)
      7 ratio = (np.cos(data['SunIncAng']) * np.cos(data['ObsSatRange'])) / 0.34202014
      9 # Reshape ratio for element-wise operation
     10 # ratio = ratio.flatten()
     11 
     12 # Raise the ratio to the power of k-1
---> 13 ratio_pow = ratio ** (k - 1)
     15 # Add to original magnitude and convert to NumPy array
     16 modeled_mag = data['NewScaleMag_2_197'].values + ratio_pow

File ~/.local/lib/python3.10/site-packages/pandas/core/ops/common.py:72, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     68             return NotImplemented
     70 other = item_from_zerodim(other)
...
   4888     except (ValueError, NotScalarConstantError):
   4889         raise ValueError(f"Length of {v} cannot be determined")
-> 4891 raise ValueError(f"Length of {v} cannot be determined")

ValueError: Length of Elemwise{pow,no_inplace}.0 cannot be determined

From what I read in some comments, this error can be generated because the variables of the function “ratio” must be numpy arrays. I have been careful to do so but I still get the same error. Do you have any other ideas as to why the error is being generated, please let me know.

pymc3 v.3.11.5
Theano-PyMC v.1.1.2
pymc3 v.3.11.5

pymc3 and theano-pymc are legacy software that are no longer supported. I strongly recommend you update to the latest version of pymc (5.17.0 at the time of writing).

1 Like