Modelling the glitch#

With asterion.GlitchModel, you can model the helium-II ionization and base of the convective zone glitches given radial mode frequencies. This can be done with modelled or observed modes (with and without observational uncertainty). This notebook will go through both situations.

[1]:
import arviz as az
import matplotlib.pyplot as plt

import asterion as ast
from asterion import GlitchModel, Inference
from asterion.data import example_star

# Plotting style setup
ast.style.use("asterion-light")
/Users/alyttle/.pyenv/versions/asterion/lib/python3.9/site-packages/jax/_src/lib/__init__.py:33: UserWarning: JAX on Mac ARM machines is experimental and minimally tested. Please see https://github.com/google/jax/issues/5501 in the event of problems.
  warnings.warn("JAX on Mac ARM machines is experimental and minimally tested. "

Data#

Import stellar evolutionary track.

[2]:
n = example_star["n"]
nu = example_star["nu"]
nu_max = example_star["nu_max"]
delta_nu = example_star["delta_nu"]

print('nu_max   = {0:.1f} ± {1:.1f} muHz'.format(*nu_max))
print('delta_nu = {0:.2f} ± {1:.2f} muHz'.format(*delta_nu))
nu_max   = 2357.7 ± 23.6 muHz
delta_nu = 111.84 ± 0.10 muHz
[3]:
_, ax = plt.subplots()

ax.plot(nu%delta_nu[0], nu, marker='o', linestyle='none', label='truth')
ax.set_xlabel(r'$\nu$ ' + f'modulo {delta_nu[0]:.2f} ' + r'($\nu/\mathrm{\mu Hz}$)')
ax.set_ylabel(r'$\nu$ ($\mathrm{\mu Hz}$)')
ax.set_title('Echelle plot')
ax.legend();
../_images/tutorials_glitch_4_0.png

Model without observational error#

We can create our model like this. We need to give it a prior for delta_nu and nu_max. Optionally, we can give it a prior for asymptotic fit parameter epsilon, but by default this is fairly uninformative. We can also pass the effective termperature of the star to improve our prior for the glitch acoustic depths.

[4]:
model = GlitchModel(nu_max, delta_nu)
model
INFO[2022-05-06 13:04:44,775]: Unable to initialize backend 'tpu_driver': NOT_FOUND: Unable to find driver in registry given worker:
INFO[2022-05-06 13:04:44,776]: Unable to initialize backend 'gpu': NOT_FOUND: Could not find registered platform with name: "cuda". Available platform names are: Host Interpreter
INFO[2022-05-06 13:04:44,777]: Unable to initialize backend 'tpu': INVALID_ARGUMENT: TpuPlatform is not available.
[4]:
GlitchModel(
 (2357.692764609278, 23.57692764609278),
 (111.8411243661503, 0.1),
 teff=None,
 epsilon=None,
 seed=0
)

Inference#

We setup an inference class with the model and can give it number of warmup steps and samples. The seed is required for reproducibility.

[5]:
infer = Inference(model, n=n, nu=nu, seed=25)

Prior predictive check#

We should do a prior predictive check to see if our model makes sense.

[6]:
infer.prior_predictive()

This plot shows the prior for nu with the dot representing the observed value. It looks good.

[7]:
data = infer.get_data()
data
[7]:
arviz.InferenceData
    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 1000, n: 14, n_pred: 250)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        * n_pred        (n_pred) float64 13.0 13.05 13.1 13.16 ... 25.9 25.95 26.0
      Data variables: (12/27)
          a_cz          (chain, draw) float64 1.533e+05 2.448e+05 ... 8.224e+04
          a_he          (chain, draw) float64 5.208e-06 4.702e-05 ... 0.0003259
          b_he          (chain, draw) float64 4.625e-09 6.692e-08 ... 5.688e-08
          cz_amplitude  (chain, draw) float64 0.03142 0.05075 ... 0.01322 0.01539
          cz_nu_max     (chain, draw) float64 0.02742 0.04472 ... 0.01324 0.01537
          delta_nu      (chain, draw) float64 111.9 111.8 111.9 ... 111.6 111.8 111.9
          ...            ...
          nu_max        (chain, draw) float64 2.364e+03 2.34e+03 ... 2.313e+03
          nu_pred       (chain, draw, n_pred) float64 1.597e+03 ... 3.156e+03
          phi_cz        (chain, draw) float64 2.202 0.688 3.082 ... 2.708 -2.846
          phi_he        (chain, draw) float64 0.3414 0.3522 3.117 ... -2.236 0.1346
          tau_cz        (chain, draw) float64 0.00273 0.002726 ... 0.002927 0.004263
          tau_he        (chain, draw) float64 0.001132 0.001154 ... 0.001633 0.001112
      Attributes:
          created_at:     2022-05-06T12:04:48.905601
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:  (n: 14)
      Coordinates:
        * n        (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables:
          nu       (n) float64 1.601e+03 1.712e+03 1.823e+03 ... 2.94e+03 3.053e+03
      Attributes:
          created_at:     2022-05-06T12:04:48.908278
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (nu_err_dim_0: 14)
      Coordinates:
        * nu_err_dim_0  (nu_err_dim_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13
      Data variables:
          nu_err        (nu_err_dim_0) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      Attributes:
          created_at:     2022-05-06T12:04:48.908422
          arviz_version:  0.11.4

[8]:
ax = ast.plot_glitch(data, group='prior');
../_images/tutorials_glitch_13_0.png
[9]:
use_alpha = False  # Option to not use alpha channel to improve look of plot
ax = ast.plot_glitch(data, group='prior', use_alpha=use_alpha);
ax = ast.plot_glitch(data, group='prior', kind="helium", ax=ax, use_alpha=use_alpha);
ax = ast.plot_glitch(data, group='prior', kind="bcz", ax=ax, use_alpha=use_alpha);
../_images/tutorials_glitch_14_0.png
[10]:
ast.plot_echelle(data, group='prior');
../_images/tutorials_glitch_15_0.png

Sample posterior#

We can sample from the posterior using one of two methods, 'nested' or 'mcmc'. Nested sampling is better suited to multi-modal solutions over MCMC. Since we are fitting sinusoidal functions, there may be cases where one or more periods fit the data, leading to instability during MCMC. Also, the glitch phase term \(\phi\) is circular, which can make it difficult to deal with the gradient at \(\pm\pi\) for gradient-based MCMC sampling methods. Therefore, we recommend using the nested sampling method, although note that the package on which is is based, ‘jaxns’ is still in early development.

If you want to sample using MCMC, follow the advice of the warning when initialising Inference and modify the distributions for \(\phi\) like so,

import numpyro.distributions as dist
model.he_glitch.phi = dist.VonMises(0.0, 0.1)
model.cz_glitch.phi = dist.VonMises(0.0, 0.1)
[11]:
infer.sample(
    num_samples=2000,
    method='nested',  # default method
    num_live_points=50,  # Should be ~ 50 per expected posterior mode
    depth=5,
)
Running nested sampling using the 'multi_ellipsoid' sampler with 50 live points and 50000 maximum samples...
Completed in 16.3 seconds.

Posterior predictive check#

[12]:
infer.posterior_predictive()

When use nested sampling the inference data may produce some warnings. This is because we save the weighted samples in a custom group which is not defined by arviz. You may ignore these.

[13]:
data = infer.get_data()
data
[13]:
arviz.InferenceData
    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2000, n: 14)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 1994 1995 1996 1997 1998 1999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables: (12/22)
          a_cz          (chain, draw) float64 3.001e+05 3.008e+05 ... 2.993e+05
          a_he          (chain, draw) float64 0.009199 0.008845 ... 0.008538 0.009232
          b_he          (chain, draw) float64 8.153e-07 8.099e-07 ... 8.177e-07
          cz_amplitude  (chain, draw) float64 0.0614 0.06153 0.061 ... 0.06146 0.06122
          cz_nu_max     (chain, draw) float64 0.05537 0.05323 ... 0.05418 0.0537
          delta_nu      (chain, draw) float64 111.8 111.7 111.8 ... 111.8 111.7 111.8
          ...            ...
          nu_bkg        (chain, draw, n) float64 1.601e+03 1.713e+03 ... 3.058e+03
          nu_max        (chain, draw) float64 2.328e+03 2.377e+03 ... 2.361e+03
          phi_cz        (chain, draw) float64 -0.7415 -0.9993 ... -1.031 -0.8768
          phi_he        (chain, draw) float64 1.596 1.455 1.426 ... 1.495 1.459 1.539
          tau_cz        (chain, draw) float64 0.002593 0.002599 ... 0.002602 0.002595
          tau_he        (chain, draw) float64 0.000824 0.0008292 ... 0.0008255
      Attributes:
          created_at:     2022-05-06T12:05:07.376270
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2000, n: 14, n_pred: 250)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 1994 1995 1996 1997 1998 1999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        * n_pred        (n_pred) float64 13.0 13.05 13.1 13.16 ... 25.9 25.95 26.0
      Data variables: (12/17)
          a_cz          (chain, draw) float64 3.001e+05 3.008e+05 ... 2.993e+05
          a_he          (chain, draw) float64 0.009199 0.008845 ... 0.008538 0.009232
          b_he          (chain, draw) float64 8.153e-07 8.099e-07 ... 8.177e-07
          cz_amplitude  (chain, draw) float64 0.0614 0.06153 0.061 ... 0.06146 0.06122
          cz_nu_max     (chain, draw) float64 0.05537 0.05323 ... 0.05418 0.0537
          dnu_cz        (chain, draw, n) float64 0.1074 -0.1018 ... 0.02751 -0.03168
          ...            ...
          nu            (chain, draw, n) float64 1.601e+03 1.712e+03 ... 3.053e+03
          nu_bkg        (chain, draw, n) float64 1.601e+03 1.713e+03 ... 3.058e+03
          nu_bkg_pred   (chain, draw, n_pred) float64 1.601e+03 ... 3.058e+03
          nu_pred       (chain, draw, n_pred) float64 1.601e+03 ... 3.053e+03
          tau_cz        (chain, draw) float64 0.002593 0.002599 ... 0.002602 0.002595
          tau_he        (chain, draw) float64 0.000824 0.0008292 ... 0.0008255
      Attributes:
          created_at:     2022-05-06T12:05:07.381522
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:             (chain: 1, draw: 2327)
      Coordinates:
        * chain               (chain) int64 0
        * draw                (draw) int64 0 1 2 3 4 5 ... 2322 2323 2324 2325 2326
      Data variables:
          logX                (chain, draw) float64 -0.0198 -0.03961 ... -47.34 -48.03
          logL                (chain, draw) float64 -8.709e+08 -9.186e+07 ... 21.59
          lp                  (chain, draw) float64 -8.709e+08 -9.186e+07 ... -8.859
          sampler_efficiency  (chain, draw) float64 1.0 1.0 1.0 ... 1.0 0.05747 1.0
      Attributes:
          created_at:            2022-05-06T12:05:07.378917
          arviz_version:         0.11.4
          method:                nested
          sampler:               multi_ellipsoid
          num_likelihood_evals:  115058
          num_weighted_samples:  2327
          logZ:                  -17.940547466926013
          logZ_err:              0.8287048878549536
          ESS:                   372.75860265695985

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 1000, n: 14, n_pred: 250)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        * n_pred        (n_pred) float64 13.0 13.05 13.1 13.16 ... 25.9 25.95 26.0
      Data variables: (12/27)
          a_cz          (chain, draw) float64 1.533e+05 2.448e+05 ... 8.224e+04
          a_he          (chain, draw) float64 5.208e-06 4.702e-05 ... 0.0003259
          b_he          (chain, draw) float64 4.625e-09 6.692e-08 ... 5.688e-08
          cz_amplitude  (chain, draw) float64 0.03142 0.05075 ... 0.01322 0.01539
          cz_nu_max     (chain, draw) float64 0.02742 0.04472 ... 0.01324 0.01537
          delta_nu      (chain, draw) float64 111.9 111.8 111.9 ... 111.6 111.8 111.9
          ...            ...
          nu_max        (chain, draw) float64 2.364e+03 2.34e+03 ... 2.313e+03
          nu_pred       (chain, draw, n_pred) float64 1.597e+03 ... 3.156e+03
          phi_cz        (chain, draw) float64 2.202 0.688 3.082 ... 2.708 -2.846
          phi_he        (chain, draw) float64 0.3414 0.3522 3.117 ... -2.236 0.1346
          tau_cz        (chain, draw) float64 0.00273 0.002726 ... 0.002927 0.004263
          tau_he        (chain, draw) float64 0.001132 0.001154 ... 0.001633 0.001112
      Attributes:
          created_at:     2022-05-06T12:05:07.386463
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:  (n: 14)
      Coordinates:
        * n        (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables:
          nu       (n) float64 1.601e+03 1.712e+03 1.823e+03 ... 2.94e+03 3.053e+03
      Attributes:
          created_at:     2022-05-06T12:05:07.388996
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (nu_err_dim_0: 14)
      Coordinates:
        * nu_err_dim_0  (nu_err_dim_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13
      Data variables:
          nu_err        (nu_err_dim_0) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      Attributes:
          created_at:     2022-05-06T12:05:07.389138
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2327, n: 14)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 2321 2322 2323 2324 2325 2326
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables: (12/22)
          a_cz          (chain, draw) float64 6.259e+04 5.553e+05 ... 2.979e+05
          a_he          (chain, draw) float64 0.05047 0.008978 ... 0.009692 0.009692
          b_he          (chain, draw) float64 1.444e-07 1.28e-08 ... 8.272e-07
          cz_amplitude  (chain, draw) float64 0.0128 0.1136 0.1193 ... 0.06094 0.06094
          cz_nu_max     (chain, draw) float64 0.01114 0.09885 ... 0.05364 0.05364
          delta_nu      (chain, draw) float64 111.8 112.0 111.7 ... 111.6 111.7 111.7
          ...            ...
          nu_bkg        (chain, draw, n) float64 1.606e+03 1.718e+03 ... 3.055e+03
          nu_max        (chain, draw) float64 2.37e+03 2.37e+03 ... 2.357e+03
          phi_cz        (chain, draw) float64 0.07975 -0.7366 ... -0.9436 -0.9436
          phi_he        (chain, draw) float64 3.094 -1.353 2.291 ... 1.606 1.571 1.571
          tau_cz        (chain, draw) float64 0.002651 0.002752 ... 0.002599 0.002599
          tau_he        (chain, draw) float64 0.0009777 0.0009486 ... 0.0008239
      Attributes:
          created_at:     2022-05-06T12:05:07.391699
          arviz_version:  0.11.4

[14]:
ax = ast.plot_glitch(data)
ax = ast.plot_glitch(data, kind='helium', quantiles=[], observed=False, ax=ax)
ax = ast.plot_glitch(data, kind='bcz', quantiles=[], observed=False, ax=ax)
../_images/tutorials_glitch_22_0.png
[15]:
# You can change the x-axis variable to frequency
ast.plot_glitch(data, kind='helium', x_var="nu");
../_images/tutorials_glitch_23_0.png
[16]:
ast.plot_glitch(data, kind='BCZ');
../_images/tutorials_glitch_24_0.png

You can also plot an echelle diagram to view different aspects of the model.

[17]:
ast.plot_echelle(data);
../_images/tutorials_glitch_26_0.png

Or you could view the model without the glitches.

[18]:
ast.plot_echelle(data, kind='glitchless');
../_images/tutorials_glitch_28_0.png

To quickly view the parameter names you can inspect data or just call the following.

[19]:
ast.get_var_names(data)
[19]:
['a_cz',
 'a_he',
 'b_he',
 'cz_amplitude',
 'cz_nu_max',
 'delta_nu',
 'dnu_cz',
 'dnu_he',
 'epsilon',
 'he_amplitude',
 'he_nu_max',
 'log_a_cz',
 'log_a_he',
 'log_b_he',
 'log_tau_cz',
 'log_tau_he',
 'nu_bkg',
 'nu_max',
 'phi_cz',
 'phi_he',
 'tau_cz',
 'tau_he']

Let’s just plot glitch parameter names.

[20]:
glitch_names = ['log_a_he', 'log_b_he', 'log_tau_he', 'phi_he', 'log_a_cz', 'log_tau_cz', 'phi_cz']
ast.plot_corner(data, var_names=glitch_names);
../_images/tutorials_glitch_32_0.png

How about the background parameters and average glitch amplitudes.

[21]:
ast.plot_corner(data, var_names=['delta_nu', 'epsilon', 'he_amplitude', 'cz_amplitude']);
../_images/tutorials_glitch_34_0.png

We can even plot from the posterior precictive, that is to say the predicted values for a given variable. E.g. 'nu' for the first 5 radial orders.

[22]:
ast.plot_corner(data, group='posterior_predictive', var_names=['nu'], coords={'n': n[:5]}, labelpad=0.15);
../_images/tutorials_glitch_36_0.png

You can also pass data to arviz functions. Note, if using nested sampling, some functions like traceplot or rhat are useless since they are tailored to MCMC methods.

Below, we plot the prior predictive and the posterior for the glitch parameters. We can use asterion.get_labeller to get an arviz labeller to replace variable names with their symbols and units.

[23]:
import arviz as az

labeller = ast.get_labeller(data, var_names=glitch_names)

ax = az.plot_density(data, group='prior_predictive', colors='C0',
                     var_names=glitch_names, hdi_prob=1., labeller=labeller)
ax = az.plot_density(data, group='posterior', colors='C1',
                     var_names=glitch_names, hdi_prob=1., labeller=labeller,
                     ax=ax)
../_images/tutorials_glitch_38_0.png

Saving the inference data#

[24]:
data.to_netcdf('data/inference_data.nc')
[24]:
'data/inference_data.nc'

Generating summary tables#

You can generate a table for the results data for a chosen data dimension(s). E.g. all the data without any dimensions can be obtained like so. By default, the values are rounded using the standard error on the mean as a guide, although you may pass your own dictionary of formatting rules.

[25]:
ast.get_table(data, dims=())
[25]:
a_cz a_he b_he cz_amplitude cz_nu_max delta_nu epsilon he_amplitude he_nu_max log_a_cz log_a_he log_b_he log_tau_cz log_tau_he nu_max phi_cz phi_he tau_cz tau_he
metric
mean 299742.0 0.00936 8.198000e-07 0.06132 0.05383 111.803 1.3319 0.4778 0.2297 5.47673 -2.0299 -6.0863 -2.585594 -3.08392 2360.2 -0.878 1.570 0.002597 0.000824
sd 2397.0 0.00064 1.360000e-08 0.00049 0.00121 0.079 0.0233 0.0160 0.0201 0.00347 0.0295 0.0072 0.000422 0.00288 23.8 0.097 0.127 0.000003 0.000005
16th 297446.0 0.00876 8.075000e-07 0.06085 0.05255 111.714 1.3092 0.4611 0.2095 5.47341 -2.0576 -6.0929 -2.586034 -3.08680 2336.3 -0.971 1.443 0.002594 0.000819
50th 299848.0 0.00932 8.199000e-07 0.06134 0.05376 111.797 1.3326 0.4788 0.2290 5.47690 -2.0306 -6.0862 -2.585544 -3.08395 2360.6 -0.887 1.572 0.002597 0.000824
84th 301949.0 0.00996 8.331000e-07 0.06177 0.05516 111.890 1.3544 0.4937 0.2490 5.47993 -2.0017 -6.0793 -2.585183 -3.08102 2384.4 -0.785 1.694 0.002599 0.000830

You can also format the table using astropy, which makes use of quantity unit metadata.

[26]:
table = ast.get_table(data, dims=(), fmt='astropy')
table
[26]:
Table length=5
metrica_cza_heb_hecz_amplitudecz_nu_maxdelta_nuepsilonhe_amplitudehe_nu_maxlog_a_czlog_a_helog_b_helog_tau_czlog_tau_henu_maxphi_czphi_hetau_cztau_he
uHz3Ms2uHzuHzuHzuHzuHzdex(uHz3)dexdex(Ms2)dex(Ms)dex(Ms)uHzradradMsMs
str4float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64
mean299742.00.009368.198e-070.061320.05383111.8031.33190.47780.22975.47673-2.0299-6.0863-2.585594-3.083922360.2-0.8781.570.002596610.0008243
sd2397.00.000641.36e-080.000490.001210.0790.02330.0160.02010.003470.02950.00720.0004220.0028823.80.0970.1272.52e-065.5e-06
16th297446.00.008768.075e-070.060850.05255111.7141.30920.46110.20955.47341-2.0576-6.0929-2.586034-3.08682336.3-0.9711.4430.002593980.0008188
50th299848.00.009328.199e-070.061340.05376111.7971.33260.47880.2295.4769-2.0306-6.0862-2.585544-3.083952360.6-0.8871.5720.002596910.0008242
84th301949.00.009968.331e-070.061770.05516111.891.35440.49370.2495.47993-2.0017-6.0793-2.585183-3.081022384.4-0.7851.6940.002599060.0008298

Or, we may want a table for all variables with the dimension \(n\) (i.e. the individual modes) and from the posterior predictive group (to include the predicted frequencies for \(n\)).

[27]:
ast.get_table(data, dims=('n',), group='posterior_predictive')
[27]:
dnu_cz dnu_he nu nu_bkg
metric n
mean 13 0.106131 -1.14883 1601.25475 1602.35
14 -0.101699 0.66000 1712.37587 1714.15
15 0.075015 1.10609 1822.86639 1825.95
16 -0.036906 0.28506 1932.24344 1937.76
17 -0.001847 -0.44218 2042.29653 2049.56
... ... ... ... ... ...
84th 22 0.024687 -0.02383 2601.02296 2610.44
23 -0.002523 -0.05540 2713.51305 2722.30
24 -0.014987 -0.01563 2826.40052 2834.15
25 0.028301 0.01535 2939.55975 2946.02
26 -0.031570 0.01410 3052.67195 3057.87

70 rows × 4 columns

Model with observational error#

The example star has frequencies with added noise to simulate observations.

[28]:
nu_obs = example_star["nu_obs"]
nu_err = example_star["nu_err"]

ax = plt.gca()
ax.plot(nu%delta_nu[0], nu, marker='o', linestyle='none', label='truth')
ax.errorbar(nu_obs%delta_nu[0], nu_obs, xerr=nu_err, marker='o',
            linestyle='none', label='observed')

ax.set_xlabel(r'$\nu$ ' + f'modulo {delta_nu[0]:.2f} ' + r'($\nu/\mathrm{\mu Hz}$)')
ax.set_ylabel(r'$\nu$ ($\mathrm{\mu Hz}$)')
ax.set_title('Echelle plot')
ax.legend();
../_images/tutorials_glitch_48_0.png

Inference#

We can add the uncertainty in place like so.

[29]:
# We update the prior to simulate future work on improving prior

import numpyro.distributions as dist

model.he_glitch.log_b = dist.Normal(-6.1, 0.1)
[30]:
infer.nu_err = nu_err
infer.nu = nu_obs
[31]:
infer.sample(
    num_samples=2000,
    method='nested',  # default method
    num_live_points=100,  # Should be ~ 50 per expected posterior mode
    depth=7,
)
Running nested sampling using the 'multi_ellipsoid' sampler with 100 live points and 50000 maximum samples...
Completed in 11.3 seconds.
[32]:
infer.posterior_predictive()
[33]:
data = infer.get_data()
data
[33]:
arviz.InferenceData
    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2000, n: 14)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 1994 1995 1996 1997 1998 1999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables: (12/22)
          a_cz          (chain, draw) float64 2.263e+05 2.32e+05 ... 2.373e+05
          a_he          (chain, draw) float64 0.005282 0.003732 ... 0.005674 0.004146
          b_he          (chain, draw) float64 6.401e-07 5.597e-07 ... 6.259e-07
          cz_amplitude  (chain, draw) float64 0.04629 0.04745 ... 0.08332 0.04853
          cz_nu_max     (chain, draw) float64 0.04194 0.04109 ... 0.07474 0.04238
          delta_nu      (chain, draw) float64 111.8 112.0 111.8 ... 111.7 111.8 111.9
          ...            ...
          nu_bkg        (chain, draw, n) float64 1.6e+03 1.712e+03 ... 3.058e+03
          nu_max        (chain, draw) float64 2.323e+03 2.376e+03 ... 2.366e+03
          phi_cz        (chain, draw) float64 1.807 -2.554 0.7418 ... -1.694 1.029
          phi_he        (chain, draw) float64 0.984 1.812 -1.541 ... -0.007141 -0.8321
          tau_cz        (chain, draw) float64 0.002521 0.006229 ... 0.007279 0.006953
          tau_he        (chain, draw) float64 0.0008544 0.0008289 ... 0.0009202
      Attributes:
          created_at:     2022-05-06T12:05:24.114792
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2000, n: 14, n_pred: 250)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 1994 1995 1996 1997 1998 1999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        * n_pred        (n_pred) float64 13.0 13.05 13.1 13.16 ... 25.9 25.95 26.0
      Data variables: (12/17)
          a_cz          (chain, draw) float64 2.263e+05 2.32e+05 ... 2.373e+05
          a_he          (chain, draw) float64 0.005282 0.003732 ... 0.005674 0.004146
          b_he          (chain, draw) float64 6.401e-07 5.597e-07 ... 6.259e-07
          cz_amplitude  (chain, draw) float64 0.04629 0.04745 ... 0.08332 0.04853
          cz_nu_max     (chain, draw) float64 0.04194 0.04109 ... 0.07474 0.04238
          dnu_cz        (chain, draw, n) float64 0.06968 -0.03756 ... 0.0197 -0.02325
          ...            ...
          nu            (chain, draw, n) float64 1.6e+03 1.712e+03 ... 3.053e+03
          nu_bkg        (chain, draw, n) float64 1.6e+03 1.712e+03 ... 3.058e+03
          nu_bkg_pred   (chain, draw, n_pred) float64 1.6e+03 1.606e+03 ... 3.058e+03
          nu_pred       (chain, draw, n_pred) float64 1.6e+03 1.606e+03 ... 3.052e+03
          tau_cz        (chain, draw) float64 0.002521 0.006229 ... 0.007279 0.006953
          tau_he        (chain, draw) float64 0.0008544 0.0008289 ... 0.0009202
      Attributes:
          created_at:     2022-05-06T12:05:24.125104
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:             (chain: 1, draw: 2510)
      Coordinates:
        * chain               (chain) int64 0
        * draw                (draw) int64 0 1 2 3 4 5 ... 2505 2506 2507 2508 2509
      Data variables:
          logX                (chain, draw) float64 -0.00995 -0.0199 ... -26.91 -27.6
          logL                (chain, draw) float64 -2.983e+04 -9.716e+03 ... -5.175
          lp                  (chain, draw) float64 -2.981e+04 -9.698e+03 ... -9.615
          sampler_efficiency  (chain, draw) float64 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0
      Attributes:
          created_at:            2022-05-06T12:05:24.122262
          arviz_version:         0.11.4
          method:                nested
          sampler:               multi_ellipsoid
          num_likelihood_evals:  162527
          num_weighted_samples:  2510
          logZ:                  -23.240147092317276
          logZ_err:              0.37530567136688314
          ESS:                   707.3429859932048

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 1000, n: 14, n_pred: 250)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        * n_pred        (n_pred) float64 13.0 13.05 13.1 13.16 ... 25.9 25.95 26.0
      Data variables: (12/27)
          a_cz          (chain, draw) float64 1.533e+05 2.448e+05 ... 8.224e+04
          a_he          (chain, draw) float64 5.208e-06 4.702e-05 ... 0.0003259
          b_he          (chain, draw) float64 4.625e-09 6.692e-08 ... 5.688e-08
          cz_amplitude  (chain, draw) float64 0.03142 0.05075 ... 0.01322 0.01539
          cz_nu_max     (chain, draw) float64 0.02742 0.04472 ... 0.01324 0.01537
          delta_nu      (chain, draw) float64 111.9 111.8 111.9 ... 111.6 111.8 111.9
          ...            ...
          nu_max        (chain, draw) float64 2.364e+03 2.34e+03 ... 2.313e+03
          nu_pred       (chain, draw, n_pred) float64 1.597e+03 ... 3.156e+03
          phi_cz        (chain, draw) float64 2.202 0.688 3.082 ... 2.708 -2.846
          phi_he        (chain, draw) float64 0.3414 0.3522 3.117 ... -2.236 0.1346
          tau_cz        (chain, draw) float64 0.00273 0.002726 ... 0.002927 0.004263
          tau_he        (chain, draw) float64 0.001132 0.001154 ... 0.001633 0.001112
      Attributes:
          created_at:     2022-05-06T12:05:24.130092
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:  (n: 14)
      Coordinates:
        * n        (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables:
          nu       (n) float64 1.601e+03 1.712e+03 1.823e+03 ... 2.94e+03 3.053e+03
      Attributes:
          created_at:     2022-05-06T12:05:24.132644
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (nu_err_dim_0: 14)
      Coordinates:
        * nu_err_dim_0  (nu_err_dim_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13
      Data variables:
          nu_err        (nu_err_dim_0) float64 0.5722 0.4164 0.286 ... 0.3386 0.483
      Attributes:
          created_at:     2022-05-06T12:05:24.132790
          arviz_version:  0.11.4

    • <xarray.Dataset>
      Dimensions:       (chain: 1, draw: 2510, n: 14)
      Coordinates:
        * chain         (chain) int64 0
        * draw          (draw) int64 0 1 2 3 4 5 6 ... 2504 2505 2506 2507 2508 2509
        * n             (n) int64 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      Data variables: (12/22)
          a_cz          (chain, draw) float64 8.212e+05 3.393e+05 ... 3.663e+05
          a_he          (chain, draw) float64 0.02791 0.01011 ... 0.003454 0.003764
          b_he          (chain, draw) float64 4.455e-07 7.565e-07 ... 5.682e-07
          cz_amplitude  (chain, draw) float64 0.1679 0.0694 ... 0.08797 0.07492
          cz_nu_max     (chain, draw) float64 0.1463 0.06342 ... 0.07656 0.06587
          delta_nu      (chain, draw) float64 111.9 112.2 112.0 ... 111.7 111.7 111.7
          ...            ...
          nu_bkg        (chain, draw, n) float64 1.777e+03 1.889e+03 ... 3.053e+03
          nu_max        (chain, draw) float64 2.37e+03 2.313e+03 ... 2.358e+03
          phi_cz        (chain, draw) float64 0.7841 2.919 -0.1555 ... -2.8 -2.485
          phi_he        (chain, draw) float64 -2.604 2.496 2.921 ... 0.8728 0.9375
          tau_cz        (chain, draw) float64 0.006266 0.004274 ... 0.002705 0.007095
          tau_he        (chain, draw) float64 0.001537 0.0006683 ... 0.0008622
      Attributes:
          created_at:     2022-05-06T12:05:24.139735
          arviz_version:  0.11.4

[34]:
ast.plot_glitch(data);
../_images/tutorials_glitch_55_0.png
[35]:
ast.plot_glitch(data, kind='He');
../_images/tutorials_glitch_56_0.png
[36]:
ast.plot_glitch(data, kind='CZ');
../_images/tutorials_glitch_57_0.png
[37]:
ast.plot_echelle(data, kind='glitchless');
../_images/tutorials_glitch_58_0.png
[38]:
ast.plot_corner(data, var_names=glitch_names);
../_images/tutorials_glitch_59_0.png
[39]:
ast.plot_corner(data, var_names=['delta_nu', 'epsilon', 'he_amplitude', 'cz_amplitude']);
../_images/tutorials_glitch_60_0.png
[40]:
ast.get_table(data, dims=())
[40]:
a_cz a_he b_he cz_amplitude cz_nu_max delta_nu epsilon he_amplitude he_nu_max log_a_cz log_a_he log_b_he log_tau_cz log_tau_he nu_max phi_cz phi_he tau_cz tau_he
metric
mean 314400.0 0.00461 6.220000e-07 0.0643 0.0565 111.830 1.3137 0.492 0.329 5.484 -2.358 -6.209 -2.298 -3.0561 2358.9 0.18 0.39 0.00544 0.000880
sd 80300.0 0.00146 6.500000e-08 0.0164 0.0146 0.082 0.0225 0.062 0.050 0.110 0.137 0.046 0.186 0.0192 19.2 1.70 1.07 0.00182 0.000039
16th 234900.0 0.00323 5.560000e-07 0.0480 0.0420 111.750 1.2900 0.433 0.278 5.371 -2.491 -6.255 -2.585 -3.0759 2342.3 -1.91 -0.69 0.00260 0.000840
50th 308300.0 0.00444 6.210000e-07 0.0630 0.0552 111.830 1.3154 0.490 0.330 5.489 -2.352 -6.207 -2.200 -3.0569 2358.9 0.32 0.49 0.00631 0.000877
84th 396000.0 0.00605 6.890000e-07 0.0810 0.0707 111.905 1.3361 0.550 0.376 5.598 -2.219 -6.162 -2.158 -3.0369 2376.7 2.09 1.48 0.00695 0.000919
[41]:
data.to_netcdf('data/obs_inference_data.nc')
[41]:
'data/obs_inference_data.nc'
[ ]: