0
$\begingroup$

If I write in Mathematica

NExpectation[-1 + 0.2*Z + b1 - b0*b4*a0^4*a2, {
  a0 \[Distributed] BetaDistribution[5, 4], 
  a2 \[Distributed] UniformDistribution[{-5, 20}], 
  b0 \[Distributed] UniformDistribution[{-5, 20}], 
  b1 \[Distributed] NormalDistribution[0, 2], 
  b4 \[Distributed] BernoulliDistribution[0.35], 
  Z \[Distributed] NormalDistribution[0, 2]}]

I obtain a numerical value (an expectation). This works well. However, if I change the last a2 by Z, I obtain no result:

NExpectation[-1 + 0.2*Z + b1 - b0*b4*a0^4*Z, {
  a0 \[Distributed] BetaDistribution[5, 4], 
  a2 \[Distributed] UniformDistribution[{-5, 20}], 
  b0 \[Distributed] UniformDistribution[{-5, 20}], 
  b1 \[Distributed] NormalDistribution[0, 2], 
  b4 \[Distributed] BernoulliDistribution[0.35], 
  Z \[Distributed] NormalDistribution[0, 2]}]

Mathematica just returns my input copied. What is happening here?

$\endgroup$
3
$\begingroup$

It works if you use the option Method

NExpectation[-1 + 0.2*Z + b1 - 
  b0*b4*a0^4*Z, {a0 \[Distributed] BetaDistribution[5, 4], 
  b0 \[Distributed] UniformDistribution[{-5, 20}], 
  b1 \[Distributed] NormalDistribution[0, 2], 
  b4 \[Distributed] BernoulliDistribution[0.35], 
  Z \[Distributed] NormalDistribution[0, 2]},
 Method -> {"MonteCarlo", "RandomSeed" -> 0}]

(* -0.999432 *)

Alternatively,

dist = TransformedDistribution[
   -1 + 0.2*Z + b1 - b0*b4*a0^4*Z,
   {a0 \[Distributed] BetaDistribution[5, 4], 
    b0 \[Distributed] UniformDistribution[{-5, 20}], 
    b1 \[Distributed] NormalDistribution[0, 2], 
    b4 \[Distributed] BernoulliDistribution[0.35], 
    Z \[Distributed] NormalDistribution[0, 2]}];

Mean@dist

(* -1. *)

SeedRandom[0];
data = RandomVariate[dist, {1000}];

Mean@data

(* -0.96742 *)

Histogram[data, Automatic, "PDF"]

enter image description here

For both of your expectations you can use Expectation rather than NExpectation

Expectation[-1 + 0.2*Z + b1 - 
  b0*b4*a0^4*a2, {a0 \[Distributed] BetaDistribution[5, 4], 
  a2 \[Distributed] UniformDistribution[{-5, 20}], 
  b0 \[Distributed] UniformDistribution[{-5, 20}], 
  b1 \[Distributed] NormalDistribution[0, 2], 
  b4 \[Distributed] BernoulliDistribution[0.35], 
  Z \[Distributed] NormalDistribution[0, 2]}]

(* -3.78409 *)

Expectation[-1 + 0.2*Z + b1 - 
  b0*b4*a0^4*Z, {a0 \[Distributed] BetaDistribution[5, 4], 
  b0 \[Distributed] UniformDistribution[{-5, 20}], 
  b1 \[Distributed] NormalDistribution[0, 2], 
  b4 \[Distributed] BernoulliDistribution[0.35], 
  Z \[Distributed] NormalDistribution[0, 2]}]

(* -1. *)

EDIT: Expectation can handle the general symbolic case

Expectation[
  C[0] + C[1]*Z + C[2]*b1 - C[3]*b0*b4*a0^4*Z, 
   {a0 \[Distributed] BetaDistribution[a0α, a0β], 
   b0 \[Distributed] UniformDistribution[{b0min, b0max}], 
   b1 \[Distributed] NormalDistribution[b1μ, b1σ], 
   b4 \[Distributed] BernoulliDistribution[b4p], 
   Z \[Distributed] NormalDistribution[Zμ, Zσ]}] // FullSimplify

(* C[0] + Zμ C[1] + b1μ C[2] - (
 a0α (1 + a0α) (2 + a0α) (3 + a0α) (b0max + 
    b0min) b4p Zμ C[3])/(
 2 (a0α + a0β) (1 + a0α + a0β) (2 + a0α + 
    a0β) (3 + a0α + a0β)) *)

% /. {C[0] -> -1, C[1] -> 1/5, C[2] -> 1, C[3] -> 1,
  a0α -> 5, a0β -> 4, b0min -> -5, b0max -> 20,
  b1μ -> 0, b1σ -> 2, b4p -> 35/100, Zμ -> 0, Zσ -> 2}

(* -1 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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