我是Python的新手,并认为这应该是一个相当普遍的问题,但无法找到解决方案.我已经看过这个页面,发现它对一个项目很有帮助,但我很难将示例扩展到多个项目而不使用'for'循环.我正在通过Emcee为250名步行者运行这段代码,所以我正在寻找最快的方法.
我有一个数字列表a = [x,y,z]
,我想重复b = [1,2,3]
一次(例如),所以我最终得到一个列表列表:
[
[x],
[y,y],
[z,z,z]
]
Run Code Online (Sandbox Code Playgroud)
我的'for'循环是:
c = [ ]
for i in range (0,len(a)):
c.append([a[i]]*b[i])
Run Code Online (Sandbox Code Playgroud)
这正是我想要的,但意味着我的代码极其缓慢.我也尝试过天真地将a和b转换为数组并[a]*b
希望它能够逐个元素地增加,但没有快乐.
我试图在使用司仪之前使用高斯,但似乎无法完全弄清楚。基本上我想更换
def lnprior(theta):
a, b, c = theta
if 1.0 < a < 2.0 and 1.0 < b < 2.0 and 1.0 < c < 2.0:
return 0.0
return -np.inf
Run Code Online (Sandbox Code Playgroud)
使用可以从具有 mu 和 sigma 的高斯分布中采样“a”的东西。我该怎么做?像这样?
def lnprior(theta):
a, b, c = theta
if 1.0 < b < 2.0 and 1.0 < c < 2.0:
return 0.0
if 0<a<20:
mu=10
sigma=1
s=np.random.normal(mu, sigma)
return s
return -np.inf
Run Code Online (Sandbox Code Playgroud)
但这似乎不对?
在马尔可夫链蒙特卡罗的背景下,我想使用 绘制参数估计问题的二维后验分布corner.corner
。我现在的图看起来像这样:
在等值线图中,等值线由概率分位数定义,因此等值线之外的概率较低。
我想知道是否可以删除概率轮廓之外的背景样本(即灰点)。
pymc3
在专门使用了多年之后,我最近开始学习emcee
,并且遇到了一些概念问题。
我正在练习霍格的《将模型拟合到数据》的第 7 章。这涉及到具有任意二维不确定性的直线的 mcmc 拟合。我在 中很容易地完成了这一点emcee
,但pymc
给我带来了一些问题。
它本质上归结为使用多元高斯似然。
这是我到目前为止所拥有的。
from pymc3 import *
import numpy as np
import matplotlib.pyplot as plt
size = 200
true_intercept = 1
true_slope = 2
true_x = np.linspace(0, 1, size)
# y = a + b*x
true_regression_line = true_intercept + true_slope * true_x
# add noise
# here the errors are all the same but the real world they are usually not!
std_y, std_x = 0.1, …
Run Code Online (Sandbox Code Playgroud)