TransitModelFit#

class stellarphot.transit_fitting.TransitModelFit[source]#

Bases: object

Transit model fits to observed light curves.

The underlying transit light curve is computed with pytransit’s RoadRunnerModel using a quadratic limb-darkening law and a circular orbit. The fit is performed with lmfit.

Attributes:
paramslmfit.Parameters

The transit model parameters: t0, period, rp (planet radius in stellar radii), a (orbital radius in stellar radii), inclination (degrees), limb_u1/limb_u2 (quadratic limb-darkening coefficients) and the linear trend coefficients airmass_trend, width_trend and spp_trend. Read or set values, bounds and whether a parameter is fit through the standard lmfit attributes, e.g. mod.params["rp"].value, mod.params["t0"].vary, mod.params["a"].min and, after a fit, mod.params["rp"].stderr.

fit_resultlmfit.minimizer.MinimizerResult or None

Result of the most recent call to fit, including fit statistics like bic and nvarys. None until fit has been run.

times, airmass, width, spp, data, weightsarray-like or None

Independent variables and data for the fit; see the property docstrings.

Notes

The orbit is always circular; there is no eccentricity parameter.

Trend fitting is opt-in: the trend coefficients default to vary=False even when a covariate (airmass, width, sky per pixel) is set. Enable them via TransitModelOptions, directly (e.g. mod.params["airmass_trend"].vary = True) or with compare_detrend_options(apply_best=True).

Examples

Fit a transit, detrending against airmass:

mod = TransitModelFit()
mod.setup_model(t0=2455001.5, depth=12.1, duration=0.15, period=3.5)
mod.times = times
mod.data = fluxes
mod.weights = 1 / flux_errors
mod.airmass = airmass
mod.params["airmass_trend"].vary = True

result = mod.fit()

print(mod.params["rp"].value, mod.params["rp"].stderr)
print(result.bic)

# Which detrending parameters does the BIC favor?
bic_table = mod.compare_detrend_options()

Attributes Summary

airmass

airmass : array-like

data

data : array-like

spp

spp : array-like

times

times : array-like

width

weights : array-like

Methods Summary

compare_detrend_options([apply_best])

Compare, via the Bayesian Information Criterion, fits with every on/off combination of the trend parameters whose covariate is set.

data_light_curve([data, detrend_by])

Function to return data light curve, optionally detrended by one or more parameters.

fit()

Fit the model to the data and update params with the best-fit values and uncertainties.

model_light_curve([at_times, detrend_by])

Calculate the light curve corresponding to the model, optionally detrended by one or more parameters.

setup_model(*[, binned_data, t0, depth, ...])

Configure a transit model for fitting.

Attributes Documentation

airmass#
airmassarray-like

Airmass at each time. Must be set before fitting.

data#
dataarray-like

Observed fluxes. Must be set before fitting.

spp#
spparray-like

Sky per pixel at each time. Must be set before fitting.

times#
timesarray-like

Times at which the light curve is observed. Must be set before fitting.

width#
weightsarray-like

Weights to use for fitting. If not provided, all weights are set to 1.

Methods Documentation

compare_detrend_options(apply_best=False)[source]#

Compare, via the Bayesian Information Criterion, fits with every on/off combination of the trend parameters whose covariate is set.

Each candidate fit runs on a copy of params — with the trends in the combination varying, and the others fixed at zero — so this method does not change the state of the model.

Parameters:
apply_bestbool, optional

If True, apply the lowest-BIC combination to params (each trend in the winning combination is varied, the others are zeroed and fixed) and run fit.

Returns:
astropy.table.Table

One row per combination with a bool column per available trend (airmass, width, spp) and a BIC column, sorted by ascending BIC (best first).

data_light_curve(data=None, detrend_by=None)[source]#

Function to return data light curve, optionally detrended by one or more parameters.

Parameters:
dataarray-like, optional

Data to use for calculating the light curve. If not provided, the data used for fitting will be used.

detrend_bystr or list of str

Parameter(s) to detrend by. If None, no detrending is done. If 'all', all parameters that are set will be used for detrending.

Returns:
dataarray-like

Data light curve, detrended if requested.

fit()[source]#

Fit the model to the data and update params with the best-fit values and uncertainties.

Returns:
lmfit.minimizer.MinimizerResult

The full fit result, also stored as fit_result. Includes fit statistics like bic.

Raises:
ValueError

If the times or the data have not been set. If the fit itself raises, params and fit_result are left untouched.

model_light_curve(at_times=None, detrend_by=None)[source]#

Calculate the light curve corresponding to the model, optionally detrended by one or more parameters.

Parameters:
at_timesarray-like

Times at which to calculate the model. If not provided, the times used for fitting will be used. Because the airmass/width/spp trend covariates are reused as-is, at_times must have the same length as the model’s times; a mismatched length raises ValueError.

detrend_bystr or list of str

Parameter(s) to detrend by. If None, no detrending is done. If 'all', all parameters that are set will be used for detrending.

Returns:
modelarray-like

Model light curve.

setup_model(*, binned_data=None, t0=0, depth=0, duration=0, period=1, inclination=90, airmass_trend=0.0, width_trend=0.0, spp_trend=0.0, model_options=None)[source]#

Configure a transit model for fitting. The duration and depth are used to estimate underlying fit parameters; they are not themselves fit parameters. Any previous parameter settings are reset to their defaults before the new values are applied.

All arguments are keyword-only.

Parameters:
binned_dataastropy.timeseries.BinnedTimeSeries, optional

Binned time series to load the times, data, weights and covariates from.

t0float

Time of the center of the transit. Can be in any units but should be consistent with the units for the period and for the times used for fitting.

depthfloat

Depth of the transit in parts per thousand. If zero (the default), the planet radius rp is left at its default starting value instead of being estimated.

durationfloat

Duration of the transit, in the same units as t0 and period. If zero (the default), the orbital radius a is left at its default starting value instead of being estimated. Must be smaller than the period.

periodfloat

Period of the planet. Should be in the same units as t0 and times used for fitting.

inclinationfloat

Inclination of the orbit, in degrees.

airmass_trendfloat, optional

Coefficient for a linear trend in airmass.

width_trendfloat

Coefficient for a linear trend in stellar width.

spp_trendfloat

Coefficient for a linear trend in sky per pixel.

model_optionsTransitModelOptions, optional

Options for the transit model fit, mapped onto the vary flags (and, for t0, the bounds) of the parameters.

Returns:
None

Sets values for the model parameters.

Raises:
ValueError

If duration is not smaller than period.