PyMC3选择模型内的数据进行开关点分析(PyMC3 select data within model for switchpoint analysis)

编程入门 行业动态 更新时间:2024-10-26 02:33:45
PyMC3选择模型内的数据进行开关点分析(PyMC3 select data within model for switchpoint analysis)

我正在生成一个时间序列,其中间发生了巨大的变化。

import numpy as np size = 120 x1 = np.random.randn(size) x2 = np.random.randn(size) * 4 x = np.hstack([x1, x2])

这一系列的x看起来像这样:

目标是使用PyMC3来估计发生变化的时间的后验分布(切换点)。 这应该发生在索引120附近。我使用了下面的代码;

from pymc3 import Model, Normal, HalfNormal, DiscreteUniform basic_model = Model() with basic_model: mu1 = Normal('mu1', mu=0, sd=10) mu2 = Normal('mu2', mu=0, sd=10) sigma1 = HalfNormal('sigma1', sd=2) sigma2 = HalfNormal('sigma2', sd=2) tau = DiscreteUniform('tau', 0, 240) # get likelihoods y1 = Normal('y1', mu=mu1, sd=sigma1, observed=x[:tau]) y2 = Normal('y2', mu=mu2, sd=sigma2, observed=x[tau:])

这样做会产生一个错误,我不能使用tau来切片数组。 在PyMC中解决这个问题的方法是什么? 似乎我需要通过PyMC中的随机事件来完成切片。

I am generating a time series that has a drastic change in the middle.

import numpy as np size = 120 x1 = np.random.randn(size) x2 = np.random.randn(size) * 4 x = np.hstack([x1, x2])

This series of x looks like this:

The goal is now to use PyMC3 to estimate the posterior distribution of the time when the change occurred (switchpoint). This should occur around the index 120. I've used the following code;

from pymc3 import Model, Normal, HalfNormal, DiscreteUniform basic_model = Model() with basic_model: mu1 = Normal('mu1', mu=0, sd=10) mu2 = Normal('mu2', mu=0, sd=10) sigma1 = HalfNormal('sigma1', sd=2) sigma2 = HalfNormal('sigma2', sd=2) tau = DiscreteUniform('tau', 0, 240) # get likelihoods y1 = Normal('y1', mu=mu1, sd=sigma1, observed=x[:tau]) y2 = Normal('y2', mu=mu2, sd=sigma2, observed=x[tau:])

Doing this gives an error that I cannot use tau to slice the array. What would be the approach to solve this in PyMC? It seems like I'll need the slicing to be done by something stochastic in PyMC.

最满意答案

结果PyMC3有一个开关模型。 设t是时间的变量。

import pymc3 as pm basic_model = pm.Model() with basic_model: mu1 = pm.Normal('mu1', mu=0, sd=10) mu2 = pm.Normal('mu2', mu=0, sd=10) sigma1 = pm.HalfNormal('sigma1', sd=2) sigma2 = pm.HalfNormal('sigma2', sd=2) switchpoint = pm.DiscreteUniform('switchpoint', t.min(), t.max()) tau_mu = pm.switch(t >= switchpoint, mu1, mu2) tau_sigma = pm.switch(t >= switchpoint, sigma1, sigma2) y = pm.Normal('y1', mu=tau_mu, sd=tau_sigma, observed=x)

Turns out PyMC3 has a switch model. Let t be the variable for time.

import pymc3 as pm basic_model = pm.Model() with basic_model: mu1 = pm.Normal('mu1', mu=0, sd=10) mu2 = pm.Normal('mu2', mu=0, sd=10) sigma1 = pm.HalfNormal('sigma1', sd=2) sigma2 = pm.HalfNormal('sigma2', sd=2) switchpoint = pm.DiscreteUniform('switchpoint', t.min(), t.max()) tau_mu = pm.switch(t >= switchpoint, mu1, mu2) tau_sigma = pm.switch(t >= switchpoint, sigma1, sigma2) y = pm.Normal('y1', mu=tau_mu, sd=tau_sigma, observed=x)

更多推荐

本文发布于:2023-04-29 06:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   数据   select   switchpoint   analysis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!