Error in basic model due to trigonometric functions using math library

Dear all,
I am very new on python and just start using PYMC3. I am getting an error while using trigonometric function in the model of PYMC3. Can You please help me why this occuring and how can I avoid this in future.
Please note that I have already calculated the data (Y) separately.
Could you please help me in this regard. Thank you!

import pymc3 as pm
basic_model = pm.Model()
with basic_model:
    A_1 = pm.Normal('A_1', mu=0, sd=1)
    A_2 = pm.Normal('A_2', mu=0, sd=1)
    sigma=1
    mu=(math.sin(math.radians(A_1)))*x+A_2
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=y)

[OUTPUT]:

  File "<ipython-input-16-916a619013f6>", line 22, in <module>
    mu=(math.sin(math.radians(A_1)))*x+A_2

TypeError: must be real number, not FreeRV

You cannot use functions from the math library (or numpy/scipy for that matter), as in most case the variables under the pymc3 with context are theano tensors. You should subtuitue it with either a theano.tensor function or a function from pymc3.math

Thank You very much Junpenglao!

One more question in this regard: My forward model convert the output from matrix to float as following:
Output1=float(output1). When I calculate my data, I get no error of course but when I call same function in the PYMC3 model, I get following error:

TypeError: float() argument must be a string or a number, not ‘TensorVariable’

Can you please help also in this.

You dont need to do that for TensorVariable, they are ndarray backend.

In the same work: Another error. Can you please also suggest what wrong I am doing here. Following is my code:

#%% Forward code
import matplotlib.pyplot as plt
import numpy as np
import math
x0=3
y0=6
z0=12
Azim=89.9
xrot1=np.matrix([[math.cos(math.radians(Azim)),\
          -math.sin(math.radians(Azim)), 0],\
          [math.sin(math.radians(Azim)),\
          math.cos(math.radians(Azim)),\
          0], [0, 0, 1]])

new_x= xrot1*(np.matrix([[x0],[y0],[z0]]))

h  = new_x[2]
U=h + (10.0*0.9999)

#%% PYMC model

import pymc3 as pm
basic_model = pm.Model()

with basic_model:
       
    Azim=pm.Normal('Azim', mu=0, sd=1)    
    sigma = pm.HalfNormal('sigma', sd=1)
    
    xrot=np.matrix([[pm.math.cos(Azim),\
          -pm.math.sin(Azim), 0],\
          [pm.math.sin(Azim),\
          pm.math.cos(Azim),\
          0], [0, 0, 1]])
    
    new_x= xrot*(np.matrix([[x0],[y0],[z0]]))
    h  = np.array(new_x[2])
    mu=h + (10.0*0.9999)
        
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

-----------------------------output error--------------------------

  File "<ipython-input-34-d6ad12081d4d>", line 20, in <module>
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\distribution.py", line 41, in __new__
    dist = cls.dist(*args, **kwargs)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\distribution.py", line 52, in dist
    dist.__init__(*args, **kwargs)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\continuous.py", line 431, in __init__
    self.mean = self.median = self.mode = self.mu = mu = tt.as_tensor_variable(mu)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\tensor\basic.py", line 200, in as_tensor_variable
    raise AsTensorError("Cannot convert %s to TensorType" % str_x, type(x))

AsTensorError: ('Cannot convert [[21.999000000000002]] to TensorType', <class 'numpy.ndarray'>)

Actually I dont get any error running your code, so make sure upgrade to the newest release of PyMC3 and theano. However, you can still make your model block more theano/pymc3:

import pymc3 as pm
import theano.tensor as tt

with pm.Model() as basic_model:
    Azim = pm.Normal('Azim', mu=0, sd=1)    
    sigma = pm.HalfNormal('sigma', sd=1)
    
    xrot = tt.stacklists([[tt.cos(Azim), -tt.sin(Azim), 0],\
                          [tt.sin(Azim), tt.cos(Azim), 0], 
                          [0, 0, 1]])
    
    new_x= xrot * (np.asarray([[x0],[y0],[z0]]))
    mu = new_x[2] + 10.0*0.9999
    
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

Thank You very much for the reply. I tried with your suggested code and this leads to another error as below (using pymc3 version 3.6, theano version 1.0.3, numpy version 1.16.2):

File “”, line 5, in
Azim = pm.Normal(‘Azim’, mu=0, sd=1)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\distribution.py”, line 41, in new
dist = cls.dist(*args, **kwargs)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\distribution.py”, line 52, in dist
dist.init(*args, **kwargs)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\continuous.py”, line 432, in init
self.variance = 1. / self.tau

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\tensor\var.py”, line 203, in rtruediv
return theano.tensor.basic.true_div(other, self)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\op.py”, line 670, in call
no_recycling=[])

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\op.py”, line 955, in make_thunk
no_recycling)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\op.py”, line 858, in make_c_thunk
output_storage=node_output_storage)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\cc.py”, line 1217, in make_thunk
keep_lock=keep_lock)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\cc.py”, line 1157, in compile
keep_lock=keep_lock)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\cc.py”, line 1609, in cthunk_factory
key = self.cmodule_key()

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\cc.py”, line 1300, in cmodule_key
c_compiler=self.c_compiler(),

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\cc.py”, line 1379, in cmodule_key_
np.core.multiarray._get_ndarray_c_version())

AttributeError: module ‘numpy.core.multiarray’ has no attribute ‘_get_ndarray_c_version’

most likely your set up environment is not correct - are you using conda?

Yes, I install python and all related stuff using conda. I write and run the script in spyder. If you could suggest me better option to code and as well to use PyMc3, please let me know. I am in very initial stage of learning PyMc3 and also pyhton. Thank You [Sorry for late reply because it was night here].

I think I come across questions re problem under spyder sometimes as well - I recommend using either jupyter notebook or jupyterlab.

I had the same problem when I updated numpy. Part of numpy is compiled c code and another is pure python, the mismatch happened because the update just change the pure python part and did not recompile the c part. I think that I solved it uninstalling and then reinstalling numpy

1 Like