petitRADTRANS.math
==================

.. py:module:: petitRADTRANS.math

.. autoapi-nested-parse::

   Stores useful mathematical functions.



Functions
---------

.. autoapisummary::

   petitRADTRANS.math.bayes_factor2sigma
   petitRADTRANS.math.box_car_conv
   petitRADTRANS.math.calculate_uncertainty
   petitRADTRANS.math.compute_resolving_power
   petitRADTRANS.math.feature_scaling
   petitRADTRANS.math.gaussian_kernel_1d
   petitRADTRANS.math.gaussian_weights_running
   petitRADTRANS.math.longitude2phase
   petitRADTRANS.math.median_filter
   petitRADTRANS.math.mean_uncertainty
   petitRADTRANS.math.median_uncertainties
   petitRADTRANS.math.normalize
   petitRADTRANS.math.phase2longitude
   petitRADTRANS.math.prt_resolving_space
   petitRADTRANS.math.resolving_space
   petitRADTRANS.math.running_mean
   petitRADTRANS.math.savgol_filter
   petitRADTRANS.math.sigma2bayes_factor


Module Contents
---------------

.. py:function:: bayes_factor2sigma(bayes_factor: float) -> [float, jax.typing.ArrayLike]

   Convert a Bayes factor, or "evidence", into a sigma significance. For Bayes factor higher than exp(25), the function
   is approximated with a square root function.
   Note: sometimes algorithms return the "log-evidence", or ln(z). The Bayes factor is z.
   Source: Benneke et al. 2013 https://iopscience.iop.org/article/10.1088/0004-637X/778/2/153
   Molliere part by Paul Mollière.
   :param bayes_factor: Bayes factor (aka "evidence")
   :return: sigma significance


.. py:function:: box_car_conv(array: jax.typing.ArrayLike, points: jax.typing.ArrayLike) -> jax.typing.ArrayLike

.. py:function:: calculate_uncertainty(derivatives: jax.typing.ArrayLike, uncertainties: jax.typing.ArrayLike, covariance_matrix: jax.typing.ArrayLike = None) -> [jax.typing.ArrayLike, None]

   Calculate the uncertainty of a function f(x, y, ...) with uncertainties on x, y, ... and Pearson's correlation
   coefficients between x, y, ...
   The function must be (approximately) linear with its variables within the uncertainties of said variables.
   For independent variables, set the covariance matrix to identity.
   Uncertainties can be asymmetric, in that case for N variables, use a (N, 2) array for the uncertainties.
   Asymmetric uncertainties are handled **the wrong way** (see source 2), but it is better than nothing.

   Sources:
       1. https://en.wikipedia.org/wiki/Propagation_of_uncertainty
       2. https://phas.ubc.ca/~oser/p509/Lec_10.pdf
       3. http://math.jacobs-university.de/oliver/teaching/jacobs/fall2015/esm106/handouts/error-propagation.pdf
   Args:
       derivatives:
           Partial derivatives of the function with respect to each variable (df/dx, df/dy, ...)
       uncertainties:
           Uncertainties of each variable (either a 1D-array or a 2D-array containing - and + unc.)
       covariance_matrix:
           Covariance matrix between the variables, by default set to the identity matrix

   Returns:
       A size-2 array containing the - and + uncertainties of the function


.. py:function:: compute_resolving_power(array: jax.typing.ArrayLike) -> float

   Compute the mean resolving power of an array.

   Args:
       array:
           A 1-D array.

   Returns:
       The mean resolving power of the array.


.. py:function:: feature_scaling(array: jax.typing.ArrayLike, min_value: float = 0.0, max_value: float = 1.0) -> jax.typing.ArrayLike

   Bring all values of array between a min and max value.

   Args:
       array: array to normalize
       min_value: target minimum value
       max_value: target maximum value

   Returns:
       The normalized array with values between min_value and max_value.


.. py:function:: gaussian_kernel_1d(sigma: float, radius: int) -> jaxtyping.Float[jaxtyping.Array,  2*{radius}]

   JAX version of the gaussian kernel to be used in the gaussian_filter1d function.

   Note that the implementation parallels that of scipy.ndimage at
   https://github.com/scipy/scipy/blob/v1.16.2/scipy/ndimage/_filters.py#L656.


