Frage

It's easy to get the standard deviation in PHP (e.g. stats_standard_deviation()) or MySQL (STDDEV()). Since the standard deviation is an averaged value valid into both directions (lower and higher), it's impossible to compare the deviation between both directions.

So I'd like to know if there is a native PHP or MySQL function to get these values?

War es hilfreich?

Lösung

I would recommend doing the calculation manually:

select avg(case when col > t.avg then col - t.avgcol end),
       avg(case when col < t.avg then t.avgcol - col end)
from table t cross join
     (select avg(col) as avgcol) as tavg;

This gives the average. If you want the "variance", just square the differences, sum them and take the square root:

select sum(case when col > t.avg then (col - t.avgcol) * (col - t.avgcol) end) / sum(col > t.avg),
       sum(case when col < t.avg then (col - t.avgcol) * (col - t.avgcol) end) / sum(col > t.avg)
from table t cross join
     (select avg(col) as avgcol) as tavg;

I suspect that you are learning to apply these concepts on real data. You might be interested in learning about statistical skew which provides another measure of how "centered" the average is. A good place to start is Wikipedia.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top