Metadata-Version: 2.4
Name: roxy-regression
Version: 1.0.1
Summary: Regression and Optimisation with X and Y errors
Author: Deaglan J. Bartlett, Harry Desmond
License-Expression: MIT
Project-URL: Homepage, https://github.com/DeaglanBartlett/roxy
Project-URL: Documentation, https://roxy.readthedocs.io
Project-URL: Source, https://github.com/DeaglanBartlett/roxy
Project-URL: Issues, https://github.com/DeaglanBartlett/roxy/issues
Project-URL: Paper, https://arxiv.org/abs/2309.00948
Keywords: regression,x-errors,mcmc,jax,numpyro,bayesian
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.12
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: jax
Requires-Dist: numpyro
Requires-Dist: jaxopt
Requires-Dist: scikit-learn
Requires-Dist: matplotlib
Requires-Dist: corner
Requires-Dist: getdist
Requires-Dist: arviz>=1.0.0
Requires-Dist: fgivenx>=2.5.2
Provides-Extra: mpi
Requires-Dist: mpi4py; extra == "mpi"
Provides-Extra: docs
Requires-Dist: sphinx>=5.0; extra == "docs"
Requires-Dist: myst-parser; extra == "docs"
Requires-Dist: sphinx-rtd-theme; extra == "docs"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: all
Requires-Dist: roxy-regression[docs,mpi,test]; extra == "all"
Dynamic: license-file

roxy
----

:roxy: Regression and Optimisation with X and Y errors
:Authors: Deaglan J. Bartlett and Harry Desmond
:Homepage: https://github.com/DeaglanBartlett/roxy
:Documentation: https://roxy.readthedocs.io/en/latest/

.. image:: https://readthedocs.org/projects/roxy/badge/?version=latest
  :target: https://roxy.readthedocs.io/en/latest/?badge=latest
  :alt: Documentation Status

.. image:: https://img.shields.io/badge/astro.CO-arXiv%32309.00948-B31B1B.svg
  :target: https://arxiv.org/abs/2309.00948

.. image:: https://github.com/DeaglanBartlett/roxy/actions/workflows/coverage.yml/badge.svg
  :target: https://github.com/DeaglanBartlett/roxy/actions/workflows/coverage.yml
  :alt: Unit tests

.. image:: https://img.shields.io/codecov/c/github/DeaglanBartlett/roxy
  :target: https://app.codecov.io/gh/DeaglanBartlett/roxy
  :alt: Coverage

\

.. image:: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-linux.yml/badge.svg
  :target: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-linux.yml
  :alt: Build Status

.. image:: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-mac.yml/badge.svg
  :target: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-mac.yml
  :alt: Build Status MacOS

.. image:: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-windows.yml/badge.svg
  :target: https://github.com/DeaglanBartlett/roxy/actions/workflows/build-windows.yml
  :alt: Build Status Windows

About
=====

``roxy`` (Regression and Optimisation with X and Y errors) is a python package for 
fitting a function to data (regression) where the data have both x and y errors, using Markov Chain Monte Carlo (MCMC).
The common approach for this problem is to use a
Gaussian likelihood with a mean given by :math:`f(x_{\rm obs}, \theta)` and a variance
:math:`\sigma_y^2 + f^\prime(x_{\rm obs}, \theta)^2 \sigma_x^2`, but this ignores the underlying
distribution of true x values and thus gives biased results. Instead, this package allows
one to use the MNR (Marginalised Normal Regression) method which does not exhibit such 
biases. 
A range of likelihoods are available in ``roxy``, however we recommed you use the following one in
these situations:

- If you have :math:`x` and :math:`y` errors and DO wish to infer intrinsic scatter: use ``method='mnr'``.
- If you have :math:`x` and :math:`y` errors and DO NOT wish to infer intrinsic scatter: use ``method='prof'``.
- If you only have :math:`y` errors: use ``method='prof'`` or ``method='unif'`` (they are identical in this case).

The code uses automatic differentiation enabled by jax to both sample the
likelihood using Hamiltonian Monte Carlo and to compute the derivatives 
required for the likelihood. We employ the NUTS method implemented in ``numpyro``
for fast sampling. For the galaxy cluster example in the MNR paper 
(which contains over 250 data points), a single chain run on a laptop performs 
approximately 3500 iterations per second, such that a chain with 700 warm-up
steps and 5000 samples takes approximately 1.6 seconds to sample, and gives
over 3500 effective samples for each of the parameters, with Gelman Rubin statistics 
equal to unity within less than 0.01. Given its efficiency and simplicity to use (one 
need to just define the function of interest, the parameters to sample and their
prior ranges), we advocate for its use not just in the presence of x errors,
but also without these.

