Coverage for algebra/regression.py: 21%

1"""

2 Regression

3==========

4

5 Various objects to perform regression:

6

7- :func:`colour.algebra.least_square_mapping_MoorePenrose`: *Least-squares*

8 mapping using *Moore-Penrose* inverse.

9

10 References

11----------

12- :cite:`Finlayson2015` : Finlayson, G. D., MacKiewicz, M., & Hurlbert, A.

13 (2015). Color Correction Using Root-Polynomial Regression. IEEE

14 Transactions on Image Processing, 24(5), 1460-1470.

15 doi:10.1109/TIP.2015.2405336

16"""

17

18 from __future__ import annotations

19

20 import typing

21

22 import numpy as np

23

24 if typing.TYPE_CHECKING:

25 from colour.hints import ArrayLike, NDArrayFloat

26

27 __author__ = "Colour Developers"

28 __copyright__ = "Copyright 2013 Colour Developers"

29 __license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"

30 __maintainer__ = "Colour Developers"

31 __email__ = "colour-developers@colour-science.org"

32 __status__ = "Production"

33

34 __all__ = [

35 "least_square_mapping_MoorePenrose",

36]

37

38

39 def least_square_mapping_MoorePenrose(y: ArrayLike, x: ArrayLike) -> NDArrayFloat:

40 """

41 Compute the *least-squares* mapping from dependent variable :math:`y` to

42 independent variable :math:`x` using *Moore-Penrose* inverse.

43

44 Parameters

45 ----------

46 y

47 Dependent and already known :math:`y` variable.

48 x

49 Independent :math:`x` variable(s) values corresponding with :math:`y`

50 variable.

51

52 Returns

53 -------

54 :class:`numpy.ndarray`

55 *Least-squares* mapping matrix.

56

57 References

58 ----------

59 :cite:`Finlayson2015`

60

61 Examples

62 --------

63 >>> prng = np.random.RandomState(2)

64 >>> y = prng.random_sample((24, 3))

65 >>> x = y + (prng.random_sample((24, 3)) - 0.5) * 0.5

66 >>> least_square_mapping_MoorePenrose(y, x) # doctest: +ELLIPSIS

67 array([[ 1.0526376..., 0.1378078..., -0.2276339...],

68 [ 0.0739584..., 1.0293994..., -0.1060115...],

69 [ 0.0572550..., -0.2052633..., 1.1015194...]])

70 """

71

72 y = np.atleast_2d(y)

73 x = np.atleast_2d(x)

74

75 return np.dot(np.transpose(x), np.linalg.pinv(np.transpose(y)))