__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. =================================================
  3. Orthogonal distance regression (:mod:`scipy.odr`)
  4. =================================================
  5. .. currentmodule:: scipy.odr
  6. Package Content
  7. ===============
  8. .. autosummary::
  9. :toctree: generated/
  10. Data -- The data to fit.
  11. RealData -- Data with weights as actual std. dev.s and/or covariances.
  12. Model -- Stores information about the function to be fit.
  13. ODR -- Gathers all info & manages the main fitting routine.
  14. Output -- Result from the fit.
  15. odr -- Low-level function for ODR.
  16. OdrWarning -- Warning about potential problems when running ODR.
  17. OdrError -- Error exception.
  18. OdrStop -- Stop exception.
  19. polynomial -- Factory function for a general polynomial model.
  20. exponential -- Exponential model
  21. multilinear -- Arbitrary-dimensional linear model
  22. unilinear -- Univariate linear model
  23. quadratic -- Quadratic model
  24. Usage information
  25. =================
  26. Introduction
  27. ------------
  28. Why Orthogonal Distance Regression (ODR)? Sometimes one has
  29. measurement errors in the explanatory (a.k.a., "independent")
  30. variable(s), not just the response (a.k.a., "dependent") variable(s).
  31. Ordinary Least Squares (OLS) fitting procedures treat the data for
  32. explanatory variables as fixed, i.e., not subject to error of any kind.
  33. Furthermore, OLS procedures require that the response variables be an
  34. explicit function of the explanatory variables; sometimes making the
  35. equation explicit is impractical and/or introduces errors. ODR can
  36. handle both of these cases with ease, and can even reduce to the OLS
  37. case if that is sufficient for the problem.
  38. ODRPACK is a FORTRAN-77 library for performing ODR with possibly
  39. non-linear fitting functions. It uses a modified trust-region
  40. Levenberg-Marquardt-type algorithm [1]_ to estimate the function
  41. parameters. The fitting functions are provided by Python functions
  42. operating on NumPy arrays. The required derivatives may be provided
  43. by Python functions as well, or may be estimated numerically. ODRPACK
  44. can do explicit or implicit ODR fits, or it can do OLS. Input and
  45. output variables may be multidimensional. Weights can be provided to
  46. account for different variances of the observations, and even
  47. covariances between dimensions of the variables.
  48. The `scipy.odr` package offers an object-oriented interface to
  49. ODRPACK, in addition to the low-level `odr` function.
  50. Additional background information about ODRPACK can be found in the
  51. `ODRPACK User's Guide
  52. <https://docs.scipy.org/doc/external/odrpack_guide.pdf>`_, reading
  53. which is recommended.
  54. Basic usage
  55. -----------
  56. 1. Define the function you want to fit against.::
  57. def f(B, x):
  58. '''Linear function y = m*x + b'''
  59. # B is a vector of the parameters.
  60. # x is an array of the current x values.
  61. # x is in the same format as the x passed to Data or RealData.
  62. #
  63. # Return an array in the same format as y passed to Data or RealData.
  64. return B[0]*x + B[1]
  65. 2. Create a Model.::
  66. linear = Model(f)
  67. 3. Create a Data or RealData instance.::
  68. mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2))
  69. or, when the actual covariances are known::
  70. mydata = RealData(x, y, sx=sx, sy=sy)
  71. 4. Instantiate ODR with your data, model and initial parameter estimate.::
  72. myodr = ODR(mydata, linear, beta0=[1., 2.])
  73. 5. Run the fit.::
  74. myoutput = myodr.run()
  75. 6. Examine output.::
  76. myoutput.pprint()
  77. References
  78. ----------
  79. .. [1] P. T. Boggs and J. E. Rogers, "Orthogonal Distance Regression,"
  80. in "Statistical analysis of measurement error models and
  81. applications: proceedings of the AMS-IMS-SIAM joint summer research
  82. conference held June 10-16, 1989," Contemporary Mathematics,
  83. vol. 112, pg. 186, 1990.
  84. """
  85. # version: 0.7
  86. # author: Robert Kern <robert.kern@gmail.com>
  87. # date: 2006-09-21
  88. from ._odrpack import *
  89. from ._models import *
  90. from . import _add_newdocs
  91. # Deprecated namespaces, to be removed in v2.0.0
  92. from . import models, odrpack
  93. __all__ = [s for s in dir()
  94. if not (s.startswith('_') or s in ('odr_stop', 'odr_error'))]
  95. from scipy._lib._testutils import PytestTester
  96. test = PytestTester(__name__)
  97. del PytestTester