As well as returning posterior samples and allowing likelihood computations
(which can be integrated into the user's larger code), ``roxy`` is interfaced with 
``arviz`` to produce trace plots, ``corner`` and ``getdist`` to make two-dimensional
posterior plots, and ``fgivenx`` for posterior predictive plots. See below for 
the relevant citations one must use if one uses these modules.


Installation
============

Installing
~~~~~~~~~~

``roxy`` requires python 3.12 or later; we test it with python 3.12 and 3.13.
To install the latest release from PyPI, run

.. code:: bash

	pip install roxy-regression

Note that the package is called ``roxy-regression`` on PyPI, but is imported
as ``roxy`` (e.g. ``from roxy.regressor import RoxyRegressor``).
Installing it also installs the runtime dependencies: numpy, scipy, jax, numpyro, jaxopt,
scikit-learn, matplotlib, corner, getdist, arviz and fgivenx.

We recommend installing into a fresh virtual environment, for example

.. code:: bash

	python3 -m venv roxy_env
	source roxy_env/bin/activate
	pip install roxy-regression

Optional extras
~~~~~~~~~~~~~~~

If one wants to reproduce the results of the ``roxy`` `paper <https://arxiv.org/abs/2309.00948>`_,
one needs to run the example given in ``examples/bias_example.py``, which requires
``mpi4py``. This is not installed by default, and needs MPI to be installed beforehand:

MacOS:

.. code:: bash

	brew install open-mpi

Ubuntu:

.. code:: bash

	sudo apt-get install openmpi-bin libopenmpi-dev

after which ``mpi4py`` can be installed alongside ``roxy`` with

.. code:: bash

	pip install "roxy-regression[mpi]"

Development version
~~~~~~~~~~~~~~~~~~~

To install the development version from GitHub, with the tools needed to build the
documentation and run the tests, run

.. code:: bash

	git clone https://github.com/DeaglanBartlett/roxy.git
	pip install -e "roxy[docs,test]"

and then run the tests with ``pytest roxy/tests``.


Licence and Citation
====================

Users are required to cite the ``roxy`` `paper <https://arxiv.org/abs/2309.00948>`_, for which the following bibtex can be used

.. code:: bibtex

  @ARTICLE{roxy,
    author = {{Bartlett}, D.~J. and {Desmond}, H.},
    title = "{Marginalised Normal Regression: Unbiased curve fitting in the presence of x-errors}",
    eprint = "2309.00948",
    archivePrefix = "arXiv",
    primaryClass = "stat.ME",
    doi = "10.21105/astro.2309.00948",
    journal = "Open J. Astrophys.",
    volume = "6",
    pages = "astro.2309.00948",
    year = "2023"
  }

and are encourgaed to cite the ``numpyro`` papers

.. code:: bibtex

  @ARTICLE{numpyro1,
	title={Composable Effects for Flexible and Accelerated Probabilistic Programming in NumPyro},
	author={Phan, Du and Pradhan, Neeraj and Jankowiak, Martin},
	journal={arXiv preprint arXiv:1912.11554},
	year={2019}
    }


.. code:: bibtex

  @ARTICLE{numpyro2,
	author    = {Eli Bingham and
	       Jonathan P. Chen and
	       Martin Jankowiak and
	       Fritz Obermeyer and
	       Neeraj Pradhan and
	       Theofanis Karaletsos and
	       Rohit Singh and
	       Paul A. Szerlip and
	       Paul Horsfall and
	       Noah D. Goodman},
	title     = {Pyro: Deep Universal Probabilistic Programming},
	journal   = {J. Mach. Learn. Res.},
	volume    = {20},
	pages     = {28:1--28:6},
	year      = {2019},
	url       = {http://jmlr.org/papers/v20/18-403.html}
    }

Additionally, if you use the function ``roxy.plotting.posterior_predictive_plot``, then, as this used the ``fgivenx`` `package <https://fgivenx.readthedocs.io/en/latest/?badge=latest>`_, you must cite

.. code:: bibtex

   @article{fgivenx,
       doi = {10.21105/joss.00849},
       url = {http://dx.doi.org/10.21105/joss.00849},
       year  = {2018},
       month = {Aug},
       publisher = {The Open Journal},
       volume = {3},
       number = {28},
       author = {Will Handley},
       title = {fgivenx: Functional Posterior Plotter},
       journal = {The Journal of Open Source Software}
   }


We also provide simple routines to plot posterior distribtuions with ``roxy.plotting.triangle_plot``. If you use ``module="corner"`` with this function, please cite

.. code:: bibtex

   @article{corner,
	doi = {10.21105/joss.00024},
	url = {https://doi.org/10.21105/joss.00024},
	year  = {2016},
	month = {jun},
	publisher = {The Open Journal},
	volume = {1},
	number = {2},
	pages = {24},
	author = {Daniel Foreman-Mackey},
	title = {corner.py: Scatterplot matrices in Python},
	journal = {The Journal of Open Source Software}
    }

and if you use ``module="getdist"``, please cite

.. code:: bibtex

   @article{getdist,
      author         = "Lewis, Antony",
      title          = "{GetDist: a Python package for analysing Monte Carlo
                        samples}",
      year           = "2019",
      eprint         = "1910.13970",
      archivePrefix  = "arXiv",
      primaryClass   = "astro-ph.IM",
      SLACcitation   = "%%CITATION = ARXIV:1910.13970;%%",
      url            = "https://getdist.readthedocs.io"
     }

MIT License

Copyright (c) 2023--2026 Deaglan John Bartlett

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


Contributors
============
Below is a list of contributors to this repository.

`Deaglan Bartlett <https://github.com/DeaglanBartlett>`_ (CNRS & Sorbonne Université, Institut d’Astrophysique de Paris and Astrophysics)

`Harry Desmond <https://github.com/harrydesmond>`_ (Institute of Cosmology & Gravitation, University of Portsmouth)

Documentation
=============

The documentation for this project can be found
`at this link <https://roxy.readthedocs.io/>`_

Acknowledgements
================
DJB is supported by the Simons Collaboration on "Learning the Universe."

HD is supported by a Royal Society University Research Fellowship (grant no. 211046).
