Joint expectations in Python or R - Cross Validated


$x$ , $y$ bivariate distributed $n(0,1)$ correlation coefficient of $p$.

is there way find expectation of $f(x)*g(y)$ - let $f(x)$ function of $x$ integrable differentiable

$e[f(x)*g(y)]$? $g(y) = 1$, if $y>c$. $g(y) = 0$, otherwise

is there way in python? or in other language r?

i aware of multivariate_normal() function under scipy.stats.

import scipy.stats st  = st.multivariate_normal() 

is there way can use combined other method?

it great if can me analytical solution also. example can take $$ f(x) = exp(x)$$

you can rather approximate expected value of $f(x)g(y)$ using simulation. instance, here r code simulate expected value of $x^2e^y$ $\rho = 0.2$ using 1 million samples:

# parameters calculation f <- function(x) x^2 g <- function(y) exp(y) rho <- 0.2  # approximate expectation library(mvtnorm) set.seed(144) simulated <- rmvnorm(1e6, c(0, 0), rbind(c(1, rho), c(rho, 1))) mean(f(simulated[,1]) * g(simulated[,2])) # [1] 1.713411 

similar code can approximate expectation in python:

import numpy np np.random.seed(144) simulated = np.random.multivariate_normal([0, 0], [[1, 0.2], [0.2, 1]], 1000000) print(np.average(simulated[:,0] ** 2 * np.exp(simulated[:,1]))) # 1.70735583203 

analytically, expectation given following expression, based on bivariate normal pdf:

$$ \int_{-\infty}^\infty \int_{-\infty}^\infty f(x)g(y)\frac{1}{2\pi\sqrt{1-\rho^2}}\exp\bigg[-\frac{x^2-2\rho xy + y^2}{2(1-\rho^2)}\bigg] dxdy $$

whether has analytical solution depends on functions $f$ , $g$.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -