charles-river-analytics / figaro

Figaro Programming Language and Core Libraries
Other
756 stars 151 forks source link

Create a CPD from a list of parents of the same type #730

Closed davidhamblin closed 6 years ago

davidhamblin commented 6 years ago

Many of the structures within Figaro support only 1 to 5 prior probabilities with any type, such as CPD, RichCPD, Apply, and Tuple. Would it be possible, if the type is consistent, to allow the use of a sequence/list for input of the same type? Instead of something like (T1, T2, T3) input into a CPD, you'd have Seq[T] or List[T]. This would allow for much bigger Bayesian networks that use consistent types (like names of states), such as Symbol or String.

bruttenberg commented 6 years ago

Hi David,

As you point out, because of typing reasons we were never able to make elements that support generic, unlimited priors. You do have two options:

  1. You can always nest elements if you need more than 5 in say, an Apply. It works but I admit it’s cumbersome.

  2. You can always check out the collections library. This library lets you use scala-like collection operations on elements. So you can do things like put a set of elements into a collection, and fold them all into a single element over lists, which you can then use as the prior for another variable.

Does this help?

From: David Hamblin [mailto:notifications@github.com] Sent: Monday, November 20, 2017 3:04 PM To: p2t2/figaro figaro@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [p2t2/figaro] Create a CPD from a list of parents of the same type (#730)

Many of the structures within Figaro support only 1 to 5 prior probabilities with any type, such as CPD, RichCPD, Apply, and Tuple. Would it be possible, if the type is consistent, to allow the use of a sequence/list for input of the same type? Instead of something like (T1, T2, T3) input into a CPD, you'd have Seq[T] or List[T]. This would allow for much bigger Bayesian networks that use consistent types (like names of states), such as Symbol or String.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/p2t2/figaro/issues/730, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AFJOJb8ZfdXdQDFMzMU770XW5J3ESVz5ks5s4dsagaJpZM4Qk0X4.

davidhamblin commented 6 years ago

Interesting, I'll have to take a look at your suggestions, thank you for the quick response! As an aside, do you think it is at all possible to implement an element that would take any number of priors -- as long as they possess the same type (and not saying that CRA needs to do it)? I have been experimenting with it locally, but I am unfortunately limited by my knowledge of the package and Scala.

bruttenberg commented 6 years ago

No problem.

To answer your question, yes I think it is possible. If I were doing it, the quick and dirty way would be to put some syntactic sugar around my suggestion of using a collection. A more optimal way do it would be to create an element that takes in a variable argument number of elements and compose them internally into a data structure that say, a chain, could then use.

From: David Hamblin [mailto:notifications@github.com] Sent: Monday, November 20, 2017 4:13 PM To: p2t2/figaro figaro@noreply.github.com Cc: Brian Ruttenberg bruttenberg@cra.com; Comment comment@noreply.github.com Subject: Re: [p2t2/figaro] Create a CPD from a list of parents of the same type (#730)

Interesting, I'll have to take a look at your suggestions, thank you for the quick response! As an aside, do you think it is at all possible to implement an element that would take any number of priors -- as long as they possess the same type (and not saying that CRA needs to do it)? I have been experimenting with it locally, but I am unfortunately limited by my knowledge of the package and Scala.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/p2t2/figaro/issues/730#issuecomment-345833662, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AFJOJTeeKm_lTo0DWMcTzjvXOSmmMru_ks5s4etwgaJpZM4Qk0X4.

davidhamblin commented 6 years ago

Brian,

I've locally implemented a new form of CPD that takes a List[T] for priors and clauses, which included adding a new CPDList class to CPD.scala, new ApplyList function to Apply.scala, apply function in Tuple.scala, a case in LazyValues.scala, and a makeFactors constructor in ApplyFactory.scala. This should allow for any number of priors, given that they are of the same type. So far I've tested it against the Figaro implementations of CPD1, CPD2, and CPD3. It seems to suit my purposes, but would you be interested in this code at all?

bruttenberg commented 6 years ago

Hi David,

I think we'd definitely be interested in that. Do you have it in a branch we can look at?