Bengt - 1 year ago 144

Python Question

I need to calculate cohen's d to determine the effect size of an experiment. Is there any implementation in a sound library I could use? If not, what would be a good implementation?

Answer

Since Python3.4, you can use the `statistics`

module for calculating spread and average metrics. With that, Cohen's d can be calculated easily:

```
from statistics import mean, stdev
from math import sqrt
# test conditions
c0 = [2, 4, 7, 3, 7, 35, 8, 9]
c1 = [i * 2 for i in c0]
cohens_d = (mean(c0) - mean(c1)) / (sqrt((stdev(c0) ** 2 + stdev(c1) ** 2) / 2))
print(cohens_d)
```

Output:

```
-0.5567679522645598
```

So we observe a medium effect.

Source (Stackoverflow)