Message Boards Message Boards

0
|
1692 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Beginner issues: Expectation uses numerical evaluation for simple function

Posted 1 year ago

I am pretty sure this is not the right place to ask this question, so apologies in advance.

I have this simple problem:

dist = NormalDistribution[0, Sqrt[0.3]]; (* Distribution for wz[1] *)

expression = 0.2 + dt (uz[1] + wz[1]); (* Expression for which expectation is computed *)

expectation = Expectation[expression, wz[1] \[Distributed] dist]

Mathematica evaluates this simple expression numerically, and I get the following result: 1. (0.2 + 1. dt uz[1.])

Why? How can I avoid numerical evaluation without using Hold? uz and wz will belong to arrays (uzArray and wzArray) whose size will change every time I run the code.

Furthermore, I am struggling to understand how to use the function Moment with arrays. A simple program as the one that follows:

T = 5;

wzArray = Array[wz, T];

wzArray[[1]] \[Distributed] NormalDistribution[0, Sqrt[0.3]];

Moment[wzArray[[1]], 1]

Gives the following error: Moment::arg1: The first argument wz[1] is expected to be a vector, a matrix, or a distribution.

Thank you very much for any help you could provide!

POSTED BY: Christian Vitale
5 Replies

Hi Gianluca!

For the first question, I want them to be threated like constant. But instead of getting the expected output:

0.2 + dt uz[1]

I am getting the output

  1. (0.2 + 1. dt uz[1.])

I wonder what am I doing wrong.

For what concerns the second question... Is there a way to get to compute Moment[x,1] where x has been defined earlier following a given distribution?

Thank you very much for your kind reply!

POSTED BY: Christian Vitale

I hadn't noticed the dot inside uz[1.], sorry. It is indeed somewhat strange and unexpected. A simpler example:

In[63]:= Expectation[ y[1] + 1.*x, 
 x \[Distributed] NormalDistribution[]]

Out[63]= y[1.]

I suppose that something like the function N is called by the algorithm, and it acts where it shouldn't.

As for the second question, according to the documentation, Moment accepts either a sample or a symbolic distribution, but not a variable name. Instead of x in Moment[x,1] you should provide the distribution of  x.

POSTED BY: Gianluca Gorni

As a workaround, you may try avoiding floating point numbers if you can:

dist = NormalDistribution[0, Sqrt[3/10]]; 
expression = 1/5 + dt (uz[1] + wz[1]); 
expectation = Expectation[expression, wz[1] \[Distributed] dist]
POSTED BY: Gianluca Gorni

A different workaround is to give the variable wz the attribute NHoldFirst:

dist = NormalDistribution[0, Sqrt[0.3]]; 
expression = 0.2 + dt (uz[1] + wz[1]); 
SetAttributes[uz, NHoldFirst];
expectation = Expectation[expression, wz[1] \[Distributed] dist]
POSTED BY: Gianluca Gorni

It takes time to get used to how the language works.

The symbols dt and uz are treated by Expectation as constants, not as random variables, unless you declare an explicit distribution for them. This is similar to what happens for example with Integrate[a x, x], where the parameter a is treated as constant.

The expression wzArray[[1]] is simply wz[1], and you cannot feed it to Moment. Your declaration

wzArray[[1]] \[Distributed] NormalDistribution[0, Sqrt[0.3]]

is not used by Moment. This works:

Moment[NormalDistribution[0, Sqrt[0.3]], 1]

This behaviour is similar to the following:

In[29]:= x == 0
Integrate[1/x, x]

Out[29]= x == 0

Out[30]= Log[x]

where the equation x == 0 is ignored by Integrate.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract