-1

I want to do a Bayesian analysis in python with a custom defined likelihood function. Below is my code.

I get the <class 'int'> error when I run the code. Does anyone know what the problem is? Thanks!

import pandas as pd
import numpy as np
import pymc3 as pm
from gurobipy import quicksum
df = pd.DataFrame(np.random.uniform(1, 125, 100), columns= ['Surface'])

def logp(Surface):
   return (-c*quicksum(j**b for i in range(0,len(df)) for j in 
range(1,Surface[i].astype(int))))

with pm.Model() as model_g:
   c = pm.Uniform('c', lower=0, upper=1)
   b = pm.Uniform('b', lower=0, upper=1)
   y = pm.DensityDist('y', logp, observed={'Surface':df['Surface']})
   trace_g = pm.sample(100, tune=100)  

az.plot_trace(trace_g, var_names=['c']);      
H2H
  • 31
  • 5

1 Answers1

0

You can't cast a TensorVariable as an int. Range expects an int. We don't know what df or 'Surface' look like.

See this answer: How to get value from a theano tensor variable backed by a shared variable?

swaggg
  • 460
  • 4
  • 13
  • I could not solve the problem using the link. Could you please give me more hints? I included df['Surface'] in my question. Thanks. – H2H Oct 23 '21 at 21:25
  • Okay, then please post all of the code. I'll have a look at it tomorrow. – swaggg Oct 23 '21 at 23:16
  • I updated the code in my question. Thank you so much! – H2H Oct 24 '21 at 01:43
  • Sorry for the delay. Could you explain what your probability distribution function does? – swaggg Oct 26 '21 at 15:52
  • Actually, I could fix the issue by replacing "quicksum" with "sum+". Thanks for your help! – H2H Oct 27 '21 at 16:11
  • Glad you've solved the problem! Sum+? Do you know about the Math module? https://docs.pymc.io/en/stable/api/math.html It has the "sum" function which is intended to be used with Theano variables. – swaggg Oct 27 '21 at 17:17
  • Working with sum+ was easier than the sum function as in each loop I wanted to calculate j**b for j in an uncertain range. – H2H Oct 27 '21 at 18:20