Ordinary least-squares regression#

An example of ordinary least squares regression with complex values. The results well be benchmarked against scipy linear regression which supports complex numbers.

import numpy as np
import matplotlib.pyplot as plt
from regressioninc.linear.models import add_intercept, OLS

Create a linear complex-valued problem.

params = np.array([-13 + 17j])
intercept = 5 - 7j
X = np.arange(-5, 5).reshape(10, 1)
y = X * params + intercept

fig = plt.figure()
plt.scatter(y, X)
plt.xlabel("Independent variable")
plt.ylabel("Dependent variable")
plt.tight_layout()
fig.show()
eg 01 ordinary least squares
/home/docs/checkouts/readthedocs.org/user_builds/regressioninc/envs/develop/lib/python3.10/site-packages/matplotlib/collections.py:192: ComplexWarning: Casting complex values to real discards the imaginary part
  offsets = np.asanyarray(offsets, float)

Run

X = add_intercept(X)
model = OLS()
model.fit(X, y)
print(model.estimate.params)
[[-13.+17.j]
 [  5. -7.j]]

Total running time of the script: (0 minutes 0.220 seconds)

Gallery generated by Sphinx-Gallery