__init__.py 4.2 KB

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