0
$\begingroup$

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)$?

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

1 Answer 1

3
$\begingroup$

Just a typo

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

Should be

m <- 0.5 -   6*x^2 + 3*x^3
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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