Message Boards Message Boards

Specify a general rule, so that mathematica always evaluate it?

GROUPS:

I want to specify a set of some general mathematical rules regarding probability theory to solve equations with "expectation" and "variance" for random variables. Therefore I want to "teach" mathematica to evaluate the expression

Integrate[p[x], {x, -Infinity, Infinity}] -> 1

always to "1". p[x] should be any arbitrary "Probability Density Function" of "x" with the result, whenever mathematica's evaluation finds the integral in an expression, it should replace it by "1" I tried this with

Simplify[Integrate[p[x], {x, -Infinity, Infinity}] /. Integrate[p[x], {x, -Infinity, Infinity}] :> 1]

and similar expressions and rules, but when I put into the next cell:

Integrate[p[x], {x, -Infinity, Infinity}]

the output is always the Integral itself, but not the expected value of "1".

My final goal is if I specify:

expectation[x_] := Integrate[x*p[x], {x, -Infinity, Infinity}]
expectation[z_] := Integrate[z*p[x], {x, -Infinity, Infinity}] /; FreeQ[z, x] := z /; FreeQ[z, x]

that mathematica recognize "z" as a constant, independant from x, get it out of the Integral and evaluates the remaining Integral to "1" and the final result as "z"

How can I solve such a problem ? Thank you for your support.

Regards Uwe

POSTED BY: uwe moehring
Answer
10 days ago

Uwe,

you can specify an upvalue for p:

p /: Integrate[p[x_], {x_, -Infinity, Infinity}] := 1

Now the only problem is that Integrate does not factor constants out of integrals so your rule will not trigger in the case of z*p[x] in the integral. To make this happen you can define a function to extract constants from integrals (from this stackexchange post)

moveconst[
  x_] := (x /. 
   Integrate[factor_ expr_, {var_, min_, max_}] /; 
     FreeQ[factor, var] :> factor Integrate[expr, {var, min, max}])

and you can use it to pull out constants (or independent variables) and Mathematica will apply the rule above.

Regards,

Neil

POSTED BY: Neil Singer
Answer
10 days ago

Thank you, Neil.

My first trials with your recommended moveconst-function worked very well.

Regards Uwe

POSTED BY: uwe moehring
Answer
10 days ago

Group Abstract Group Abstract