Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

My question involves extending the functionality of Integrate over specific integrals in the most generic manner.

Specifically, is it possible to "hack into" Integrate so as a to add to the "grammar" of analytic integration a new family of integrals which the user would have provided (in the spirit of ProbabilityDistribution which allows the user to include his own PDF into the existing tools).

The idea is that Mathematica would be able to identify when such integrals pop up and replace it by the given definition. The specific example I have in mind involves the integral of MultinormalDistribution over square subregions of the integration domain. That is to say I would like to attach to either MultiNormalDistribution, or Expectation, (or Integrate) the following rule

Expectation[Boole[(x>a)&&(y>b)], {x,y} ~Distributed~
    MultinormalDistribution[{0,0},{{1,c},{c,1}}]] = FF[a,b,c]

(where FF is a given function of a, b, and c which could be tabulated).

@JM suggested using TagSet or TagSetDelayed, but the difficulty is to decide to which function it should be associated with, and with what efficiency, given that it would be unwise to temper with build in symbols like Integrate?

Attempt

As a simpler toy problem I tried

Unprotect[Integrate]; 
Integrate /: Integrate[f[x], {x, a_, b_}] = F[a, b] 
Protect[Integrate];

for which Integrate[f[x],{x,1,2}] returns F[1,2]

But for instance Integrate[2 f[x], {x, 1, 2}] is not evaluated, which suggests that my new (possibly admittedly dangerous) definition has not been taken as a global rule for integration.

More relevantly, I tried

Unprotect[Expectation];
Expectation[ Boole[x_ > a_ && y_ > b_], {x_, y_}~Distributed~
    MultinormalDistribution[{0, 0}, {{1, c_}, {c_, 1}}]] = FF[a, b, c]
Protect[Expectation];

so that

Expectation[ 
 Boole[x > 1 && y > 2], {x, y}~Distributed~
  MultinormalDistribution[{0, 0}, {{1, 1/3}, {1/3, 1}}]] 

returns FF[1,2,1/3]

(after a short while).

Questions

  1. What is the best way to write down the definition? Assign it to MultinormalDistribution, Expectation, Integrate?

  2. How to make sure it is "integrated" by Mathematica so that when attempting, e.g. to compute the expectation of d*Boole[x>a && y>b] it would return d*FF[a,b,c] (or the expectation of u+Boole[x>a && y>b] the Expectation of u + FF[a,b,c])? More specifically, how to make sure Mathematica tries to transform a given integral into a form which could match the new definition?

share|improve this question
    
    
As it happens, the 2D example I give below has an "analytic" solution in terms of OwenT cf math.stackexchange.com/questions/153421/… It remains interesting to extend it e.g. to 3D. –  chris Jun 6 '12 at 11:45
    
Dilapidating your fortune? –  belisarius Oct 8 '12 at 0:56
    
@belisarius very nouveau riche isn't it? I am actually really interested in the answer! (and what's the point being virtually rich :-) –  chris Oct 8 '12 at 5:38

1 Answer 1

At first, overriding Integrate[] is not per-se dangerous. Nevertheless, you should make sure that it gives the correct results by implementing a few unit tests (for example check, that Integrate[2 f[x], {x,1,2}] truly results in 2F[1,2], as you'd expect.

Second, the pattern matching for Integrate[] is a bit more subtle: The rule you have given will not match 2 f[x] as argument for integrate, but only functions depending on the integration variable without any prefactors.

A rule you could use for that purpose would be Integrate[c_ f_[x_], {x_, a_, b_}] := c F[a, b] /; FreeQ[c, x], which checks whether c depends on x. You will also need a few other rules for multiplication, etc. It's important to note here that c___ does not work as a pattern, as (schematically) f[x] is not the same as (no prefactor) f[x].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.