.. py:function:: gaussian_weights_running(sigmas: jax.typing.ArrayLike, truncate: float = 4.0, max_radius: int = 20) -> jax.typing.ArrayLike

   Compute 1D Gaussian convolution kernels for an array of standard deviations.

   This version supports JIT compilation by avoiding arrays whose shape depends on
   values computed at runtime. To guarantee static shapes when JIT-compiling,
   pass an integer ``max_radius`` (half-kernel size). If ``max_radius`` is not
   provided the radius will be computed in Python (non-JIT use).

   Args:
       sigmas:
           1-D array of standard deviations for each position (shape: (N,)).
       truncate:
           Truncate the filter at this many standard deviations (used only when
           ``max_radius`` is None).
       max_radius:
           Optional integer radius to force a static kernel size of
           ``2*max_radius+1``. Recommended for JIT compilation.

   Returns:
       weights: array of shape (N, 2*radius+1) containing the normalized
           Gaussian kernel for each sigma.


.. py:function:: longitude2phase(longitude: float)

.. py:function:: median_filter(spectrum, size=10, mode='nearest')

   JAX-compatible implementation of scipy.ndimage.median_filter for 1D arrays.
   Applies a median filter with a sliding window of specified size.

   Args:
       spectrum: 1D array to filter
       size: Window size (will be made odd if even)
       mode: Padding mode ('nearest', 'reflect', 'constant', etc.)
              - 'nearest': pad with edge values
              - 'reflect': mirror values at boundaries
              - 'constant': pad with zeros
              - 'edge': same as 'nearest'

   Returns:
       Filtered spectrum with same shape as input


.. py:function:: mean_uncertainty(uncertainties: jax.typing.ArrayLike) -> float

   Calculate the uncertainty of the mean of an array.

   Args:
       uncertainties: individual uncertainties of the averaged array

   Returns:
       The uncertainty of the mean of the array


.. py:function:: median_uncertainties(uncertainties: jax.typing.ArrayLike) -> float

   Calculate the uncertainty of the median of an array.

   Demonstration:
       uncertainty ~ standard deviation = sqrt(variance) = sqrt(V)
       V_mean / V_median = 2 * (N - 1) / (pi * N); (see source)
       => V_median = V_mean * pi * N / (2 * (N - 1))
       => uncertainty_median = uncertainty_mean * sqrt(pi * N / (2 * (N - 1)))

   Source:
       https://mathworld.wolfram.com/StatisticalMedian.html

   Args:
       uncertainties: individual uncertainties of the median of the array

   Returns:
       The uncertainty of the median of the array


.. py:function:: normalize(array: jax.typing.ArrayLike, axis: int = None) -> jax.typing.ArrayLike

.. py:function:: phase2longitude(phase: float, rad2deg: bool = False)

.. py:function:: prt_resolving_space(start, stop, resolving_power) -> jax.typing.ArrayLike

   Return numbers evenly spaced at the specified resolving power.

   Args:
       start:
           The starting value of the sequence.
       stop:
           The end value of the sequence.
       resolving_power:
           Resolving power of the sample

   Returns:
       Samples spaced following the specified resolving power.



.. py:function:: resolving_space(start, stop, resolving_power)

.. py:function:: running_mean(x: jax.typing.ArrayLike, n: int) -> jax.typing.ArrayLike

.. py:function:: savgol_filter(spectrum, window_length=11, polyorder=3, mode='nearest')

   JAX-compatible Savitzky-Golay filter for 1D signals.
   Fits a polynomial to a sliding window and evaluates it at the center.

   Note: Edge behavior differs slightly from scipy due to JAX padding mode limitations.
   The interior points (away from edges) match scipy's output to machine precision.

   Args:
       spectrum: 1D input array
       window_length: Length of the filter window (will be made odd if even)
       polyorder: Order of the polynomial to fit (must be < window_length)
       mode: Padding mode ('nearest'→'edge', 'reflect', 'constant', etc.)
             At edges: JAX uses 'edge' padding which may differ slightly from scipy's 'nearest'

   Returns:
       Filtered spectrum (same shape as input)


.. py:function:: sigma2bayes_factor(sigma: float) -> float

   Convert a sigma significance into a Bayes factor, or "evidence".
   Note: sometimes algorithms return the "log-evidence", or ln(z). The Bayes factor is z.
   Source: Benneke et al. 2013 https://iopscience.iop.org/article/10.1088/0004-637X/778/2/153
   :param sigma: sigma significance
   :return: Bayes factor (aka "evidence")


