Skip to content

Commit

Permalink
Finished editting existing workflow pages; added simplify command to …
Browse files Browse the repository at this point in the history
…discrete model workflow
  • Loading branch information
GenevaS committed Jun 30, 2017
1 parent 633441d commit fc2af24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
17 changes: 14 additions & 3 deletions docs/workflow/discrete.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,22 @@ x5 = true
return burglary
````

We can now use the [`hakaru` command](../intro/samplegen.md) to answer our original question: if the alarm is sounding, what are the chances that I am being burglarized? If
you simply call the command `hakaru burglary_disintegrate.hk` in the command line, you will create an infinte stream of samples:
One program transformation that can be called at this stage is the [Hakaru-Maple `simplify` subcommand](../transforms/hk-maple.md). This will call Maple to algebraically
simplify Hakaru models. Calling `hk-maple burglary_disintegrate.hk` produces a new, simpler, model of our burglary alarm scenario:

````nohighlight
weight(19/200000, return true) <|>
weight(9999/1000000, return false)
````

This comment has been minimized.

Copy link
@yuriy0

yuriy0 Jul 3, 2017

I think it would be more useful to show the simplified program before instantiating x to true:

$ disintegrate examples/burglary.hk | hk-maple -
(fn x5 bool:
  (match (x5 == true):
    true:
     weight(19/200000, return true) <|>
     weight(9999/1000000, return false)
    false:
     weight(1/200000, return true) <|>
     weight(989901/1000000, return false)))

Strictly speaking, this answers the original question as posed just as well, but it also answers a second, also interesting, question: "what is the probability you are being burgled, and the alarm doesn't go off?". From the 'real life' point of view, somebody shopping for burglar alarms is probably more interested in the answer to this second question than in the answer to the original one.

This comment has been minimized.

Copy link
@GenevaS

GenevaS Jul 4, 2017

Author Contributor

I tried this example, but it caused the hakaru command to fail with:

hakaru.exe: TODO: evaluatePrimOp{Equal}
CallStack (from HasCallStack):
  error, called at haskell\Language\Hakaru\Sample.hs:340:29 in hakaru-0.4.0-8IG2WtWQS8YBJ7MLg4Aabh:Language.Hakaru.Sample

I have pushed the new Hakaru programs in cef9de0 that uncovered this problem.

This comment has been minimized.

Copy link
@yuriy0

yuriy0 Jul 4, 2017

Indeed, it seems the sampler doesn't implement equality for booleans. I'm guessing it's never been needed.

You have to manually perform the application as it was done before, i.e. x5 = true; ... instead of burglary = fn x5 ...; burglary(true) to sample from it. Alternatively, you can pass the version of burglary_disintegrate_simplify.hk at cef9de0 to hk-maple once again to have it perform the trivial evaluation. If you do, you recover the version of burglary_disintegrate_simplify.hk before cef9de0.

But perhaps this should really be fixed, by implementing equality for bool.

This comment has been minimized.

Copy link
@zaxtax

zaxtax via email Jul 4, 2017

Member

This comment has been minimized.

Copy link
@ccshan

ccshan Jul 5, 2017

Member

Language.Hakaru.Sample should indeed be straightforward to fix, but I haven't done it yet because it would be nice to do it for all datum, not just for bool. (That should still be straightforward but just a bit more code.)

In the meantime, I've made hk-maple remove =true. (ff5e9fe)

This comment has been minimized.

Copy link
@yuriy0

yuriy0 Jul 6, 2017

Equality for bool/pair/either now works (89b501c and 5cb73a3)


Without any further work, we can already see that the alarm sounding is most likely a false alarm. However, the `simplify` transformation will not always produce a clear
result such as this one. For these situations, you must use the [`hakaru` command](../intro/samplegen.md).

How does the `hakaru` command answer our original question? If you simply call the command `hakaru burglary_disintegrate.hk` in the command line, you will create an
infinite stream of samples:

````bash
$ hakaru burglary_disintegrate.hk
$ hakaru burglary_disintegrate_simplify.hk
9.999999999999995e-3 false
9.999999999999995e-3 false
9.999999999999995e-3 false
Expand Down
8 changes: 4 additions & 4 deletions docs/workflow/intro.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# What is the Hakaru Workflow? #

When you write a Hakaru program, you must follow the workflow of Bayesian inference. Bayesian inference is a method of statistical inference where Bayes' theorm is used
to update the probability of a statistical hypothisis as more information becomes available[^1]. Baye's theorm describes the probability of an event based on knowledge of
When you write a Hakaru program, you must follow the workflow of Bayesian inference. Bayesian inference is a method of statistical inference where Bayes' theorem is used
to update the probability of a statistical hypothesis as more information becomes available[^1]. Bayes theorem describes the probability of an event based on knowledge of
event-related conditions[^2].

In Hakaru, the workflow of Bayesian inference appears as *modelling*, *transformation*, and *application* stages[^3]:
Expand All @@ -15,7 +15,7 @@ model conditioning.
on what is to be inferred. At the end of this stage, you can use Hakaru to show the resulting distribution as both a stream of samples and as a term in the Hakaru language.


[^1]: [Baysian inference (Wikipedia)](https://en.wikipedia.org/wiki/Bayesian_inference)
[^2]: [Bayes' theorm (Wikipedia)](https://en.wikipedia.org/wiki/Bayes%27_theorem)
[^1]: [Bayesian inference (Wikipedia)](https://en.wikipedia.org/wiki/Bayesian_inference)
[^2]: [Bayes' theorem (Wikipedia)](https://en.wikipedia.org/wiki/Bayes%27_theorem)
[^3]: P. Narayanan, J. Carette, W. Romano, C. Shan and R. Zinkov, "Probabilistic Inference by Program Transformation in Hakaru (System Description)", Functional and Logic
Programming, pp. 62-79, 2016.
2 changes: 2 additions & 0 deletions examples/documentation/burglary_disintegrate_simplify.hk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
weight(19/200000, return true) <|>
weight(9999/1000000, return false)

0 comments on commit fc2af24

Please sign in to comment.