__init__.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """
  2. =================================================
  3. Orthogonal distance regression (:mod:`scipy.odr`)
  4. =================================================
  5. .. currentmodule:: scipy.odr
  6. .. deprecated:: 1.17.0
  7. `scipy.odr` is deprecated and will be removed in SciPy 1.19.0. Please use
  8. `pypi.org/project/odrpack/ <https://pypi.org/project/odrpack/>`_
  9. instead.
  10. The following example shows a brief comparison of the APIs::
  11. import numpy as np
  12. import scipy.odr
  13. import odrpack
  14. # Classic "Pearson data" that motivates ODR.
  15. # Errors are in both variables, and if you don't account for this,
  16. # doing a linear fit of X vs. Y or Y vs. X will give you quite
  17. # different results.
  18. p_x = np.array([0., .9, 1.8, 2.6, 3.3, 4.4, 5.2, 6.1, 6.5, 7.4])
  19. p_y = np.array([5.9, 5.4, 4.4, 4.6, 3.5, 3.7, 2.8, 2.8, 2.4, 1.5])
  20. p_sx = np.array([.03, .03, .04, .035, .07, .11, .13, .22, .74, 1.])
  21. p_sy = np.array([1., .74, .5, .35, .22, .22, .12, .12, .1, .04])
  22. # Old-style
  23. # The RealData class takes care of details like turning
  24. # standard-deviation error bars into weights.
  25. p_dat = scipy.odr.RealData(p_x, p_y, sx=p_sx, sy=p_sy)
  26. # Note, parameters come before `x` in scipy.odr
  27. p_mod = scipy.odr.Model(lambda beta, x: beta[0] + beta[1]*x)
  28. p_odr = scipy.odr.ODR(p_dat, p_mod, beta0=[1., 1.])
  29. old_out = p_odr.run()
  30. # New-style
  31. # Parameters come after data, in the new API.
  32. # We must convert the error bars into weights ourselves.
  33. new_out = odrpack.odr_fit(lambda x, beta: beta[0] + beta[1] * x,
  34. p_x, p_y, beta0=np.array([1.0, 1.0]),
  35. weight_x=p_sx**-2, weight_y=p_sy**-2)
  36. assert np.isclose(old_out.beta, new_out.beta).all()
  37. Package Content
  38. ===============
  39. .. autosummary::
  40. :toctree: generated/
  41. Data -- The data to fit.
  42. RealData -- Data with weights as actual std. dev.s and/or covariances.
  43. Model -- Stores information about the function to be fit.
  44. ODR -- Gathers all info & manages the main fitting routine.
  45. Output -- Result from the fit.
  46. odr -- Low-level function for ODR.
  47. OdrWarning -- Warning about potential problems when running ODR.
  48. OdrError -- Error exception.
  49. OdrStop -- Stop exception.
  50. polynomial -- Factory function for a general polynomial model.
  51. exponential -- Exponential model
  52. multilinear -- Arbitrary-dimensional linear model
  53. unilinear -- Univariate linear model
  54. quadratic -- Quadratic model
  55. Usage information
  56. =================
  57. Introduction
  58. ------------
  59. Why Orthogonal Distance Regression (ODR)? Sometimes one has
  60. measurement errors in the explanatory (a.k.a., "independent")
  61. variable(s), not just the response (a.k.a., "dependent") variable(s).
  62. Ordinary Least Squares (OLS) fitting procedures treat the data for
  63. explanatory variables as fixed, i.e., not subject to error of any kind.
  64. Furthermore, OLS procedures require that the response variables be an
  65. explicit function of the explanatory variables; sometimes making the
  66. equation explicit is impractical and/or introduces errors. ODR can
  67. handle both of these cases with ease, and can even reduce to the OLS
  68. case if that is sufficient for the problem.
  69. ODRPACK is a FORTRAN-77 library for performing ODR with possibly
  70. non-linear fitting functions. It uses a modified trust-region
  71. Levenberg-Marquardt-type algorithm [1]_ to estimate the function
  72. parameters. The fitting functions are provided by Python functions
  73. operating on NumPy arrays. The required derivatives may be provided
  74. by Python functions as well, or may be estimated numerically. ODRPACK
  75. can do explicit or implicit ODR fits, or it can do OLS. Input and
  76. output variables may be multidimensional. Weights can be provided to
  77. account for different variances of the observations, and even
  78. covariances between dimensions of the variables.
  79. The `scipy.odr` package offers an object-oriented interface to
  80. ODRPACK, in addition to the low-level `odr` function.
  81. Additional background information about ODRPACK can be found in the
  82. `ODRPACK User's Guide
  83. <https://docs.scipy.org/doc/external/odrpack_guide.pdf>`_, reading
  84. which is recommended.
  85. Basic usage
  86. -----------
  87. 1. Define the function you want to fit against.::
  88. def f(B, x):
  89. '''Linear function y = m*x + b'''
  90. # B is a vector of the parameters.
  91. # x is an array of the current x values.
  92. # x is in the same format as the x passed to Data or RealData.
  93. #
  94. # Return an array in the same format as y passed to Data or RealData.
  95. return B[0]*x + B[1]
  96. 2. Create a Model.::
  97. linear = Model(f)
  98. 3. Create a Data or RealData instance.::
  99. mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2))
  100. or, when the actual covariances are known::
  101. mydata = RealData(x, y, sx=sx, sy=sy)
  102. 4. Instantiate ODR with your data, model and initial parameter estimate.::
  103. myodr = ODR(mydata, linear, beta0=[1., 2.])
  104. 5. Run the fit.::
  105. myoutput = myodr.run()
  106. 6. Examine output.::
  107. myoutput.pprint()
  108. References
  109. ----------
  110. .. [1] P. T. Boggs and J. E. Rogers, "Orthogonal Distance Regression,"
  111. in "Statistical analysis of measurement error models and
  112. applications: proceedings of the AMS-IMS-SIAM joint summer research
  113. conference held June 10-16, 1989," Contemporary Mathematics,
  114. vol. 112, pg. 186, 1990.
  115. """
  116. # version: 0.7
  117. # author: Robert Kern <robert.kern@gmail.com>
  118. # date: 2006-09-21
  119. from ._odrpack import *
  120. from ._models import *
  121. from . import _add_newdocs
  122. # Deprecated namespaces, to be removed in v2.0.0
  123. from . import models, odrpack
  124. __all__ = [s for s in dir()
  125. if not (s.startswith('_') or s in ('odr_stop', 'odr_error'))]
  126. import warnings
  127. msg = ("`scipy.odr` is deprecated as of version 1.17.0 and will be removed in "
  128. "SciPy 1.19.0. Please use `https://pypi.org/project/odrpack/` instead.")
  129. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  130. del warnings
  131. from scipy._lib._testutils import PytestTester
  132. test = PytestTester(__name__)
  133. del PytestTester