Re: Pattern matching - new user question
Robert Dodier <robert.dodier <at> gmail.com>
2009-10-01 15:15:46 GMT
On 10/1/09, Nathaniel Virgo <nathanielvirgo <at> gmail.com> wrote:
> I'm a new user of Maxima and I'm interested in using it for calculations in
> Bayesian probability theory and information theory. I'm trying to get my
> head around how to implement new simplifier rules, in order to be able to
> manipulate probability expressions.
Well, this is a topic near & dear to my heart, so I am glad to try to help ....
> (%i1) infix("|",50,50);
> (%o1) |
> "(a|b)" is to be read as P(a|b), or the probability of a given b. This is
> my preferred notation for Bayesian probability theory.
> Since we're talking about Bayesian probability theory, a and b stand for
> statements of logic that may or may not be true.
> (%i2) matchdeclare([AA,BB,CC],all);
> (%o2) done
> (%i3) defrule(bayes, (AA|BB and CC), (BB|AA and CC)*(AA|CC)/(BB|CC));
> (%o3) bayes:AA | BB and CC->((AA | CC)*(BB | AA and CC))/BB | CC
> The above is Bayes' theorem as a maxima rule. The following works fine:
Well, you can also define "|" as an ordinary function, which
might have more easily predictable consequences.
e.g. "|"(ll, rr) := <stuff involving args(rr) and args(ll) ...>
> (%i4) bayes( a | b and c);
> (%o4) ((a | c)*(b | a and c))/b | c
> ...but it doesn't want to match subexpressions:
> (%i5) bayes( (a | b and c) * x);
> (%o5) false
Right --- the rule function itself matches only an exact match.
I suppose this isn't clearly documented; sorry about that.
> this doesn't work either:
> (%i6) apply1( (a|b and c)*x, bayes);
> (%o6) (a | b and c)*x
I get a stack overflow or endless loop for this one.
Pretty sure the rule is applied to a|b and c, then b|a and c,
then a|b and c, ad infinitum. That's inherent in your definition,
right? As it's defined, the rule doesn't show a preference for
one form or another. There may be a way to tell Maxima to
apply the rule just once, I don't know at the moment.