PyMC3在模型中选择用于切换点分析的数据

时间:2016-03-10 16:27:54

标签: python pymc3

我正在制作一个在中间发生剧烈变化的时间序列。

import UIKit
import MessageUI

    class EmailViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate  {

       let userDefaults = NSUserDefaults.standardUserDefaults()

        @IBOutlet weak var name: UITextField!
        @IBOutlet weak var phone: UITextField!
        @IBOutlet weak var email: UITextField!

        @IBAction func SendEmailButton(sender: AnyObject) {

            let fields: [UITextField] = [name, phone, email]

            let messageBody = "Name:\(name.text)\nPhone:\(phone.text)\nEmail:\(email.text)"

            let emailTitle = "Interface Information"
            let toRecipents = [""]
            let mc: MFMailComposeViewController = MFMailComposeViewController()
            mc.mailComposeDelegate = self
            mc.setSubject(emailTitle)
            mc.setMessageBody(messageBody, isHTML: false)
            mc.setToRecipients(toRecipents)
            self.presentViewController(mc, animated: true, completion: nil)

        }
        override func viewDidAppear(animated: Bool) {

            name.text = userDefaults.stringForKey("name")  
            phone.text = userDefaults.stringForKey("phone")
            email.text = userDefaults.stringForKey("email")
        }
    }

这一系列import numpy as np size = 120 x1 = np.random.randn(size) x2 = np.random.randn(size) * 4 x = np.hstack([x1, x2]) 看起来像这样:

enter image description here

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

x

执行此操作会出现错误,我无法使用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:]) 对数组进行切片。在PyMC中解决这个问题的方法是什么?看起来我需要在PyMC中通过随机的东西来完成切片。

1 个答案:

答案 0 :(得分:3)

原来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)