regressioninc.math module#

Helpful arithmetic functions for complex numbers

regressioninc.math.transpose(arr: ndarray) ndarray[source]#

Hermitian transpose of an array (transpose and complex conjugation)

Parameters:

arr (np.ndarray) – Input array

Returns:

Hermitian transpose

Return type:

np.ndarray

regressioninc.math.sum_r2(X: ndarray, y: ndarray, coef: ndarray) float[source]#

Calculate sum of square residuals

Parameters:
  • X (np.ndarray) – The regressors

  • y (np.ndarray) – The observations

  • coef (np.ndarray) – The estimated coefficients

Returns:

The sum of square residuals

Return type:

float

regressioninc.math.geometric_median(arr: ndarray) complex128[source]#

Calculate the geometric median for a 1-D array of complex numbers

https://en.wikipedia.org/wiki/Geometric_median

Parameters:

arr (np.ndarray) – A 1-D complex array

Returns:

The geometric median

Return type:

np.complex_

Examples

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from regressioninc.math import geometric_median
>>> arr = np.array([1 - 1j, -1 - 1j, -1 + 1j, 1 + 1j])
>>> med = geometric_median(arr)
>>> print(np.round(med, 6))
0j
>>> plt.figure() 
>>> plt.scatter(arr.real, arr.imag, c="b", marker="x") 
>>> plt.scatter(med.real, med.imag, c="r", marker="o") 
>>> plt.tight_layout() 
>>> plt.show() 

(Source code, png, hires.png, pdf)

_images/regressioninc-math-1.png
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from regressioninc.math import geometric_median
>>> arr = np.array([3 - 2j, -4 - 9j, 2 + 1j, -3 + 6j])
>>> med = geometric_median(arr)
>>> print(np.round(med, 6))
(1.444444+0.074074j)
>>> plt.figure() 
>>> plt.scatter(arr.real, arr.imag, c="b", marker="x") 
>>> plt.scatter(med.real, med.imag, c="r", marker="o") 
>>> plt.tight_layout() 
>>> plt.show() 

(Source code, png, hires.png, pdf)

_images/regressioninc-math-2.png