====
MLGW
====

MLGW (Machine Learning Gravitational Waves) is a useful tool for quickly generate a GW waveform for a BBH coalescence. It is part of a Master thesis work at University of Pisa (Italy).
It implements a ML model which is able to reproduce waveforms of GWs generated by state-of-the-art models. It is quicker than standard methods and it has the same degree of accuracy.

To generate a wave:
::

	import mlgw.GW_generator as generator
	import numpy as np
	import matplotlib.pyplot as plt

	#generating the wave
	generator = generator.GW_generator() #creating an istance of the generator
	theta = np.array([20,10,0.5,-0.3]) #list of parameters to be given to generator [m1,m2,s1,s2]
	times = np.linspace(-8,0.01, 100000) #time grid at which waves shall be evaluated
	h_p, h_c = generator.get_WF(theta, times) #returns amplitude and phase of the wave

	generator.model_summary() #printing model summary

	#plotting the wave
	plt.title("Waves by a BBH with [m1,m2,s1,s2] = "+str(theta))
	plt.plot(times, h_p[0,:]) #plot the plus polarization
	plt.show()

The ML model
============
The model is composed by a PCA + Mixture of Experts model and aims to generate a GW given some input parameter of the BBH. So far the model is fitted only to deal with aligned BH spins.

A PCA model is used to reduce dimensionality, trhough a linear transformation, of a densely sampled wave. It maps the wave to the linear combination of the first ``K`` principal components of the dataset.

A Mixture of Experts model (MoE) is useful to map the orbital parameters of the black holes to the reduced representation of the wave. A prediction of MoE is a linear combination of regression models (the experts), weighted by the output of a gating function which decides which expert to use. The orbital parameters considered are mass ratio ``q=m1/m2`` and the two BHs z-component spins ``s1`` and ``s2``; the total mass ``m1+m2`` is a scale factor and the dependence on it must not be fitted.

A complete model includes two PCA models for both phase and amplitude of the wave and a MoE model for each of the PC considered. The expert takes the form of a basis function regression and one can specify the features they want to use for their regression in the training and test data.

A dataset of GW must be created to fit the PCA model. It usually holds waves in time domain; it is advisable to generate them in a fixed reduced grid ``t' = t/M_tot`` where M_tot is the total mass of the BBH. The grid is such that the merger time is at ``t = 0``.

Usage of mlgw
=============
It outputs the GW strain:
h = h_+ + i h_x = Y_{2,2}(i, phi) h_{2,2}(m1,m2,s1,s2) + Y_{2,-2}(i, phi) h_{2,-2}(m1,m2,s1,s2)

Package mlgw consists in five modules.
   * **GW_generator**: the module holds class ``GW_generator`` which builds up all the model components (i.e. PCA + regressions for each PC) and performs some post processing of the waveform for dealing with known dependence on other physical quantities.
   * **EM_MoE**: holds an implementation of a MoE model as well as the softmax classifier required for it
   * **ML_routines**: holds an implementation of the PCA model as well a GDA classifier and a routine to do data augmentation
   * **GW_helper**: provides some routines to generate a dataset and to evaluate the closeness between waves. This is useful to assess model ability to reproduce original waves
   * **fit_model**: provides some routines useful to fit the MoE + PCA model.

Class ``GW_generator`` provides method ``get_WF`` to return the desidered waveform. Orbital parameters must be specified. It accepts data in one of the following layout of D features:
::

	D = 3	[q, spin1_z, spin2_z]
	D = 4	[m1, m2, spin1_z, spin2_z]
	D = 5	[m1, m2, spin1_z , spin2_z, D_L]
	D = 6	[m1, m2, spin1_z , spin2_z, D_L, inclination]
	D = 7	[m1, m2, spin1_z , spin2_z, D_L, inclination, phi_0]
	D = 14	[m1, m2, spin1 (3,), spin2 (3,), D_L, inclination, phi_0, long_asc_nodes, eccentricity, mean_per_ano]

Method ``__call__`` can only be given the last line.

Waves are usually generated in reduced grid ``t' = t/M_tot`` with a fixed number of grid points. With argument ``x_grid``, the user can specify a grid which they want to evaluate the wave at. Any custom grid must meet the convention that the origin of time is at merger and that the inspiral takes place at negative times.
An additional boolean argument ``red_grid`` must state whether the gri must be intended in reduced time domain or true time domain. Furthermore, the user must choose whether they want phase and amplitude or the plus and cross polarizations.

This page is intented to present the use of the code for the purpose of generating a wave. For more advanced needs or for more information, please refer to the code documentation: ::

   import mlgw
   help(mlgw)
   help(mlgw.<module_name>)

For full code source (and much more code) see: <https://github.com/stefanoschmidt1995/MLGW>

