charles-river-analytics / figaro

Figaro Programming Language and Core Libraries
Other
757 stars 153 forks source link

Modelling question #460

Closed toncho11 closed 8 years ago

toncho11 commented 9 years ago

I have a transportation model which is kind of Bayes network with 3 nodes: "Calendar","Previous Traffic" and "Traffic".

"Traffic" depends on both "Calendar" and "PreviousTraffic". So I set Figaro's CPD for "Traffic" based on the values of the other two. My objective is to predict the traffic by providing both "Calendar" and "PreviousTraffic" as evidence.

1) Does this simple model makes sense for prediction?

2) Is evidence provided in a good way? I intend to provide the current traffic as "PreviousTraffic", the "Calendar" is always observable and I would expect to get a reasonable value for "Traffic" which is my prediction.

2) So I have two options to train it: a) I have the values of the 3 components from historical data. So I suppose I can do a frequency counting and I provide these to the Figaro's CPD function:

val Traffic = RichCPD(Calendar, PreviousTraffic, (1, 2) -> Flip or other distribution, ... ... And here comes the confusion. I use discrete values to describe "Calendar" and "PreviousTraffic" and "Traffic". Let's say [1,2,3] for all. So for example I count how many times I have (1,2)-(Calendar,PreviousTraffic) corresponding for example to (1)-(Traffic) and I divide to the number of all other combinations of the values of the 3 variables and that way I get the probability I need. Is that correct? But then Flip is only for two values (true,false) so I will need another element in the RichCPD.

b) I use "model parameter learning" as explained in the documentation.

Thank you in advance. Understanding this correctly is crucial to understanding everything. Maybe you could make a new example based on this scenario.

bruttenberg commented 9 years ago

Hi,

Thanks for your questions.

With regard to the overall model, I don't know the specifics of what you're trying to do, but it sounds like it makes sense. You're saying the probability of traffic is conditioned upon some calendar variable and some previous traffic. Something like this would usually be accomplished using an explicitly represented dynamic model (one that models temporal processes). However, if you're still getting familiar with Figaro, I think this way of doing it is dine.

I can't say I quite understand your specific question though. Is it possible for you to rephase it or provide some code?

Sorry I can't be of more help, but if you provide some more details I might be able to guide you.

Brian

toncho11 commented 9 years ago

Thank you for your quick response. My question is how to convert my database to probabilities and fill the CPD function. It is supposed to be simple.

Example:

1 2 3 1 2 3 1 1 2 2 2 3 2 2 3 2 2 3 2 2 3 1 1 1

These are the values of my tree variables "PreviousTraffic", "Weather" and "CurrentTraffic" in this order. How do I put this in the CPD function? Combination 1 2 3 is repeated 2 times and the rows are 8, so the probability of outcome 3 having that "PreviousTraffic", "Weather" are 1 and 2 is 2/8 = 1/4.

So for 2 2 3 we have 4 repetitions against 8 rows, so the probability of "CurrentTraffic"=3 having "PreviousTraffic", "Weather" being 2 2 is 4/8 = 1/2

val CurrentTraffic = RichCPD(Calendar, PreviousTraffic, (1, 2) -> 0.25 (value 3) (2, 2) -> 0.5 (value 3)

Can you finish the above RichCPD function for me incorporating the probabilities explained above? These are not boolean values T/F as it was in the documentation, so I am a bit confused.

bruttenberg commented 9 years ago

Sorry for the slow response.

I see your question. So for a CPD, you are trying to define the probability of an outcome (CurrentTraffic), conditioned on values of parent variables (PreviousTraffic, Calendar). That is, for each combination of values for PreviousTraffic and Calendar, you need to create a distribution that defines the CurrentTraffic.

So, you would declare the CPD as:

val CurrentTraffic = CPD(Calendar, PreviousTraffic, (1, 2) -> Constant(3), (2, 2) -> Constant(3), (1, 1) -> Select(0.5 -> 2, 0.5 -> 1) (2, 1) -> ??? )

Note that I didn’t put anything for (2,1) because you haven’t specified an outcome for that. This CPD defines probabilities for each value of Calendar and PreviousTraffic; you would then define the probability distributions for each of the Calendar and PreviousTraffic and variables. Does that make sense?

Brian

From: toncho11 [mailto:notifications@github.com] Sent: Monday, May 25, 2015 7:50 AM To: p2t2/figaro Cc: Brian Ruttenberg Subject: Re: [figaro] Modelling question (#460)

Thank you for your quick response. My question is how to convert my database to probabilities and fill the CPD function. It is supposed to be simple.

Example:

1 2 3 1 2 3 1 1 2 2 2 3 2 2 3 2 2 3 2 2 3 1 1 1

These are the values of my tree variables "PreviousTraffic", "Weather" and "CurrentTraffic" in this order. How do I put this in the CPD function? Combination 1 2 3 is repeated 2 times and the rows are 8, so the probability of outcome 3 having that "PreviousTraffic", "Weather" are 1 and 2 is 2/8 = 1/4.

So for 2 2 3 we have 4 repetitions against 8 rows, so the probability of "CurrentTraffic"=3 having "PreviousTraffic", "Weather" being 2 2 is 4/8 = 1/2

val CurrentTraffic = RichCPD(Calendar, PreviousTraffic, (1, 2) -> 0.25 (value 3) (2, 2) -> 0.5 (value 3)

Can you finish the above RichCPD function for me incorporating the probabilities explained above? These are not boolean values T/F as it was in the documentation, so I am a bit confused.

— Reply to this email directly or view it on GitHubhttps://github.com/p2t2/figaro/issues/460#issuecomment-105216836.

toncho11 commented 9 years ago

Thanks! I think this clarifies the confusion.

Two more small questions:

1) Suppose that "PreviousTraffic" and "Calendar" are observed. Then the output of the CPD will be directly what has been already defined in the CPD. Right? So a probabilistic programming language will be useful when at least one of the parent variables is represented by a distribution (not observed). Right?

2) Can we instead of constructing the CPD (as shown above) use the "Learning Model Paramaters From Data 8" in Figaro's tutorial. In both cases we are trying to build a distribution based on the data, right? For example we use the Dirichlet?