I have issue with the deterministic and sampling function. The sampler is consist of three priors (a0, a1, a2) that will be acting as mean function to a simple finite element function. The displacement obtained from FE (uFE) will act as the input to likelihood function in MvNormal. There are several matrix operations in the FE (FEpyMCsoln) and why I can’t use the a0, a1, a2 directly from the prior sampling to matrix inversion. As this likely be the case for this error. Help is deeply appreciated.
# Define FEpyMCsoln function
def FEpyMCsoln(x_c, mean):
global_stiffness_results = []
for j in range(len(x_c)):
element = Element(centre=x_c[j], node_ids=[j], diffuse=mean[j], max_ids=len(x_c), ident=[j+1])
global_stiffness = element.giveGlobalStiffness()
global_stiffness_results.append(global_stiffness)
# Combine global stiffness matrices to get the final global stiffness matrix
A = np.sum(global_stiffness_results, axis=0) # Sum along the appropriate axis
Ainv = np.linalg.inv(A)
BC = np.ones((len(A)-1))
ufe = np.dot(Ainv[1:, 1:], BC)
return ufe
# Assuming x_c and u are defined elsewhere
# Define perturbation
perturb = 0.05 * np.max(u[:,0])
# Generate perturbed data in NumPy
y = u + np.random.normal(loc=0.0, scale=perturb, size=u.shape)
# Flatten x_c to a numpy array
x_cent = x_c
# Create covariance matrix
covariance = np.eye(len(x_cent)) * perturb
# Define the model
with pm.Model() as model:
# Priors
a0 = pm.Normal('a0', mu=1., sigma=0.5)
a1 = pm.Normal('a1', mu=0.25, sigma=0.5)
a2 = pm.Normal('a2', mu=0.05, sigma=0.5)
mean = np.exp(a0 + a1 * np.sin(np.pi * x_cent) + a2 * np.sin(2 * np.pi * x_cent))
# Define FEpyMCsoln as a deterministic pm
uFE = pm.Deterministic('u', FEpyMCsoln(x_c, mean))
# Likelihood
likelihood = pm.MvNormal('y', mu=uFE, cov=covariance, observed=y)
# MCMC sampling
trace = pm.sample(1000, tune=1000)
The error is as follows.
UFuncTypeError: Cannot cast ufunc 'inv' input from dtype('O') to dtype('float64') with casting rule 'same_kind'