Opened 12 years ago

Closed 12 years ago

# Expectation of probability spaces is defined improperly

Reported by: Owned by: weigandt weigandt minor sage-duplicate/invalid/wontfix misc Probability, Expectation N/A

### Description

I'm not sure if anyone really maintains the file

sage/local/lib/python2.6/site-packages/sage/probability/random_variable.py

but there's a definite problem with the way expectation is defined.

```sage: X=frequency_distribution([1,1,1,2,2,2])
sage: X.expectation()
0.500000000000000
```

Instead of weighting values by probabilities the function sums the squares of the probabilities involved. It should be an easy fix, I"ll try to put a patch up soon, if you're used to submitting patches and bored, you'll probably get to it before me.

### comment:1 Changed 12 years ago by jason

I think I understand it a bit differently, but I'm not an expert in statistics. For me, it's easier to understand if we use letters rather than numbers.

```sage: X=frequency_distribution('AAAABBB')
sage: X('A')
0.500000000000000
```

So the value of 'A' is 0.5.

```sage: X.probability_space()('A')
0.500000000000000
```

So the probability of 'A' is also 0.5. Thus summing up the products of the values (0.5 for each of 'A' and 'B') and the probabilities of (also 0.5 for each of 'A' and 'B') gives 0.5.

I don't know if that's the natural way to do things, but I do see some of the reasoning behind the code.

### comment:2 Changed 12 years ago by jason

Maybe this is what you intended?

```sage: P=frequency_distribution([1,1,1,2,2,2])
sage: P
Discrete probability space defined by {1: 0.500000000000000, 2: 0.500000000000000}
sage: X=DiscreteRandomVariable(P, {1:1, 2:2})
sage: X
Discrete random variable defined by {1: 1, 2: 2}
sage: X.expectation()
1.50000000000000
```

Maybe the confusion is that DiscreteProbabilitySpace? inherits from DiscreteRandomVariable? (and so inherits the expectation method from DiscreteRandomVariable?)? Again, I'm not a statistician, so I'm not sure what's natural.

### comment:3 Changed 12 years ago by jason

This is more clear, at least in my mind:

```sage: S='AAABBB'
sage: P=frequency_distribution(S)
sage: P
Discrete probability space defined by {'A': 0.500000000000000, 'B': 0.500000000000000}
sage: X=DiscreteRandomVariable(P, {'A':1, 'B':2})
sage: X.expectation()
1.50000000000000
```

That says we want a probability space that calculates the frequency over the set 'AB' of the letters in the string 'AAABBB'. So we first define the probability space. Then we create a random variable by assigning the value 1 to 'A' and 2 to 'B'. Then we can ask for the expectation of the random variable, which uses the probability space and our mapping of S to real numbers to correctly calculate the expectation.

I guess the confusing thing before was that the value of 1 was the frequency of 1, not the actual number 1.

### comment:4 Changed 12 years ago by weigandt

• Resolution set to invalid
• Status changed from new to closed

Oh, oops! Sorry for being confused here. Maybe this could have better documentation, but I think there's already a ticket open for that. So I suppose this should be closed.

Thanks Jason for showing me what I was trying to do!

### comment:5 Changed 12 years ago by mvngu

• Milestone changed from sage-4.7 to sage-duplicate/invalid/wontfix
Note: See TracTickets for help on using tickets.