Fitting with polynomials

This notebook is available at arjunsavel/cortecs

Fitting with polynomials#

For our final fitting tutorial, we’ll review how to fit opacity functions with polynomials. This approach is generally the least performant of the three currently supported, though it likely performs well enough for very smoothly varying opacity functions (such as collisionally induced absorption).

Setting up the objects#

[1]:
import numpy as np

import cortecs
from cortecs.opac.opac import *
from cortecs.fit.fit import *
from cortecs.fit.fit_pca import *
from cortecs.eval.eval import *

We’ll be using the same Opac object as in the Quickstart.

[2]:
T_filename = "temperatures.npy"
P_filename = "pressures.npy"
wl_filename = "wavelengths.npy"

cross_sec_filename = "absorb_coeffs_C2H4.npy"

load_kwargs = {
    "T_filename": T_filename,
    "P_filename": P_filename,
    "wl_filename": wl_filename,
}
opac_obj = Opac(cross_sec_filename, loader="platon", load_kwargs=load_kwargs)

Now, we instantiate a Fitter with the polynomial method.

[3]:
fitter = Fitter(opac_obj, method="polynomial")
fitter
[3]:
<cortecs.fit.fit.Fitter at 0x2a0e82a10>
[4]:
fitter.fit()
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4616/4616 [00:08<00:00, 537.31it/s]

Let’s use an Evaluator to see how well we captured the opacity function.

[5]:
evaluator = Evaluator(opac_obj, fitter)
[6]:
temperature = 300.0
pressure = 100
wavelength = 2.99401875e-05

evaluator.eval(pressure, temperature, wavelength)
[6]:
Array(1.3991368e-05, dtype=float32)

Accuracy-wise, this doesn’t perform that well. This performance hit is partially because this opacity zeros out in many corners of parameter space, and the polynomial fit can’t account for those sharp edges.

Let’s check the evaluation time, as well.

[7]:
%%timeit

evaluator.eval(temperature, pressure, wavelength)
103 µs ± 889 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

This is a bit slower than some of the other methods, too.