如何在stan / pystan中包括数据测量不确定度

问题描述 投票:0回答:1

我对斯坦完全陌生。我只是想拟合在测量中具有不确定性的数据,但无法在拟合中包括不确定性。例如,我有维度为N的x [N],y [N]和yerror [N]数组。假设数据是二阶多项式:y = a0 + a1x + a2x * x,并且我的误差为y, yerror [N]。现在,我在pystan中的代码如下:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

A0=0.5; A1=1.5; A2=-0.2; A3=-0.008; A4=0.00025
def func(xx):
    return A0+A1*xx+A2*xx*xx#+A3*(x**3)+A4*(x**4)

x=10*np.random.rand(100); x=np.sort(x)
fx=func(x);
yerror=np.random.rand(len(x))
y=np.random.normal(fx,scale=sigy)

np.random.seed(101)

model = """
data {
    int<lower=0> N;
    vector[N] x;
    vector[N] y;
}
parameters {
    real a0;
    real a1;
    real a2;
    real<lower=0> sigma;
}
model {
    vector[N] x2;
    for(i in 1:N){x2[i]=x[i]*x[i];}
    y ~ normal(a0 + a1 * x + a2 * x2, sigma);
}
"""

# Put our data in a dictionary
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=2000, chains=4, warmup=400, thin=3, seed=101)

代码未在数据测量中使用不确定度yerror [N]。怎么做?抱歉,如果我已经问了一些愚蠢/已回答的问题。预先感谢。

python montecarlo mcmc stan pystan
1个回答
0
投票

我从following link找到了答案。我们必须将“参数” sigma更改为已知的错误。

© www.soinside.com 2019 - 2024. All rights reserved.