Skip to main content
added 309 characters in body
Source Link
Andy Ross
  • 19.4k
  • 2
  • 61
  • 93

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist = 
 ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
   nprobs = Normalize[tally[[All, 2]], Total];
   Piecewise[
    Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@ 
       tally[[All, 1]]}]]
   ], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
 Expectation[
  y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

For completeness, to compute the variance using Expectation it gets a little messy.

mu = Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] pdist];
Table[Expectation[(y - mu)^2 \[Conditioned] 
   x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(*{0, 9/4, 0, 9/4, 0}*)

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist = 
 ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
   nprobs = Normalize[tally[[All, 2]], Total];
   Piecewise[
    Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@ 
       tally[[All, 1]]}]]
   ], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
 Expectation[
  y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist = 
 ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
   nprobs = Normalize[tally[[All, 2]], Total];
   Piecewise[
    Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@ 
       tally[[All, 1]]}]]
   ], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
 Expectation[
  y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

For completeness, to compute the variance using Expectation it gets a little messy.

mu = Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] pdist];
Table[Expectation[(y - mu)^2 \[Conditioned] 
   x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(*{0, 9/4, 0, 9/4, 0}*)
added 551 characters in body
Source Link
Andy Ross
  • 19.4k
  • 2
  • 61
  • 93

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist = 
 ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
   nprobs = Normalize[tally[[All, 2]], Total];
   Piecewise[
    Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@ 
       tally[[All, 1]]}]]
   ], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
 Expectation[
  y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)

Edit: An incredibly inefficient but distribution-based solution in M8 is to use ProbabilityDistribution.

pdist = 
 ProbabilityDistribution[Block[{tally = Tally[data], nprobs},
   nprobs = Normalize[tally[[All, 2]], Total];
   Piecewise[
    Transpose[{nprobs, (And @@ Thread[{x, y} == #]) & /@ 
       tally[[All, 1]]}]]
   ], {x, -3, 3, 1}, {y, -3, 3, 1}]

Table[
 Expectation[
  y \[Conditioned] x == i, {x, y} \[Distributed] pdist], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)
Source Link
Andy Ross
  • 19.4k
  • 2
  • 61
  • 93

Assuming you have version 9 you can do the following.

data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}};

dist = EmpiricalDistribution[data];

Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}]

(*{1, 3/2, 0, 3/2, 1}*)

Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution in version 8. In that case you could code this up yourself as...

Table[Mean[Cases[data, {i, y_} :> y]], {i, -2, 2}]

(* {1, 3/2, 0, 3/2, 1} *)