Expectation of a function

I am generating a function of $x$: $m(x)=0.5-6x^2+3x^3$, where $x$ is taken from a uniform distribution: $x\sim U(-1,1)$.

Now, I would like to center the function $m(x)$, so that the expectation of the function $m(x)$ is zero: $Em(x)=0$.

So, since the density of $x$, $f(x)=\frac{1}{2}$ by definition of uniform distribution, $Em(x)=\int_{-1}^{1}\frac{1}{2}(0.5-6x^2+3x^3)dx=-1.5$.

Questions: I expect that when I generate this function in R and calculate its sample average of the function value, the sample mean of this function, $\frac{1}{n}\sum_{j=1}^{n}m(x_j)$, will converge to its expectation, which is -1.5. But this is not the case:

n<-200000
x<-runif(n,-1,1)
m<-0.5-0.6*x^2+3*x^3
mean(m1)
> 0.2996


and this is quite far from -1.5 as its expected value.

I must miss something important here. Am I using a wrong weight to calculate the sample mean of the function $m(x)$?

• Maybe I'm missing something but shouldn't it be: m<-0.5-6*x^2+3*x^3 Commented Feb 6, 2018 at 2:15
• Oh god...thanks, I really didn't capture this typo!
– Rico
Commented Feb 6, 2018 at 2:48
• @AlexanderF. - you may as well post your comment as an answer, since you did figure it out! Commented Feb 6, 2018 at 3:33
• @Rico, please consider accepting (& upvoting) AlexanderF.'s answer if it resolved your issue. Commented Feb 6, 2018 at 15:26

m <- 0.5 - 0.6*x^2 + 3*x^3

m <- 0.5 -   6*x^2 + 3*x^3