Message Boards Message Boards


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

Posted 1 year ago
2 Replies
3 Total Likes

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

2 Replies


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)

  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.



Posted 1 year ago

Thank you, Neil.

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

Regards Uwe

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract