python贝叶斯估计库
Synthetic data is widely used in various domains. This is because many modern algorithms require lots of data for efficient training, and data collection and labeling usually are a time-consuming process and are prone to errors. Furthermore, some real-world data, due to its nature, is confidential and cannot be shared.
合成数据广泛用于各个领域。 这是因为许多现代算法需要大量数据才能进行有效的训练,并且数据收集和标记通常是一个耗时的过程,并且容易出错。 此外,由于其性质,某些真实世界的数据是机密的,无法共享。
Some methods, such as generative adversarial network¹, are proposed to generate time series data. However, GAN is hard to train and might not be stable; besides, it requires a large volume of data for efficient training.
提出了一些方法,例如生成对抗网络¹,来生成时间序列数据。 但是,GAN很难训练,并且可能不稳定。 此外,它需要大量的数据来进行有效的培训。
This article will introduce the tsBNgen, a python library, to generate synthetic time series data based on an arbitrary dynamic Bayesian network structure.
本文将介绍tsBNgen (一个python库),以基于任意动态贝叶斯网络结构生成综合时间序列数据。
Following is a list topics discussed in this article.
以下是本文讨论的主题列表。
Introduction
介绍
Features
特征
Instruction
指令
Example
例
Conclusion
结论
Miroslava on Miroslava摄于 Unsplash UnsplashtsBNgen is a python package released under the MIT license to generate time series data from an arbitrary Bayesian network structure. Bayesian networks are a type of probabilistic graphical model widely used to model the uncertainties in real-world processes. Dynamic Bayesian networks (DBNs)are a special class of Bayesian networks that model temporal and time series data.
tsBNgen是根据MIT许可发布的python软件包,可从任意贝叶斯网络结构生成时间序列数据。 贝叶斯网络是一种概率图形模型,广泛用于模拟现实世界过程中的不确定性。 动态贝叶斯网络(DBN)是一类特殊的贝叶斯网络,用于对时间和时间序列数据进行建模。
Bayesian networks receive lots of attention in various domains, such as education and medicine. For example, in², the authors used an HMM, a variant of DBN, to predict student performance in an educational video game. One significant advantage of directed graphical models (Bayesian networks) is that they can represent the causal relationship between nodes in a graph; hence they provide an intuitive method to model real-world processes. This statement makes tsBNgen very useful software to generate data once the graph structure is determined by an expert.
贝叶斯网络在教育和医学等各个领域都受到广泛关注。 例如,在in²中,作者使用HMM(DBN的一种)来预测学生在教育视频游戏中的表现。 有向图模型(贝叶斯网络)的一个显着优点是它们可以表示图中节点之间的因果关系。 因此,它们提供了一种对实际过程进行建模的直观方法。 该声明使tsBNgen非常有用,可以在专家确定图形结构后生成数据。
To learn more about the package, documentation, and examples, please visit the following GitHub repository.
要了解有关软件包,文档和示例的更多信息,请访问以下GitHub存储库。
Following is the list of supported features and capabilities of tsBNgen:
以下是tsBNgen支持的功能列表:
Easy and simple interface. 简单易用的界面。 Support for discrete, continuous, and hybrid networks (a mixture of discrete and continuous nodes). 支持离散,连续和混合网络(离散和连续节点的混合)。 Support for discrete nodes using multinomial distributions and Gaussian distributions for continuous nodes. 支持使用连续分布的多项式分布和高斯分布的离散节点。 Supports arbitrary loopback (temporal connection) values for temporal dependencies. 支持任意的环回(时间连接)值以用于时间依赖性。 Easy to modify and extend the code to support for the new structure. 易于修改和扩展代码以支持新结构。To use tsBNgen, either clone the above repository or install the software using the following commands:
要使用tsBNgen ,请克隆以上存储库或使用以下命令安装软件:
pip install tsBNgenAfter the software is successfully installed, then issue the following commands to import all the functions and variables.
成功安装软件后,然后发出以下命令以导入所有功能和变量。
from tsBNgen import * from tsBNgen.tsBNgen import *This is all you need to take advantage of all the functionalities that exist in the software.
这就是您需要利用软件中所有功能的全部。
Before going over some examples, let me define the following parameters, which they will be used throughout this section.Note: The following description, tables (as a form of an image) and images are obtained from this paper by the author³.
在介绍一些示例之前,让我定义以下参数,在本节中将使用它们。 注意:以下说明,表格(以图像的形式)和图像是作者³从本文中获得的。
Example 1
例子1
Assume you would like to generate data for the following architecture in Fig 1, which is an HMM structure.
假设您想为图1中的以下架构生成数据,该架构是HMM结构。
Fig 1. (Photo by the Author) 图1.(作者照片)The top layer nodes are known as states, and the lower ones are called the observation. In HMM, states are discrete, while observations can be either continuous or discrete. The following tables summarize the parameters setting and probability distributions for the Fig 1.
顶层节点称为状态,而下层节点称为观察。 在HMM中,状态是离散的,而观测值可以是连续的或离散的。 下表总结了图1的参数设置和概率分布。
Conditional Probability Distribution for all the nodes at time t (Photo by the Author) 在时间t所有节点的条件概率分布(作者提供的照片)In Table 1, T refers to the length of time series, N refers to the number of samples, and loopback determines the length of the temporal connection. For example, a loopback value of 1 implies that a node is connected to some other nodes at previous time.
在表1中,T表示时间序列的长度,N表示采样数,环回确定时间连接的长度。 例如,回送值为1表示一个节点在上一次连接到其他一些节点。
Note: tsBNgen can simulate the standard Bayesian network (cross-sectional data) by setting T=1.
注意: tsBNgen可以通过设置T = 1来模拟标准贝叶斯网络(横截面数据)。
Architecture 1 with the above CPDs and parameters can easily be implemented as follows:
具有上述CPD和参数的体系结构1可以轻松实现如下:
import timeSTART=time.time()T=20N=1000N_level=[4]Mat=pd.DataFrame(np.array(([0,1],[0,0]))) # HMMNode_Type=['D','C']CPD={'0':[0.25,0.25,0.25,0.25],'01':{'mu0':20,'sigma0':5,'mu1':40,'sigma1':5,'mu2':60,'sigma2':5,'mu3':80,'sigma3':5}}Parent={'0':[],'1':[0]}CPD2={'00':[[0.6,0.3,0.05,0.05],[0.25,0.4,0.25,0.1],[0.1,0.3,0.4,0.2],[0.05,0.05,0.4,0.5]],'01':{'mu0':20,'sigma0':5,'mu1':40,'sigma1':5,'mu2':60,'sigma2':5,'mu3':80,'sigma3':5}}loopbacks={'00':[1]}Parent2={'0':[0],'1':[0]}Time_series1=tsBNgen(T,N,N_level,Mat,Node_Type,CPD,Parent,CPD2,Parent2,loopbacks)Time_series1.BN_data_gen()FINISH=time.time()print('Total Time is',FINISH-START)The above code generates a 1000 time series with length 20 corresponding to states and observations. Observations are normally distributed with particular mean and standard deviation. The states are discrete (hence the ‘D’) and take four possible levels determined by the N_level variable. loopbacks is a dictionary in which each key has the following form: node+its parent. Since in architecture 1, only states, namely node 0 (according to the graph’s topological ordering), are connected across time and the parent of node 0 at time t is node 0 at time t-1; therefore, the key value for the loopbacks is ‘00’ and since the temporal connection only spans one unit of time, its value is 1.
上面的代码生成一个1000个时间序列,其长度为20,对应于状态和观察值。 观察值呈正态分布,具有特定的均值和标准差。 状态是离散的(因此为“ D”),并采用由N_level变量确定的四个可能的级别。 loopbacks是一个字典,其中每个键具有以下形式:node +其父级。 由于在体系结构1中,只有状态(即节点0(根据图形的拓扑顺序))跨时间连接,并且在时间t处节点0的父级在时间t-1处为节点0;在状态t处,状态0为节点0。 因此,回送的关键值为“ 00”,并且由于时间连接仅跨越一个时间单位,因此其值为1。
The total time to generate the above data is 2.06 (s), and running the model through the HMM algorithm gives us more than 93.00 % accuracy for even five samples.Now let’s take a look at a more complex example. From now on, to save some space, I avoid showing the CPD tables and only show the architecture and the python code used to generate data.
生成上述数据的总时间为2.06(s),并且通过HMM算法运行该模型即使对于五个样本也可以为我们提供93.00%的精度,现在让我们来看一个更复杂的示例。 从现在开始,为了节省空间,我避免显示CPD表,而只显示用于生成数据的体系结构和python代码。
Example 2
例子2
Example 2 refers to the architecture in Fig 2, where the nodes in the first two layers are discrete and the last layer nodes(u₂) are continuous.
例2涉及图2中的体系结构,其中前两层中的节点是离散的,而最后一层中的节点(u 2)是连续的。
Fig 2 (Photo by the Author) 图2(作者照片)Based on the graph’s topological ordering, you can name them nodes 0, 1, and 2 per time point. Let’s say you would like to generate data when the node 0 (the top node) takes two possible values (binary), the node 1(the middle node) takes four possible values, and the last node is continuous and will be distributed according to Gaussian distribution for every possible value of its parents. The following python codes simulate this scenario for 2000 samples with a length of 20 for each sample.
根据图的拓扑顺序,可以在每个时间点将它们命名为节点0、1和2。 假设您想在节点0(顶部节点)取两个可能值(二进制),节点1(中间节点)取四个可能值,而最后一个节点是连续的并且将根据高斯分布的父母的所有可能价值。 以下python代码针对2000个样本模拟了这种情况,每个样本的长度为20。
T=20N=2000N_level=[2,4]Mat=pd.DataFrame(np.array(([0,1,1],[0,0,1],[0,0,0])))Node_Type=['D','D','C']CPD={'0':[0.6,0.4],'01':[[0.5,0.3,0.15,0.05],[0.1,0.15,0.3,0.45]],'012':{'mu0':10,'sigma0':2,'mu1':30,'sigma1':5, 'mu2':50,'sigma2':5,'mu3':70,'sigma3':5,'mu4':15,'sigma4':5,'mu5':50,'sigma5':5,'mu6':70,'sigma6':5,'mu7':90,'sigma7':3}}Parent={'0':[],'1':[0],'2':[0,1]}CPD2={'00':[[0.7,0.3],[0.2,0.8]],'011':[[0.7,0.2,0.1,0],[0.6,0.3,0.05,0.05],[0.35,0.5,0.15,0],[0.2,0.3,0.4,0.1],[0.3,0.3,0.2,0.2],[0.1,0.2,0.3,0.4],[0.05,0.15,0.3,0.5],[0,0.05,0.25,0.7]],'012':{'mu0':10,'sigma0':2,'mu1':30,'sigma1':5, 'mu2':50,'sigma2':5,'mu3':70,'sigma3':5,'mu4':15,'sigma4':5,'mu5':50,'sigma5':5,'mu6':70,'sigma6':5,'mu7':90,'sigma7':3}}Parent2={'0':[0],'1':[0,1],'2':[0,1]}loopbacks={'00':[1],'11':[1]}Time_series2=tsBNgen(T,N,N_level,Mat,Node_Type,CPD,Parent,CPD2,Parent2,loopbacks)Time_series2.BN_data_gen()In the same way, you can generate time series data for any graphical models you want. This is a wonderful tool since lots of real-world problems can be modeled as Bayesian and causal networks.
同样,您可以为所需的任何图形模型生成时间序列数据。 这是一个很棒的工具,因为可以将许多现实世界中的问题建模为贝叶斯和因果网络。
For more examples, up-to-date documentation please visit the following GitHub page.
有关更多示例,最新文档,请访问以下GitHub页面。
Bonus: If you would like to see a comparative analysis of graphical modeling algorithms such as the HMM and deep learning methods such as the LSTM on a synthetically generated time series, please look at this paper⁴.
优点:如果您希望在综合生成的时间序列上对图形建模算法(例如HMM)和深度学习方法(例如LSTM)进行比较分析,请查看本文⁴。
In this article, I introduced the tsBNgen, a python library to generate synthetic data from an arbitrary BN. The features and capabilities of the software are explained using two examples. For more up-to-date information about the software, please visit the GitHub page mentioned above.
在本文中,我介绍了tsBNgen ,这是一个从任意BN生成合成数据的python库。 使用两个示例说明该软件的功能。 有关该软件的更多最新信息,请访问上面提到的GitHub页面。
[1] M. Frid-Adar, E. Klangand, M. Amitai, J. Goldberger, H. Greenspan, Synthetic data augmentation using gan for improved liver lesion classification(2018), IEEE 2018 15th international symposium on biomedicalimaging.
[1] M. Frid-Adar,E。Klangand,M。Amitai,J。Goldberger,H。Greenspan,使用gan进行合成数据增强以改善肝脏病变分类(2018年),IEEE 2018第15届国际生物医学影像研讨会。
[2] M. Tadayon, G. Pottie, Predicting Student Performance in an Educational Game Using a Hidden Markov Model(2020), IEEE 2020 IEEE Transactions on Education.
[2] M. Tadayon,G. Pottie,使用隐马尔可夫模型预测教育游戏中的学生表现(2020),IEEE 2020 IEEE Transactions Transactions on Education。
[3] M. Tadayon, G. Pottie, tsBNgen: A Python Library to Generate Time Series Data from an Arbitrary Dynamic Bayesian Network Structure (2020), arXiv 2020, arXiv preprint arXiv:2009.04595.
[3] M. Tadayon,G. Pottie, tsBNgen:一个从任意动态贝叶斯网络结构生成时间序列数据的Python库(2020),arXiv 2020,arXiv预印本arXiv:2009.04595。
[4] M. Tadayon, G. Pottie, Comparative Analysis of the Hidden Markov Model and LSTM: A Simulative Approach (2020), arXiv 2020, arXiv preprint arXiv:2008.03825.
[4] M. Tadayon,G. Pottie, 《隐马尔可夫模型与LSTM的比较分析:一种模拟方法》 (2020年),arXiv 2020,arXiv预印本arXiv:2008.03825。
翻译自: https://medium.com/@manitadayon/tsbngen-a-python-library-to-generate-time-series-data-from-an-arbitrary-dynamic-bayesian-network-4b46e178cd9f
python贝叶斯估计库
相关资源:基于贝叶斯网络模型的交通状态预测