* do-file for Supplementary exercise 4.71 of IPS7e version 17 /* works also with versions 13-16 */ set obs 5 egen x=fill(540(5)560) * type probabilities into the var2 column rename var2 px * direct implementation of formula generate prod=x*px total(prod) * same thing in one command, puts the mean value in a new column egen meancol=total(x*px) * now for variance and standard deviation egen varcol=total(px*(x-550)^2) generate sdcol=sqrt(varcol) * same thing for temperature in degrees F generate xF=1.8*x+32 egen meanxF=total(xF*px) egen varxF=total(px*(xF-1022)^2) generate sdxF=sqrt(varxF) browse * the nicest approach is however to use the probabilities as weights * frequency weights must be integers, so we convert to percentages generate perc=int(100*px) sum x [fweight=perc] sum xF [fweight=perc]