{"action":"created","issue":{"url":"https://api.github.com/repos/p2t2/figaro/issues/533","labels_url":"https://api.github.com/repos/p2t2/figaro/issues/533/labels{/name}","comments_url":"https://api.github.com/repos/p2t2/figaro/issues/533/comments","events_url":"https://api.github.com/repos/p2t2/figaro/issues/533/events","html_url":"https://github.com/p2t2/figaro/issues/533","id":121132919,"number":533,"title":"How to make a learnable HMM? (Question)","user":{"login":"mioalter","id":3781776,"avatar_url":"https://avatars.githubusercontent.com/u/3781776?v=3","gravatar_id":"","url":"https://api.github.com/users/mioalter","html_url":"https://github.com/mioalter","followers_url":"https://api.github.com/users/mioalter/followers","following_url":"https://api.github.com/users/mioalter/following{/other_user}","gists_url":"https://api.github.com/users/mioalter/gists{/gist_id}","starred_url":"https://api.github.com/users/mioalter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mioalter/subscriptions","organizations_url":"https://api.github.com/users/mioalter/orgs","repos_url":"https://api.github.com/users/mioalter/repos","events_url":"https://api.github.com/users/mioalter/events{/privacy}","received_events_url":"https://api.github.com/users/mioalter/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/p2t2/figaro/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-12-09T00:29:20Z","updated_at":"2016-01-11T22:22:56Z","closed_at":null,"body":"Hi,\r\n\r\nI can construct an HMM, but I'm having trouble making an HMM with learnable parameters. I'll explain my setup and hopefully you can suggest changes and/or a way forward.  \r\n\r\nMy HMM is the one from wikipedia https://en.wikipedia.org/wiki/Hidden_Markov_model\r\nin which the hidden states are {Rainy, Sunny} and the observed states are {Walk, Shop, Clean}.\r\nI have\r\n* types `H` and `O` to model the hidden and observed states\r\n* an HMM class\r\n```\r\nclass HMM(\r\n    val length : Int\r\n    , val apriori : Array[Double]\r\n    , val rainyT : Array[Double]\r\n    , val sunnyT : Array[Double]\r\n    , val rainyE : Array[Double]\r\n    , val sunnyE : Array[Double]\r\n) {\r\n    val hidden : List[Element[H]] = makeHiddenSequence(length, transition(rainyT, sunnyT), apriori)\r\n    val observed : List[Element[O]] = makeObservedSequence(hidden, emission(rainyE, sunnyE))\r\n}\r\n```\r\n\r\nin which `apriori` represents the apriori distribution over hidden states, `rainyT` and `sunnyT` are the rows of the transition matrix, `rainyE` and `sunnyE` are the rows of the emission matrix. I have helper functions, like `transition : (Array[Double], Array[Double]) => (Element[H] => Element[H])` which makes the transition matrix from its rows, that I use to construct the vals `hidden` and `observed` for the given data. \r\n\r\nI am not very clear on how to make a parameterized version of this. Starting from the types in my HMM class, it looks like I want to \r\n* replace `Array[Double]` with `Parameter[Array[Double]]`\r\n* replace `Select`s defined in terms of `Array` values in my helper functions by `ParameterizedSelect`s \r\n* write classes `class ParameterizedH(...) extends Parameterized[H]` and `class ParameterizedO(...) extends Parameterized[O]` (I'm a little fuzzy on this)\r\n* replace `List[Element[H]]` with `List[ParameterizedH]` and similarly for `O`.\r\n\r\nSo, my question: is there a way to set this up that is more easily amenable to making a parameterized version and/or do you have any pointers?\r\n\r\nThanks!\r\n-Mio"},"comment":{"url":"https://api.github.com/repos/p2t2/figaro/issues/comments/170710363","html_url":"https://github.com/p2t2/figaro/issues/533#issuecomment-170710363","issue_url":"https://api.github.com/repos/p2t2/figaro/issues/533","id":170710363,"user":{"login":"apfeffer","id":5399671,"avatar_url":"https://avatars.githubusercontent.com/u/5399671?v=3","gravatar_id":"","url":"https://api.github.com/users/apfeffer","html_url":"https://github.com/apfeffer","followers_url":"https://api.github.com/users/apfeffer/followers","following_url":"https://api.github.com/users/apfeffer/following{/other_user}","gists_url":"https://api.github.com/users/apfeffer/gists{/gist_id}","starred_url":"https://api.github.com/users/apfeffer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apfeffer/subscriptions","organizations_url":"https://api.github.com/users/apfeffer/orgs","repos_url":"https://api.github.com/users/apfeffer/repos","events_url":"https://api.github.com/users/apfeffer/events{/privacy}","received_events_url":"https://api.github.com/users/apfeffer/received_events","type":"User","site_admin":false},"created_at":"2016-01-11T22:22:56Z","updated_at":"2016-01-11T22:22:56Z","body":"HI Mio,\r\n\r\nYou should be able to say something like the following (I have not checked this code for correctness):\r\n\r\nval color = Select(Dirichlet(2, 3), ‘red, ‘green)\r\nval size =\r\n  CPD(color,\r\n     ‘red -> Select(Dirichlet(1, 2, 3), ‘small, ‘medium, ‘large),\r\n     ‘green -> Select(Dirichlet(1, 1), ‘medium, ‘large))\r\n\r\nThe type of size should be ParameterizedSelect. (CPD is just syntactic sugar for Chain).\r\n\r\nIs this what you’re looking for?\r\n\r\nThanks,\r\n\r\nAvi\r\n+ + + + + + + + + + + + + +\r\nAvi Pfeffer, PhD\r\nPrincipal Scientist\r\nCharles River Analytics\r\n617.491.3474 x513\r\nww.cra.com\r\n\r\n\r\nFrom: Mio Alter [mailto:notifications@github.com]\r\nSent: Thursday, January 7, 2016 2:23 PM\r\nTo: p2t2/figaro <figaro@noreply.github.com>\r\nSubject: Re: [figaro] How to make a learnable HMM? (Question) (#533)\r\n\r\n\r\nHi Brian,\r\n\r\nno worries. I haven't yet, but I'll have more time to try again next week. My fundamental confusion is: I don't see an easy way to Chain a ParameterizedSelect and a function to make new a ParameterizedSelect the way I can Chain an Element and a function to make a new Element. Am I missing something obvious? If not, I'll look more at how Chain is implemented for Element[_] and see if I can do the same for Parameterized[_].\r\n\r\nThanks,\r\nMio\r\n\r\n—\r\nReply to this email directly or view it on GitHub<https://github.com/p2t2/figaro/issues/533#issuecomment-169780447>.\r\n"}}