  Neural networks are being extensively used for modelling data, especially in
the case where no likelihood can be formulated. Although in the case of X-ray
spectral fitting, the likelihood is known, we aim to investigate the neural
networks ability to recover the model parameters but also their associated
uncertainties, and compare its performance with standard X-ray spectral
fitting, whether following a frequentist or Bayesian approach. We apply
Simulation-Based Inference with Neural Posterior Estimation (SBI-NPE) to X-ray
spectra. We train a network with simulated spectra, and then it learns the
mapping between the simulated spectra and their parameters and returns the
posterior distribution. The model parameters are sampled from a predefined
prior distribution. To maximize the efficiency of the training of the neural
network, yet limiting the size of the training sample to speed up the
inference, we introduce a way to reduce the range of the priors, either through
a classifier or a coarse and quick inference of one or multiple observations.
SBI-NPE is demonstrated to work equally well as standard X-ray spectral
fitting, both in the Gaussian and Poisson regimes, both on simulated and real
data, yielding fully consistent results in terms of best fit parameters and
posterior distributions. The inference time is comparable to or smaller than
the one needed for Bayesian inference. On the other hand, once properly
trained, an amortized SBI-NPE network generates the posterior distributions in
no time. We show that SBI-NPE is less sensitive to local minima trapping than
standard fit statistic minimization techniques. We find that the neural network
can be trained equally well on dimension-reduced spectra, via a Principal
Component Decomposition, leading to a shorter inference time. Neural posterior
estimation thus adds up as a complementary tool for X-ray spectral fitting
(abridged).
