# 求两个随机变量的差的绝对值的期望

1. a <= b <= c <= d
2. a <= c < b <= d
3. a <= c <= d < b

double eabs(int a, int b, int c, int d)
{
if (a > c) {swap(a, c); swap(b, d);}
if (b > d)
{
double e_ad_cd = eabs(a, d, c, d);
double p_ad_cd = (double)(d - a + 1) / (b - a + 1);
double e_db_cd = (b - c + 1) / 2.0;
double p_db_cd = (double)(b - d) / (b - a + 1);
return e_ad_cd * p_ad_cd + e_db_cd * p_db_cd;
}
int n = max(b - c + 1, 1);
double e_ab_cd = (c + d - a - b) / 2.0;
double e_cb_cb = (n - 1.0 / n) / 3.0;
double p_cb_cb = (double)n / (b - a + 1) * (double)n / (d - c + 1);
return e_ab_cd + e_cb_cb * p_cb_cb;
}

double eabs(double a, double b, double c, double d)
{
if (a > c) {swap(a, c); swap(b, d);}
if (b > d)
{
double e_ad_cd = eabs(a, d, c, d);
double p_ad_cd = (d - a) / (b - a);
double e_db_cd = (b - c) / 2.0;
double p_db_cd = (b - d) / (b - a);
return e_ad_cd * p_ad_cd + e_db_cd * p_db_cd;
}
double n = max(b - c, 0.0);
double e_ab_cd = (c + d - a - b) / 2.0;
double e_cb_cb = n / 3.0;
double p_cb_cb = n / (b - a) * n / (d - c);
return e_ab_cd + e_cb_cb * p_cb_cb;
}

posted @ 2011-02-02 12:13  atyuwen  阅读(10839)  评论(0编辑  收藏  举报