{"action":"created","issue":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1401","repository_url":"https://api.github.com/repos/pymc-devs/pymc3","labels_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1401/labels{/name}","comments_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1401/comments","events_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1401/events","html_url":"https://github.com/pymc-devs/pymc3/issues/1401","id":179513554,"number":1401,"title":"ENH - Mixture density class","user":{"login":"AustinRochford","id":3465592,"avatar_url":"https://avatars.githubusercontent.com/u/3465592?v=3","gravatar_id":"","url":"https://api.github.com/users/AustinRochford","html_url":"https://github.com/AustinRochford","followers_url":"https://api.github.com/users/AustinRochford/followers","following_url":"https://api.github.com/users/AustinRochford/following{/other_user}","gists_url":"https://api.github.com/users/AustinRochford/gists{/gist_id}","starred_url":"https://api.github.com/users/AustinRochford/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AustinRochford/subscriptions","organizations_url":"https://api.github.com/users/AustinRochford/orgs","repos_url":"https://api.github.com/users/AustinRochford/repos","events_url":"https://api.github.com/users/AustinRochford/events{/privacy}","received_events_url":"https://api.github.com/users/AustinRochford/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-09-27T14:43:13Z","updated_at":"2016-09-27T16:07:56Z","closed_at":null,"body":"I often find myself marginalizing over discrete variables and using hacks like\r\n\r\n```\r\ndef normal_mixture_rvs(*args, **kwargs):\r\n    w = kwargs['w']\r\n    mu = kwargs['mu']\r\n    tau = kwargs['tau']\r\n    \r\n    size = kwargs['size']\r\n    \r\n    component = np.array([np.random.choice(w.shape[1], size=size, p=w_ / w_.sum())\r\n                          for w_ in w])\r\n    \r\n    return sp.stats.norm.rvs(mu[np.arange(w.shape[0]), component], tau[component]**-0.5)\r\n\r\nclass NormalMixture(pm.distributions.Continuous):\r\n    def __init__(self, w, mu, tau, *args, **kwargs):\r\n        super(NormalMixture, self).__init__(*args, **kwargs)\r\n        \r\n        self.w = w\r\n        self.mu = mu\r\n        self.tau = tau\r\n        \r\n        self.mean = (w * mu).sum()\r\n    \r\n    def random(self, point=None, size=None, repeat=None):\r\n        w, mu, tau = draw_values([self.w, self.mu, self.tau], point=point)\r\n        \r\n        return normal_mixture_rvs(w=w, mu=mu, tau=tau, size=size)\r\n    \r\n    def logp(self, value):\r\n        w = self.w\r\n        mu = self.mu\r\n        tau = self.tau\r\n        \r\n        return bound(logsumexp(tt.log(w) + (-tau * (value - mu)**2 + tt.log(tau / np.pi / 2.)) / 2.,\r\n                               axis=1).sum(),\r\n                     tau >=0, w >= 0, w <= 1)\r\n```\r\n\r\nto speed up convergence.  It seems to me like we could probably add a nice built-in `MixtureDensity` class to facilitate this flexibly.\r\n\r\nLooking for feedback before starting to write this."},"comment":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/comments/249913167","html_url":"https://github.com/pymc-devs/pymc3/issues/1401#issuecomment-249913167","issue_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1401","id":249913167,"user":{"login":"AustinRochford","id":3465592,"avatar_url":"https://avatars.githubusercontent.com/u/3465592?v=3","gravatar_id":"","url":"https://api.github.com/users/AustinRochford","html_url":"https://github.com/AustinRochford","followers_url":"https://api.github.com/users/AustinRochford/followers","following_url":"https://api.github.com/users/AustinRochford/following{/other_user}","gists_url":"https://api.github.com/users/AustinRochford/gists{/gist_id}","starred_url":"https://api.github.com/users/AustinRochford/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AustinRochford/subscriptions","organizations_url":"https://api.github.com/users/AustinRochford/orgs","repos_url":"https://api.github.com/users/AustinRochford/repos","events_url":"https://api.github.com/users/AustinRochford/events{/privacy}","received_events_url":"https://api.github.com/users/AustinRochford/received_events","type":"User","site_admin":false},"created_at":"2016-09-27T16:07:55Z","updated_at":"2016-09-27T16:07:55Z","body":"@kyleabeauchamp yes, I think it's Chapter 11, Latent Discrete Parameters, of the [Stan reference v2.8.0](http://www.uvm.edu/~bbeckage/Teaching/DataAnalysis/Manuals/stan-reference-2.8.0.pdf)"}}
{"actor":{"display_login":"AustinRochford"}}