Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does numpy.std() use abs()?

I checked the numpy library and found the following definition for the standard deviation in numpy:

std = sqrt(mean(abs(x - x.mean())**2))

Why is the abs() function used? - Because mathematically the square of a number will be positive per definition.

So I thought:

abs(x - x.mean())**2 == (x - x.mean())**2
like image 790
Kevin Wallis Avatar asked Sep 09 '26 07:09

Kevin Wallis


1 Answers

The square of a real number is always positive, but this is not true for complex numbers.

A very simple example: j**2=-1

A more complex (pun intended) example: (3-2j)**2=(5-12j)

From documentation:

Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.

Note: Python uses j for the imaginary unit, while mathematicians uses i.

like image 182
klutt Avatar answered Sep 10 '26 20:09

klutt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!