我正在尝试对在Pystan中进行的贝叶斯分析进行敏感性分析。对于敏感性分析,我考虑使用Pystran或SALib。
我的贝叶斯作品在这里。
from SALib.sample import saltelli
from SALib.analyze import morris
import numpy as np
from SALib.plotting.morris import horizontal_bar_plot, covariance_plot
import pystan
import matplotlib.pyplot as plt
#sns.set() # Nice plot aesthetic
#np.random.seed(101)
model = """
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
real b;
real a;
real<lower=0> sigma;
}
model {
y ~ normal(b*x/a + x, sigma);
}
"""
b = 0.5
a = 4.0
sigma = 0.1
# Generate and plot data
x = np.random.rand(5)
y = b*(x/(a+x))
y = np.random.normal(y, scale=sigma)
data = {'N': len(x), 'x': x, 'y': y}
# Compile the model
sm = pystan.StanModel(model_code=model)
# Train the model and generate samples
fit = sm.sampling(data=data, iter=1000, chains=4, warmup=500, thin=1, seed=101)
所以我得到某种输出。如何转换此输出以满足SALib灵敏度分析。
problem = {
'num_vars': 2,
'names': ['a', 'b'],
'bounds': [[-0.1, 0.1],
[-0.1, 0.1]]
}
param_values = saltelli.sample(problem, 1000)
# Run model (example)
# Perform analysis
Si = morris.analyze(problem,param_values, fit, print_to_console=True)
AttributeError: 'stanfit4anon_model_daf2269febef107b3bd9f4dd43dce80' object has no attribute 'dtype'