Bengt - 2 years ago 256

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?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**