# Beginner issues: Expectation uses numerical evaluation for simple function

**URL:** <https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246>\
**Category:** Questions\
**Tags:** statistics-and-probability\
**Created:** [June 21, 2023, 8:36am UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246 "2023-06-21T08:36:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deportavolacotogna](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@deportavolacotogna](https://community.wolfram.com/u/deportavolacotogna)\
**Post date:** [June 21, 2023, 8:36am UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/1 "2023-06-21T08:36:25Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 21, 2023, 10:49am UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/2 "2023-06-21T10:49:57Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![deportavolacotogna](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@deportavolacotogna](https://community.wolfram.com/u/deportavolacotogna)\
**Post date:** [June 21, 2023, 2:08pm UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/3 "2023-06-21T14:08:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 21, 2023, 2:46pm UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/4 "2023-06-21T14:46:11Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 21, 2023, 3:16pm UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/5 "2023-06-21T15:16:49Z")

</div>

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]

```

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 21, 2023, 3:24pm UTC](https://community.wolfram.com/t/beginner-issues-expectation-uses-numerical-evaluation-for-simple-function/22246/6 "2023-06-21T15:24:19Z")

</div>

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]

```
