"""Reproduce the synthetic detector, inference, and discrimination examples."""
from math import erf, exp, log, pi, sqrt


def gaussian_cdf(x):
    return (1+erf(x/sqrt(2)))/2


def bin_counts(left, right, sigma):
    return sum(500*(gaussian_cdf((right-center)/sigma)-
                    gaussian_cdf((left-center)/sigma)) for center in (1.8,2.2))


def singlet_triplet():
    gap = 0.8
    thermal_energy = 0.4
    f = 3*exp(-gap/thermal_energy)/(1+3*exp(-gap/thermal_energy))
    x = log(3*(1-f)/f)
    dtemp_df = gap/(x*x*f*(1-f))
    gap_sd, fraction_sd = 0.02,0.01
    temp_sd = sqrt((gap_sd/x)**2 + (dtemp_df*fraction_sd)**2)
    covariance = gap_sd**2/x
    print("Triplet fraction, thermal energy, its SD, covariance, correlation:",
          f, gap/x, temp_sd, covariance, covariance/(gap_sd*temp_sd))


if __name__ == "__main__":
    for sigma in (0.1,0.3):
        print("Gaussian sigma, line bin, central bin:",sigma,bin_counts(1.75,1.85,sigma),
              bin_counts(1.95,2.05,sigma))
    singlet_triplet()
    for temp in (50,4):
        theta = 0.08617333262*temp
        print("T, kBT (meV), |Jperp|/kBT:",temp,theta,1.6/theta)
    print("20 meV probe and transverse time scales (ps):",0.6582119569/20,0.6582119569/1.6)
    print("Solitary pulse mass after ten time units:",exp(-0.1),
          "shift units for kappa=1 and 1/2:",log(3),-2*log(3))
