# How to compute weighted means and SDs?

I want to calculate volume weighted average price and standard deviation for the following dataset:

Jan Product  A:     q= 100       p=23
Jan Prodouct B:     q=11         p=45
Feb Prodouct B:     q= 55.7      p=60


What formulas should I use?

If you know Stata: Should I use pweights or fweights?

Neither. Your quantities are the weights and they are sometimes non-integer, so they are in Stata terms aweights.

Note: It is sometimes suggested that with fractional weights you can multiply up to get integers, and then use fweights, but that is not necessary. It would work fine for the calculation given, but in some problems it will give quite the wrong answer, essentially whenever sample size is taken literally.

[This is off-topic for CV, as it is essentially about software use, and should be migrated.]

• Thanks for the aweight! I voted to delete my answer just to keep it clean. May 14 '13 at 17:46
• Generous of you! I've edited my answer to reduce the cross-reference. May 14 '13 at 17:47

These are descriptive statistics intended to convey information about price (p) where "volume" (q) apparently describes how much was sold at each price.

When dealing with such weights, it's easiest to compute totals first. Adopting hypothetical units of measurement in order to make the numbers concrete, and pretending that all three records are to be summarized (even though they pertain to two different products), suppose the data give volumes in thousands of units sold per month and the prices are in Euros. Then the totals paid in the three months are

Jan product A: 100 K/month * 1 month * 23 Euros =  2300 000 Euros
Jan product B: 11 K/month * 1 month * 45 Euros =    495 000 Euros
Feb product B: 55.7 K/month * 1 month * 60 Euros = 3342 000 Euros
-----------------------------------------------------------------
Total:                                             6137 000 Euros


The total volume is 100 K/month * 1 month + 11 K/month * 1 month + 55.7 K/month * 1 month = 166.7 K units. Thus, the average price per unit must equal 6137 / 116.7 = 36.81 Euros.

Notice that this is not computed by either the fweights nor the pweights options. (In a now-deleted answer, @Penguin_Knight showed us that the unweighted mean is 42.67 and the fweighted mean is 37.00.)

The standard deviation is similarly computed: the residual prices are (23 - 36.81), (45 - 36.81), and (60 - 36.81). Their weighted mean square is obtained exactly as above: multiply each squared residual by its volume, add them up, and divide by the total volume. The square root of this result, equal to 17.28, is the standard deviation.

These calculations are straightforward to do in Stata or in any statistical software, so I omit the software-specific details.

The weighting options in Stata are wonderful, especially for probability weights, and I have used them: but I never use them without first conducting a detailed check with a calculation like the one here just to make sure I am interpreting the weights correctly. It's usually simple to make such a check by creating a small artificial dataset, most of whose values are zeros. In this way the effect of the weights can immediately be read off the results (and the manual calculations are very easy indeed).