简体   繁体   English

马尔可夫链蒙特卡洛(python,numpy)

[英]Markov Chain Monte Carlo (python, numpy)

I am doing some research in physics, for which I need to analyze some data using a Markov Chain Monte Carlo (MCMC). 我正在做一些物理研究,为此我需要使用Markov Chain Monte Carlo(MCMC)分析一些数据。 I tried to just write one myself but I keep coming across bugs when python/numpy rounds a very very small number down to zero. 我试图自己写一个,但是当python / numpy将一个非常小的数字四舍五入时,我总是遇到错误。 Specifially when I need to do something like numpy.exp(-1000) . 特别是当我需要执行numpy.exp(-1000) That expression itself is part of a larger math equation so I can't just take the log of it. 该表达式本身是一个较大的数学方程式的一部分,因此我不能仅取其对数。

I know that there are MCMC modules available for python and I have taken a look at some of them but am having trouble understanding the documentation for them to apply them. 我知道有适用于python的MCMC模块,我已经看过其中的一些模块,但是在理解文档中无法应用它们时遇到了麻烦。 Can anyone recommend one? 谁能推荐一个? What I have is a column of data that I plug into a probability distribution. 我所拥有的是我插入概率分布的一列数据。 This distribution also has two other variables which I will be conducting random walks on and recording each step in the markov chain. 此分布还具有其他两个变量,我将进行随机游走并记录马尔可夫链中的每个步骤。 I will then need to make a histogram of each of those two variables based off the markov chain. 然后,我需要根据马尔可夫链对这两个变量中的每一个进行直方图绘制。 I apologize if this question is too vague. 如果这个问题太含糊,我深表歉意。 Any ideas or suggestions are much appreciated, thanks! 任何想法或建议都非常感谢,谢谢!

Use higher-precision floats, if available on your system. 如果系统上可用,请使用更高精度的浮点数。 For example, if you have float128 : 例如,如果您有float128

import numpy as np
print(np.exp(np.float128(-1000)))  # 5.07595889755e-435
print(np.exp(np.float128(-10000)))  #  1.13548386531e-4343

Also see longdouble . 另请参阅longdouble It really depends on your operating system to what is supported and how. 实际上,这取决于您的操作系统支持什么以及如何支持。

You can convert arrays that require this precision and work with them with Numpy functions: 您可以转换需要此精度的数组,并使用Numpy函数对其进行处理:

# Example array with 3 dimensions
d = np.random.uniform(-10000, -100, 24)
d.shape = (2, 3, 4)

# Cast to a higher precision
D = d.astype(np.float128)
np.exp(D[:,2])  # array([[4.263772e-4326, 4.3465066e-1474, ...

Use PyMC - it's great. 使用PyMC-很棒。 Work through the tutorials and you should soon understand how to build a model. 完成这些教程,您应该很快了解如何构建模型。

http://pymc-devs.github.io/pymc/tutorial.html http://pymc-devs.github.io/pymc/tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM