Imports


In [6]:
import numpy as np
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt

from scipy import stats

# import warnings
# warnings.filterwarnings('error')

from multihist import Hist1d, Histdd

Default settings


In [7]:
# Digitizer sample size
dt = 2

# Waveform time labels
spe_ts = np.linspace(0, 639*2, 640) - 340 * 2
# Valid time (because the waveform does not range the full time span)
valid_t_range = (-100, 300)
t_mask = (valid_t_range[0] <= spe_ts) & (spe_ts < valid_t_range[1])
spe_ts = spe_ts[t_mask]
spe_t_edges = np.concatenate([[spe_ts[0] - dt/2], spe_ts + dt/2])


default_params = dict(
    t1 = 3.1,    # Singlet lifetime, Nest 2014 p2
    t3 = 24,     # Triplet lifetime, Nest 2014 p2
    fs = 0.2,    # Singlet fraction
    tts = 2.,     # Transit time spread.
    s1_min=50,
    s1_max=100,
    dset='er',
    pulse_model=1, # This is the CHANNEL that is used...
    n_photons = int(2e5),
    t_min = -15.,
    t_max = 125.,
    s1_sample = 'data', # 'uniform'
    error_offset  = 0. , 
    error_pct = 0.
    
)

def get_params(params):
    '''
    Returns full set of parameters, setting the values given in `params` and setting the values in 
    `default_params` if not set explicity.
    '''
    for k, v in default_params.items(): # key, value
        params.setdefault(k, v)
    if params['tts'] < 0:
        params['tts'] = 1e-6
    return params

Load PMT pulses

Pulse shape

One of the elements of simulted S1s is the single p.e. pulse model. We extract this from the gain calibration dataset.


In [8]:
import pickle
from scipy.interpolate import interp1d

spe_pulses_cum = []
spe_ys = []
for ch, fn in enumerate(['170323_103732', '170323_104831']):
    with open('../pulse_shape_single_pe/%s_ch%d.pickle' % (fn, ch) , 'rb') as infile:
        ys = pickle.load(infile)[t_mask]
    plt.plot(spe_ts, ys/ys.sum(), label='Channel %d' % ch)
    spe_ys.append(ys/ys.sum())
    # spe_pulses_cum: list of 2 elements: cumulative distribution for two channels
    spe_pulses_cum.append(
        interp1d(spe_ts, np.cumsum(ys)/ys.sum())
    )
plt.ylim(-0.01, 0.3)
plt.xlabel('Time (ns)')
plt.ylabel('Area / (2 ns)')
plt.legend()
plt.title('Relative (normalized) amplitude of single p.e. pulses.')
plt.show()



In [9]:
for ch, p in enumerate(spe_pulses_cum):
    plt.plot(spe_ts, p(spe_ts), label='Channel %d' % ch)
plt.grid(alpha=0.2, linestyle='-')
plt.xlabel('Time (ns)')
plt.ylabel('Cumulative fraction of area found')
plt.legend()
plt.show()


What do we need the cumulative fraction for? Well, we input this into the custom_pmt_pulse_current in pax.simulation. Here is a quick check that all is well. There is just a little shift, but the alignment is quite arbitrary anyway.


In [10]:
# custom_pmt_pulse_current(pmt_pulse, offset, dt, samples_before, samples_after)
from pax.simulation import custom_pmt_pulse_current
for ch, c in zip([0, 1], ['blue', 'red']):
    plt.plot(custom_pmt_pulse_current(spe_pulses_cum[ch], 0.1, 2, 10, 100), color=c)
    plt.plot(spe_ts * 0.5 + 10 - 0.5, spe_ys[ch] * 0.5, color=c, ls='--')
plt.xlim(-10, 60)
plt.xlabel('Time sample number')
plt.ylabel('Relative amplitude')
plt.show()


Gain variation


In [11]:
gain_params = []
for ch, fn in enumerate(['170323_103732', '170323_104831']):
    with open('../pulse_shape_single_pe/%s_ch%d_function.pickle' % (fn, ch) , 'rb') as infile:
        _norm, _popt, _perr = pickle.load(infile)
        gain_params.append(np.concatenate([np.array([_norm]), _popt, _perr]))
gain_params = np.array(gain_params)

In [12]:
import scipy

def area_sample(n_values, gain_params, **params):
    params = get_params(params)
    channel = params['pulse_model']
    norm, mu, sigma, _, _ = gain_params[channel]
    lower, upper = (0., 3.)
    X = stats.truncnorm((lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
    return X.rvs(n_values)

In [13]:
def gaus_trunc(x, mu, sigma):
    return (x > 0) * np.exp( - (x - mu)**2 / (2 * sigma**2))

In [14]:
nbins = 600
ran = (-0.5, 3.5)

for channel in (0, 1):
    plt.hist(area_sample(200000, gain_params, pulse_model = channel), bins=nbins, histtype='step', normed=True, range=ran)
    x_plot = np.linspace(*ran, num=nbins)
    y_plot =  gaus_trunc(x_plot,gain_params[channel][1], gain_params[channel][2])
    norm = 1 / (np.sum(y_plot) * (ran[1] - ran[0])) * nbins
    plt.plot(x_plot, norm * y_plot)
    plt.title('Channel %d' % channel)
    plt.show()


S1 model

Simulation


In [15]:
import numba

# def split_s1_groups(x, n_x, s1_min, s1_max):
#     """Splits x into groups with uniform(s1_min, s1_max) elements, then return matrix of histograms per group.
#     Returns: integer array (n_x, n_groups)
#     n_x: number of possible values in x. Assumed to be from 0 ... n_x - 1
#     s1_min: minimum S1 number of hits
#     s1_max: maximum S1 number of hits
#     """
#     # We want to exhaust the indices x. Simulate a generous amount of S1 sizes
#     n_s1_est = int(1.5 * 2 * len(x) / (s1_min + s1_max))
#     if 
#     hits_per_s1 = np.random.randint(s1_min, s1_max, size=n_s1_est)
    
#     result = np.zeros((n_x, n_s1_est), dtype=np.int)
#     s1_i = _split_s1_groups(x, hits_per_s1, result)
#     return result[:,:s1_i - 1]

# @numba.jit(nopython=True)
# def _split_s1_groups(x, hits_per_s1, result):
#     s1_i = 0
#     for i in x:
#         if hits_per_s1[s1_i] == 0:
#             s1_i += 1
#             continue
#         result[i, s1_i] += 1
#         hits_per_s1[s1_i] -= 1
#     return s1_i 

def split_s1_groups(x, n_x, areas, **params):
    """Splits x into groups with uniform (s1_min, s1_max) elements, then return matrix of histograms per group.
    Returns: integer array (n_x, n_groups)
    n_x: number of possible values in x. Assumed to be from 0 ... n_x - 1
    s1_min: minimum S1 number of hits
    s1_max: maximum S1 number of hits
    """
    params = get_params(params)
    # We want to exhaust the indices x. Simulate a generous amount of S1 sizes
    n_s1_est = int(1.5 * 2 * len(x) / (params['s1_min'] + params['s1_max']))
    
    if params['s1_sample'] == 'data' and 'xams_data' not in globals():
        print('Warning: data-derived s1 area distribution not possible, reverting to uniform...')
        params['s1_sample'] = 'uniform'
    if params['s1_sample'] == 'uniform':
        pe_per_s1 = (params['s1_max'] - params['s1_min']) * np.random.random(size=n_s1_est) + params['s1_min']
    elif params['s1_sample'] == 'data':
        # Take S1 from the data sample
        s1s_data = xams_data[params['dset']]['s1']
        s1s_data = s1s_data[(s1s_data >= params['s1_min']) & (s1s_data < params['s1_max'])]
        pe_per_s1  = np.random.choice(s1s_data, size=n_s1_est)
    else:
        raise ValueError('Configuration not understood, got this: ', params['s1_sample'])
    result = np.zeros((n_x, n_s1_est), dtype=float)
    # s1_i = _split_s1_groups(x, pe_per_s1, result)
    s1_i = _split_s1_groups(x, pe_per_s1, result, areas)
    return result[:,:s1_i - 1]


@numba.jit(nopython=True)
def _split_s1_groups(x, hits_per_s1, result, areas):
    s1_i = 0
    for photon_i, i in enumerate(x):
        if hits_per_s1[s1_i] < 0:
            s1_i += 1
            continue
        result[i, s1_i] += areas[photon_i]
        hits_per_s1[s1_i] -= areas[photon_i]
    return s1_i 

# %%timeit
# split_s1_groups(np.random.randint(0, 100, size=int(1e6)), 101, 10, 20)

def shift(x, n):
    """Shift the array x n samples to the right, adding zeros to the left."""
    if n > 0:
        return np.pad(x, (n, 0), mode='constant')[:len(x)]
    else:
        return np.pad(x, (0, -n), mode='constant')[-len(x):]



def simulate_s1_pulse(**params):
    # n_photons=int(2e5), 
    """Return (wv_matrix, time_matrix, t_shift vector) for simulated S1s, consisting of n_photons in total
    """
    params = get_params(params)
    n_photons = params['n_photons']

    ##
    # Make matrix (n_samples, n_waveforms) of pulse waveforms with various shifts
    ##
    i_noshift = np.searchsorted(spe_t_edges, [0])[0]    # Index corresponding to no shift in the waveform
    y = spe_ys[params['pulse_model']]  # This is the CHANNEL
    # This is a matrix filled with waveforms, ordered by their SHIFT.
    # So, these are all just model waveforms and will be selected later
    wv_matrix = np.vstack([shift(y, i - i_noshift) 
                           for i in range(len(spe_ts))]).T 
    
    ##
    # Simulate S1 pulse times, convert to index
    ##
    times = np.zeros(n_photons)

    n_singlets = np.random.binomial(n=n_photons, p=params['fs']) # We randomly select if the photon came from a singlet
                                                                 # or triplet decay
    # Time is distributed according to exponential distribution
    # This is the TRUE time of all the photons generated, assuming time=0  is the time of the interaction
    times += np.concatenate([
        np.random.exponential(params['t1'], n_singlets),
        np.random.exponential(params['t3'], n_photons - n_singlets)
    ])
    # Since `times` is now sorted in (singlet, triplet), shuffle them
    np.random.shuffle(times)
    
    # Here we start taking into account detector physics: the transit time spread (simulated as normal dist.)
    times += np.random.normal(0, params['tts'], size=n_photons)
    
    # Find the bin that the photon would be in if it were sampled.
    indices = np.searchsorted(spe_t_edges, times)

    # Now, we delete all the photons that are outside of the bin range and re-match to the bin centers
    # (Check the searchsorted documentation)
    indices = indices[~((indices == 0) | (indices == len(spe_t_edges)))] - 1

    # This is the new amount of photons simulated
    if len(indices) < n_photons:
        # print('Warning: I just threw away %d photons...' % (n_photons - len(indices)))
        n_photons = len(indices)
    
    # TODO: gain variation simulation
    areas = area_sample(n_photons, gain_params, **params)
    
    # NOTE do we also want to take the difference between the two channels into accont?

    ##
    # Build instruction matrix, simulate waveforms
    ##
    # So far, we've just been simulating a bunch of photons (very many).
    # We are now going to split this into S1s: the split will be made at a random point between s1_min and s1_max.
    # `index_matrix` is a matrix split into groups forming S1s. 
    # index_matrix = split_s1_groups(indices, len(spe_t_edges) - 1, params['s1_min'], params['s1_max'])

    index_matrix = split_s1_groups(indices, len(spe_t_edges) - 1, areas, **params)

    # Now, index_matrix[:, 0] contains a list of number of entries for the shift for each timestamp in bin
    n_s1 = index_matrix.shape[1]
    
    # return wv_matrix, index_matrix
    # Remember that wv_matrix is a matrix of waveforms, each element at position i of which is shifted i samples
    s1_waveforms = np.dot(wv_matrix, index_matrix)
    # return s1_waveforms

    ##
    # Alignment based on maximum sample, compute average pulse
    ##
    time_matrix, t_shift = aligned_time_matrix(spe_ts, s1_waveforms)    
    return s1_waveforms, time_matrix, t_shift

def aligned_time_matrix(ts, wv_matrix, mode = '10p'):
    """Return time matrix that would align waveforms im wv_matrix"""
    n_s1 = wv_matrix.shape[1]

    if mode == 'max':
        # Find the position of maximum sample and match its times
        t_shift = ts[np.argmax(wv_matrix, axis=0)]
    elif mode == '10p':
        fraction_reached = np.cumsum(wv_matrix, axis=0) / np.sum(wv_matrix, axis=0)
        # Get the sample where 10% is reached by taking the sample closest to the 10% point
        # This is as good as you can get without introducing fractional samples (which may be an improvement)
        # TODO get interpolation in here
        distance_to_10p_point = np.abs(fraction_reached - 0.1)
        t_shift = ts[np.argmin(distance_to_10p_point, axis=0)]
    
    time_matrix = np.repeat(ts, n_s1).reshape(wv_matrix.shape)
    time_matrix -= t_shift[np.newaxis,:]
    return time_matrix, t_shift

def average_pulse(time_matrix, wv_matrix):
    """Return average pulse, given time and waveform matrices"""
    h, _ = np.histogram(time_matrix, bins=spe_t_edges, weights=wv_matrix)
    h /= h.sum()
    return h

def s1_average_pulse_model(*args, **kwargs):
    wv_matrix, time_matrix, _ = simulate_s1_pulse(*args, **kwargs)
    return average_pulse(time_matrix, wv_matrix)

In [16]:
s1_wvs, tmat, _ = simulate_s1_pulse(n_photons=int(2e5), t3=1, t1=50, tts=1, fs=0.5, dset='nr')
for i in range(100):
    plt.plot(tmat[:, i], s1_wvs[:, i], alpha=0.1, c='k')
plt.grid(alpha=0.2, linestyle='-')


Warning: data-derived s1 area distribution not possible, reverting to uniform...

Here is what we get out. wv_matrix is a matrix containing the y-coordinates of the waveforms. The columns are the individual waveforms, to get the first waveform, go wv_matrix[:, 0]. time_matrix is the same thing except for it contains the times. t_shift_vector contains the shift of the waveform in ns (based on pulse times).

Statistical errors

Here we simulate statistical errors by simulating n_data_s1s and then performing bootstrap trials. The conclusion:....


In [17]:
def s1_models_resample(*args, n_data_s1s=1000, bootstrap_trials=10, **kwargs):
    """Return bootstrap_trials waveform templates from sampling n_data_s1s s1s"""
    wv_matrix, time_matrix, _ = simulate_s1_pulse(*args, **kwargs)
    n_s1s = wv_matrix.shape[1]
    
    waveform_templates = np.zeros((len(spe_ts), bootstrap_trials))

    for i in range(bootstrap_trials):
        new_indices = np.random.randint(n_s1s, size=n_data_s1s)

        waveform_templates[:, i] = average_pulse(time_matrix[:, new_indices], 
                                                 wv_matrix[:, new_indices])
    
    return waveform_templates

def sigmas_plot(x, q, color='b', **kwargs):
    for n_sigma, alpha in [(1,0.5), (2, 0.1)]:
        plt.fill_between(x,
                         np.percentile(q, 100 * stats.norm.cdf(-n_sigma), axis=1),
                         np.percentile(q, 100 * stats.norm.cdf(n_sigma), axis=1),
                         alpha=alpha, linewidth=0, color=color, step='mid')
    plt.plot(x, 
             np.percentile(q, 50, axis=1), 
             color=color, linestyle='-', alpha=0.5, linewidth=1, **kwargs)

In [18]:
waveform_templates = s1_models_resample(n_data_s1s=100, s1_min=50, s1_max=60, bootstrap_trials=100)
sigmas_plot(spe_ts, waveform_templates)


Warning: data-derived s1 area distribution not possible, reverting to uniform...

Statistical errors are negligible if you have more than a few hundred waveforms.

Systematic errors


In [19]:
import itertools

def s1_models_error(*args, shifts=None, **kwargs):
    '''
    Compute the error on the S1 waveform given errors on specific parameters.
    This will compute the S1 model for parameter +error, +0, and -error.
    All combinations of paramters are tried.
    `shifts` is a dict containting the allowed shift (+/-) for each model parameter.
    `*args` and `**kwargs` will be passed to `s1_average_pulse_model` to compute the base model.
    This function can also be used for getting the difference in pulse model for channel 0 and 1.
    '''
    if shifts is None:
        # Default uncertainty: in pulse model and in TTS
        shifts = dict(tts=0.5, pulse_model=[0,1])
    
    base_model = s1_average_pulse_model(*args, **kwargs)
    
    # Allow specifying a single +- amplitude of variation
    for p, shift_values in shifts.items():
        if isinstance(shift_values, (float, int)):
            shifts[p] = kwargs.get(p, default_params[p]) + np.array([-1, 0, 1]) * shift_values
    
    shift_pars = sorted(shifts.keys())
    shift_values = [shifts[k] for k in shift_pars]
    # shift_value_combs is a list of paramters that will be tried to compute the average pulse.
    # Contains all combintations: (+, 0, -) for all the parameters. ((3n)^2 for n number of parameters.)
    shift_value_combs = list(itertools.product(*shift_values))
    
    alt_models = []
    for vs in shift_value_combs:
        kw = dict()
        kw.update(kwargs)
        for i, p in enumerate(shift_pars):
            kw[p] = vs[i]        
        alt_models.append(s1_average_pulse_model(*args, **kw))
    
    
    alt_models = np.vstack(alt_models)
    # Hmmm. this seems like an upper estimate of the error, no?
    # ask jelle
    minus = np.min(alt_models, axis=0)
    plus = np.max(alt_models, axis=0)    
    return minus, base_model, plus
    
#     return [s1_average_pulse_model(*args, **kwargs) 
#              for q in [-tts_sigma, 0, tts_sigma]]

In [20]:
minus, base, plus = s1_models_error()
plt.fill_between(spe_ts, minus, plus, alpha=0.5, linewidth=0, label='Uncertainty')
plt.plot(spe_ts, base, label='Base model')
plt.xlabel('Time, (ns)')
plt.ylabel('Time')
plt.legend()
plt.show()


Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...
Warning: data-derived s1 area distribution not possible, reverting to uniform...

Real data waveforms

Here we read the S1 data for three (highfield) datasets: NR, ER and BG_NR. We store it in the form of a dict (keys: er, nr, bg_nr). Each dict item is an array containing the waveforms (per row).


In [21]:
xams_data = dict()
xams_data['nr'], xams_data['er'], xams_data['bg_nr'] = pickle.load(open('highfield_dataframes.pickle', 'rb'))

xams_s1s = dict()
# Get pulse waveforms to matrix rather than object column
for k, d in xams_data.items():
    xams_s1s[k] = np.array([x for x in d['s1_pulse']])
    del d['s1_pulse']

Here's an example waveform


In [22]:
plt.plot(spe_ts, xams_s1s['nr'][0])
plt.xlabel('Time (ns)')
plt.ylabel('Amplitude')
plt.show()



In [23]:
def real_s1_wv(**params):
    """Return average S1 waveform, number of S1s it was constructed from"""
    params = get_params(params)
    
    areas = xams_data[params['dset']]['s1'].values
    mask = (params['s1_min'] < areas) & (areas < params['s1_max'])

    # Could now derive distribution, I'll just assume uniform for the moment.
    # Hist1d(areas[mask],
    #        bins=np.linspace(params['s1_min'], params['s1_max'], 100)).plot()

    n_data_s1s = mask.sum()
    wvs = xams_s1s[params['dset']][mask].T
    tmat, _ = aligned_time_matrix(spe_ts, wvs)
    real_s1_avg =  average_pulse(tmat, wvs)
    
    return real_s1_avg, n_data_s1s

In [24]:
s1_range = (10, 20)
dset  ='nr'

ydata, n_data_s1s = real_s1_wv(s1_min = s1_range[0], s1_max = s1_range[1])
plt.plot(spe_ts, ydata)
plt.title('Average waveform %.1f - %.1f p.e., %d events.' % (s1_range[0], s1_range[1], n_data_s1s))


Out[24]:
<matplotlib.text.Text at 0x7f2f071c5518>

In [25]:
s1_bins = np.linspace(0, 100, 11)

In [26]:
for left, right in zip(s1_bins[:-1], s1_bins[1:]):
    ydata, n_data_s1s = real_s1_wv(s1_min = left, s1_max = right, dset = 'er')
    plt.plot(spe_ts, ydata, label = '%d - %d p.e.' % (left, right))
    #plt.title('Average waveform %.1f - %.1f p.e., %d events.' % (left, right, n_data_s1s))
    #plt.show()
plt.xlim(-10, 100)
plt.title('ER')
plt.legend()
plt.show()

for left, right in zip(s1_bins[:-1], s1_bins[1:]):
    ydata, n_data_s1s = real_s1_wv(s1_min = left, s1_max = right, dset='nr')
    plt.plot(spe_ts, ydata, label = '%d - %d p.e.' % (left, right))
    #plt.title('Average waveform %.1f - %.1f p.e., %d events.' % (left, right, n_data_s1s))
    #plt.show()
plt.xlim(-10, 100)
plt.title('NR')
plt.legend()
plt.show()


Model-data comparison

Plotting


In [128]:
def residuals(ydata, minus, base, plus, **params):
    params = get_params(params)
    # CHANGED BY ERIK check for zero
    sigma = get_sigma(minus, base, plus, **params)
    if 0. in sigma:
        zero_positions = np.where(sigma == 0)
        print('Warning: found zero in error array at positions: ', zero_positions)
        print('Replacing with infinite error instead...')
        for pos in zero_positions:
            sigma[pos] = np.inf
    return (ydata - base) / sigma

def get_sigma(minus, base, plus, **params):
    params = get_params(params)
    sigma = np.abs(plus - minus)/2 + params['error_offset'] + params['error_pct'] * np.abs(base)
    return sigma


def comparison_plot(ydata, minus, base, plus, **params):
    params = get_params(params)
    sigmas = get_sigma(minus, base, plus, **params)

    # large subplot
    ax2 = plt.subplot2grid((3,1), (2,0))
    ax1 = plt.subplot2grid((3,1), (0,0), rowspan=2, sharex=ax2)

    #f, (ax1, ax2) = plt.subplots(2, sharex=True)
    plt.sca(ax1)
    # plt.fill_between(spe_ts, minus, plus, alpha=0.5, linewidth=0, step='mid')
    plt.fill_between(spe_ts, base - sigmas, base + sigmas,
                     alpha=0.5, linewidth=0, step='mid')
    plt.plot(spe_ts, base, linestyle='steps-mid', label='Model')
    plt.plot(spe_ts, ydata, marker='.', linestyle='', markersize=3, c='k', label='Observed')

    plt.grid(alpha=0.1, linestyle='-', which='both')
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.ylabel("Fraction of amplitude")
    plt.axhline(0, c='k', alpha=0.5)
    leg = plt.legend(loc='upper right', numpoints=1)
    leg.get_frame().set_linewidth(0.0)
    leg.get_frame().set_alpha(0.5)
    plt.ylim(0, None)

    #ax1.set_xticklabels([])

    # Add residuals
    plt.sca(ax2)
    plt.subplot2grid((3,1), (2,0), sharex=ax1)
    plt.xlim(params['t_min'], params['t_max'])

    res = residuals(ydata, minus, base, plus)
    
    plt.plot(spe_ts, res,
             linestyle='', marker='x', c='k', markersize=3)
    plt.ylim(-3, 3)
    plt.grid(which='both', linestyle='-', alpha=0.1)
    plt.axhline(0, c='k', alpha=0.5)

    plt.ylabel("Residual")
    plt.xlabel("Time since alignment point")
    plt.text(#plt.xlim()[1] * 0.5, plt.ylim()[1] * 0.6,
             60, 2,
             'Mean abs. res.: %0.3f' % np.abs(res).mean())

    plt.tight_layout()
    plt.gcf().subplots_adjust(0,0,1,1,0,0)

def comparison_plot_2(ydata, minus, base, plus, **params):
    params = get_params(params)
    res = residuals(ydata, minus, base, plus, **params)
    sigmas = get_sigma(minus, base, plus, **params)
    
#     plt.fill_between(spe_ts, minus - params['error_offset'], plus + params['error_offset'],
#                      alpha=0.5, linewidth=0, step='mid')
    plt.fill_between(spe_ts, base - sigmas, base + sigmas,
                     alpha=0.5, linewidth=0, step='mid')
    plt.plot(spe_ts, base, linestyle='steps-mid', label='Model')
    plt.plot(spe_ts, ydata, marker='.', linestyle='', markersize=3, c='k', label='Observed')
    plt.yscale('log')
    plt.ylim(2e-5, 1e-1)
    plt.ylabel("Fraction of amplitude")
    plt.xlabel('Time (ns)')
    for _l in (params['t_min'], params['t_max']):
        plt.axvline(_l, ls='dotted', color='black')
    plt.twinx()
    plt.plot(spe_ts, np.abs(res), color='red')
    plt.ylabel('Residual / error')
    plt.ylim(0)
    plt.xlim(params['t_min'] - 20, params['t_max'] + 50)
    
    res = res[(spe_ts >= params['t_min']) & (spe_ts < params['t_max'])]
    chi2 = sum(res**2) / len(spe_ts[(spe_ts >= params['t_min']) & (spe_ts < params['t_max'])])
    print('chi2 = %f' % chi2)

In [89]:
cust_params = {
    's1_min' : 20,
    's1_max' : 30,
    'dset' : 'nr',
    'tts' : .75,
    'fs' : 0.2
}

ydata, n_data_s1s = real_s1_wv(**cust_params)
minus, base, plus = s1_models_error(**cust_params)
res = residuals(ydata, minus, base, plus)

comparison_plot(ydata, minus, base, plus)
print('Average waveform %.1f - %.1f p.e., %d events.' % (cust_params['s1_min'], cust_params['s1_max'], n_data_s1s))


Average waveform 20.0 - 30.0 p.e., 1260 events.

In [90]:
comparison_plot_2(ydata, minus, base, plus, error_offset = 0.0002)


chi2 = 2.719946

Fitting

Residuals function


In [123]:
def gof(verbose=True, mode = 'chi2_ndf', **params):
    '''
    Get the mean residuals for given model parameters.
    
    '''
    params = get_params(params)
    
    # Do not allow unphysical values
    if params['t1'] < 0 or params['t3'] < 0 or not (0 <= params['fs'] <= 1):
        result = float('inf')
    else:
        ydata, _ = real_s1_wv(**params)
        # By default, the errors are set to: [0,1] for pulse model, 1.0 for tts
        minus, base, plus = s1_models_error(**params)
        res = residuals(ydata, minus, base, plus, **params)
        assert len(res) == len(spe_ts)
        res = res[(spe_ts >= params['t_min']) & (spe_ts < params['t_max'])]
        if mode == 'mean':
            result = np.abs(res).mean()
        elif mode == 'median':
            result = np.median(np.abs(res))
        elif mode == 'chi2':
            result = np.sum(res**2)
        elif mode == 'chi2_ndf':
            result = 1/len(res) *np.sum(res**2)
        elif mode == 'res':
            result = res
        else:
            raise ValueError('Mode unknown, fot this: %s' % mode)
    if verbose and (mode != 'res'):
        print('gof={gof}, fs={fs}, t1={t1}, t3={t3}, tts={tts}'.format(gof=result, **params))
    return result

In [31]:
from copy import deepcopy
def gof_simultaneous(fs_er, fs_nr, verbose=True, mode='mean', **params):
    params = get_params(params)
    params_er = deepcopy(params)
    params_nr = deepcopy(params)
    params_er['dset'] = 'er'
    params_nr['dset'] = 'nr'
    params_er['fs'] = fs_er
    params_nr['fs'] = fs_nr
    gof_er = gof(verbose=False, mode=mode, **params_er)
    gof_nr = gof(verbose=False, mode=mode, **params_nr)
    if verbose:
        print('gof_er={gof_er}, gof_nr={gof_nr}, fs_er={fs_er}, fs_nr={fs_nr} t1={t1}, t3={t3}, tts={tts}'.format(
            gof_er=gof_er, gof_nr=gof_nr, fs_er = params_er['fs'], fs_nr = params_nr['fs'], **params))    
    return gof_er + gof_nr

In [32]:
gof_simultaneous(fs_er = 0.2, fs_nr = 0.16, mode='chi2', error_offset = 2e-4)


gof_er=70.33197197587143, gof_nr=843.4904703424528, fs_er=0.2, fs_nr=0.16 t1=3.1, t3=24, tts=2.0
Out[32]:
913.82244231832419

Statistics of nphotons and stability of fit


In [34]:
iterations = 100
n_photons_scan = [int(1e4), int(3e4), int(7e4), int(2e5)]

const_gofs = []
for n_photons in n_photons_scan:
    print(n_photons)
    const_gofs.append([gof(verbose = False, mode='chi2', n_photons = n_photons) for _ in range(iterations)])


10000
30000
70000
200000

In [35]:
for gofs, n_photons, c in zip(const_gofs, n_photons_scan, ['blue', 'orange', 'green', 'red', 'black']):
    plt.hist(gofs, label="%d" % n_photons, histtype='step', range=(0, 500), bins=100, color = c)
    plt.axvline(np.mean(gofs), color = c)
plt.legend()
plt.show()


Wait, what? The residuals spread get larger with increasing stats? That does not sound right.


In [36]:
for i in range(10):
    plt.plot(gof(mode='res', error_offset = 0.))



In [37]:
for i in range(10):
    plt.plot((gof(mode='res', error_offset = 0., error_pct = 0.1))**2)



In [41]:
def sigma_from_params(**params):
    params = get_params(params)
    # ydata, _ = real_s1_wv(**params)
    minus, base, plus = s1_models_error(**params)
    sigma = get_sigma(minus, base, plus, **params)
    sigma = sigma[(spe_ts >= params['t_min']) & (spe_ts < params['t_max'])]

    return sigma

In [57]:
plt.plot(1/sigma_from_params(error_pct = 5e-2, error_ofset = 1e-3))
plt.ylim(0)


Out[57]:
(0, 47834.489962572865)

In [112]:
iterations = 250
n_photons_scan = [int(1e4), int(3e4), int(7e4), int(2e5)]

const_gofs = []
for n_photons in n_photons_scan:
    print(n_photons)
    const_gofs.append([gof(verbose = False, mode='chi2', n_photons = n_photons,
                           error_pct = 1e-2, error_ofset = 1e-4) for _ in range(iterations)])


10000
30000
70000
200000

In [113]:
for gofs, n_photons, c in zip(const_gofs, n_photons_scan, ['blue', 'orange', 'green', 'red', 'black']):
    plt.hist(gofs / np.average(gofs), label="%d" % n_photons, histtype='step', range=(0, 2), bins=200, color = c)
    plt.axvline(color = c)
plt.legend()
plt.show()



In [108]:
ydata, n_data_s1s = real_s1_wv()
minus, base, plus = s1_models_error()
# res = residuals(ydata, minus, base, plus)

comparison_plot_2(ydata, minus, base, plus, error_pct = 1e-2, error_offset = 1e-4, t_max= 125)
# plt.ylim(0, 2)


chi2 = 1.422140

Fit fit fit


In [ ]:
from scipy import optimize
optresult = optimize.minimize(
    lambda x: gof_simultaneous(fs_er=x[0], fs_nr=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, 
                               mode='chi2', error_offset = 1e-4),
    [0.2, 0.3, 25., 2.],
    bounds=[[.01, 1], [20, 30], [.1, 5]],
    options=dict(maxfev=10000),
    method='Powell',
)
print('Done')

# mode = mean, s1_min =30, s1_max = 100: [  0.20968042,   0.28464569,  24.8145522 ,   2.42197182]
# array([  0.17916349,   0.32752012,  24.00000003,   1.03864494])
# array([  0.18086791,   0.24823393,  24.23984679,   2.3384889 ]) 462.62128366264312
# array([  0.19454366,   0.3126068 ,  25.57424767,   2.38196603]) 484.92280858647905

In [ ]:
x = optresult.x

In [ ]:
def check_params(plot_type = 0, **params):
    params = get_params(params)
    ydata, _ = real_s1_wv(**params)
    minus, base, plus = s1_models_error(**params)
    if plot_type == 1:
        comparison_plot(ydata, minus, base, plus, **params)
    elif plot_type == 2:
        comparison_plot_2(ydata, minus, base, plus, **params)
    elif plot_type == 0:
        comparison_plot(ydata, minus, base, plus, **params)
        plt.show()
        comparison_plot_2(ydata, minus, base, plus, **params)

    return

In [ ]:
x

In [ ]:
optresult

In [ ]:
check_params(s1_min = 30, s1_max = 100, dset='er', fs=x[0], t3 = x[2], tts=x[3], plot_type=0, error_offset = 1e-4)
plt.title('ER')
plt.show()

check_params(s1_min = 30, s1_max = 100, dset='nr', fs=x[1], t3 = x[2], tts=x[3], plot_type=0, error_offset = 1e-4)
plt.title('NR')
plt.show()

In [ ]:
gofs = [gof_simultaneous(fs_er=x[0], fs_nr=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, 
                         mode='chi2', error_offset = 1e-4) 
        for _ in range(20)]

In [ ]:
plt.hist(gofs)

Fit singlet fraction and TTS


In [124]:
from scipy import optimize
optresult = optimize.minimize(
    lambda x: gof(fs=x[0], tts=x[1], s1_min=30, s1_max = 100, error_pct = 1e-2, error_offset = 1e-4, mode='chi2_ndf'),
    [0.2, 2],
    bounds=[[.01, 1], [20, 30], [.1, 5]],
    options=dict(maxfev=1000),
    method='Powell',
)
print('Done')


/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py:394: RuntimeWarning: Method Powell cannot handle constraints nor bounds.
  RuntimeWarning)
gof=0.770676394387705, fs=0.2, t1=3.1, t3=24, tts=2.0
gof=0.7286314368266296, fs=0.2, t1=3.1, t3=24, tts=2.0
gof=inf, fs=1.2, t1=3.1, t3=24, tts=2.0
gof=inf, fs=-1.418034, t1=3.1, t3=24, tts=2.0
gof=0.8616206301245938, fs=0.2, t1=3.1, t3=24, tts=2.0
gof=inf, fs=-0.418033974844, t1=3.1, t3=24, tts=2.0
/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py:1876: RuntimeWarning: invalid value encountered in double_scalars
  tmp2 = (x - v) * (fx - fw)
gof=31.04729667915003, fs=0.581966, t1=3.1, t3=24, tts=2.0
gof=inf, fs=-0.036067965235263316, t1=3.1, t3=24, tts=2.0
gof=6.984241599826563, fs=0.34589802515600004, t1=3.1, t3=24, tts=2.0
gof=0.7983361033350416, fs=0.1393000731574512, t1=3.1, t3=24, tts=2.0
gof=0.6282482773951543, fs=0.1670182944998811, t1=3.1, t3=24, tts=2.0
gof=0.5191621133901148, fs=0.16734811156488227, t1=3.1, t3=24, tts=2.0
gof=0.4802518541943177, fs=0.17982002278289044, t1=3.1, t3=24, tts=2.0
gof=0.4961818053731075, fs=0.17364501934418344, t1=3.1, t3=24, tts=2.0
gof=0.6124364091995548, fs=0.18752808796060091, t1=3.1, t3=24, tts=2.0
gof=0.5736163564496944, fs=0.1827642416065598, t1=3.1, t3=24, tts=2.0
gof=0.6335075120262466, fs=0.1774613814194213, t1=3.1, t3=24, tts=2.0
gof=0.5146510469955032, fs=0.18094461427009215, t1=3.1, t3=24, tts=2.0
gof=0.6164416074764564, fs=0.1789191019758516, t1=3.1, t3=24, tts=2.0
gof=0.5266044865442224, fs=0.18024957849489093, t1=3.1, t3=24, tts=2.0
gof=0.5978492719305601, fs=0.17947590166590904, t1=3.1, t3=24, tts=2.0
gof=0.42581088073067885, fs=0.18002182256506155, t1=3.1, t3=24, tts=2.0
gof=0.5357651342747873, fs=0.18002182256506155, t1=3.1, t3=24, tts=2.0
gof=1.096119903443901, fs=0.18002182256506155, t1=3.1, t3=24, tts=3.0
gof=0.8268902202933353, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.381966
gof=0.5858877127505298, fs=0.18002182256506155, t1=3.1, t3=24, tts=2.0
gof=0.44951014098977443, fs=0.18002182256506155, t1=3.1, t3=24, tts=1.381966025156
gof=0.4306703656087777, fs=0.18002182256506155, t1=3.1, t3=24, tts=1.0000000155472633
gof=0.4656659917019805, fs=0.18002182256506155, t1=3.1, t3=24, tts=1.0470497220357995
gof=0.4389624072702519, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.7639320347647374
gof=0.48516024192404295, fs=0.18002182256506155, t1=3.1, t3=24, tts=1.010498582460143
gof=0.4136183840247568, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.990000015692736
gof=0.5012849577308089, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.9036497332895921
gof=0.5296268294056831, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.9570171437243367
gof=0.6292506208303373, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.9774016800184544
gof=1.0040503550220397, fs=0.1600436451301231, t1=3.1, t3=24, tts=1e-06
gof=0.5508182313063051, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.990000015692736
gof=inf, fs=1.1800218225650616, t1=3.1, t3=24, tts=0.990000015692736
gof=inf, fs=-1.4380121774349384, t1=3.1, t3=24, tts=0.990000015692736
gof=0.4572736162351409, fs=0.18002182256506155, t1=3.1, t3=24, tts=0.990000015692736
gof=inf, fs=-0.43801215227893847, t1=3.1, t3=24, tts=0.990000015692736
gof=32.994460316807945, fs=0.5619878225650616, t1=3.1, t3=24, tts=0.990000015692736
gof=inf, fs=-0.056046142670201776, t1=3.1, t3=24, tts=0.990000015692736
gof=6.651211669879178, fs=0.3259198477210616, t1=3.1, t3=24, tts=0.990000015692736
gof=0.7654778509672601, fs=0.1356984056813152, t1=3.1, t3=24, tts=0.990000015692736
gof=0.39255044949203577, fs=0.17124586386888266, t1=3.1, t3=24, tts=0.990000015692736
gof=0.3572526159343914, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.990000015692736
gof=0.5991017597430491, fs=0.15472590262015862, t1=3.1, t3=24, tts=0.990000015692736
gof=0.42246529725343596, fs=0.1619937595159009, t1=3.1, t3=24, tts=0.990000015692736
gof=0.40774638355182785, fs=0.1673017643555476, t1=3.1, t3=24, tts=0.990000015692736
gof=0.5318308232961548, fs=0.16476983374293996, t1=3.1, t3=24, tts=0.990000015692736
gof=0.4269819096558629, fs=0.16583019971114518, t1=3.1, t3=24, tts=0.990000015692736
gof=0.42841335856940693, fs=0.16679731111952048, t1=3.1, t3=24, tts=0.990000015692736
gof=0.4244268456889808, fs=0.16623522345855665, t1=3.1, t3=24, tts=0.990000015692736
gof=0.4620605435295886, fs=0.1666209047055414, t1=3.1, t3=24, tts=0.990000015692736
gof=0.41360000731957103, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.990000015692736
gof=0.6320895882163345, fs=0.16648554188877857, t1=3.1, t3=24, tts=1.990000015692736
gof=1.2312337719396678, fs=0.16648554188877857, t1=3.1, t3=24, tts=1e-06
gof=0.43886643726354574, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.990000015692736
gof=0.5479435774734546, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.37196604084873597
gof=0.5063587797546322, fs=0.16648554188877857, t1=3.1, t3=24, tts=1.371966015692736
gof=0.36450700632737004, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.44892284375881975, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.7173671049710744
gof=0.5468459763600357, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.8567267201582875
gof=0.4599387556579802, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9302450589307412
gof=0.4719463018228743, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9534350702063377
gof=0.43784307395509847, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.93946850423868
gof=0.39708510451883106, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9341337509022777
gof=0.47833720460420426, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9320960565093854
gof=0.3798870817556699, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9314283254210778
gof=0.4939209998135177, fs=0.15294926121249558, t1=3.1, t3=24, tts=0.871673368659083
gof=0.4603308090892528, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9308366921759095
gof=inf, fs=1.1664855418887785, t1=3.1, t3=24, tts=0.9308366921759095
gof=inf, fs=-1.4515484581112215, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.39876679984881297, fs=0.16648554188877857, t1=3.1, t3=24, tts=0.9308366921759095
gof=inf, fs=-0.45154843295522146, t1=3.1, t3=24, tts=0.9308366921759095
gof=30.675605817746963, fs=0.5484515418887785, t1=3.1, t3=24, tts=0.9308366921759095
gof=inf, fs=-0.06958242334648476, t1=3.1, t3=24, tts=0.9308366921759095
gof=5.709423175231812, fs=0.3123835670447786, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.9396710800788467, fs=0.13920569919763054, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.6527574852801344, fs=0.1833800716920745, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.373477517055695, fs=0.1654078895181334, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4233000162185152, fs=0.1553995436901722, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.5256787536295439, fs=0.16137378066073851, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.45574217777101234, fs=0.1638669970943097, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4380268327605127, fs=0.16481932100257515, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.39461312426673323, fs=0.16581951608353923, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.5150090261108451, fs=0.16518307635651966, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4000851444328263, fs=0.1655651168708152, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4691431574554047, fs=0.16532201853404443, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4011661035928927, fs=0.16546794502112785, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.3885805199020526, fs=0.16537508972182488, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.42650723546133573, fs=0.16543082867839018, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.4649165998281455, fs=0.16539536111113662, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.5549508251676032, fs=0.16541866605183983, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.47479245302736045, fs=0.1654078895181334, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.5392108093914432, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.9308366921759095
gof=1.1459885724526102, fs=0.1654078895181334, t1=3.1, t3=24, tts=1e-06
gof=0.5277535391177147, fs=0.1654078895181334, t1=3.1, t3=24, tts=0.9308366921759095
gof=0.702900759259355, fs=0.1654078895181334, t1=3.1, t3=24, tts=0.31280271733190945
gof=0.396937111321284, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.3128026921759095
gof=0.4323372570635232, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.5488706670199095
gof=0.37013708763111114, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.3367349439778058
gof=0.5168789846035047, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.4183104261949049
gof=0.3934553002074163, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.3678940046183423
gof=0.4137623418645813, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.3412802162382176
gof=0.3766436733446772, fs=0.1654078895181334, t1=3.1, t3=24, tts=1.332675961449787
Done

In [127]:
optresult


Out[127]:
   direc: array([[ 1.,  0.],
       [ 0.,  1.]])
     fun: 0.37013708763111114
 message: 'Optimization terminated successfully.'
    nfev: 107
     nit: 3
  status: 0
 success: True
       x: array([ 0.16540789,  1.33673494])

In [129]:
fit = optresult.x
print(fit)
ydata, _ = real_s1_wv()
minus, base, plus = s1_models_error(fs=fit[0], tts=fit[1], s1_min = 30, s1_max = 100,
                                    error_pct = 1e-2, error_offset = 1e-4)
comparison_plot(ydata, minus, base, plus, error_pct = 1e-2, error_offset = 1e-4)
plt.show()
comparison_plot_2(ydata, minus, base, plus, error_pct = 1e-2, error_offset = 1e-4)
plt.show()


[ 0.16540789  1.33673494]
chi2 = 0.491222

GOF uncertainty

Need higher stats?

Fit three parameters


In [143]:
from scipy import optimize
optresult = optimize.minimize(
    lambda x: gof(fs=x[0], t3=x[1], tts=x[2], s1_min = 30, s1_max = 100,
                                    error_pct = 0.5e-2, error_offset = 1e-5),
    [0.2, 24, 3],
    bounds=[[.01, 1], [20, 30], [.1, 5]],
    options=dict(maxfev=1000),
    method='Powell',
)


/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py:394: RuntimeWarning: Method Powell cannot handle constraints nor bounds.
  RuntimeWarning)
gof=2.2177495739287187, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=2.2424170787502713, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=1.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-1.418034, t1=3.1, t3=24.0, tts=3.0
gof=1.9907361500855736, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-0.418033974844, t1=3.1, t3=24.0, tts=3.0
/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py:1876: RuntimeWarning: invalid value encountered in double_scalars
  tmp2 = (x - v) * (fx - fw)
gof=62.71189198157917, fs=0.581966, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-0.036067965235263316, t1=3.1, t3=24.0, tts=3.0
gof=10.643597994279677, fs=0.34589802515600004, t1=3.1, t3=24.0, tts=3.0
gof=2.0856411545070754, fs=0.20270879309856418, t1=3.1, t3=24.0, tts=3.0
gof=4.53509046574129, fs=0.10983006359094741, t1=3.1, t3=24.0, tts=3.0
gof=2.3759057628128755, fs=0.17563167701709168, t1=3.1, t3=24.0, tts=3.0
gof=1.906131778180374, fs=0.19202480672058136, t1=3.1, t3=24.0, tts=3.0
gof=2.1767636528311116, fs=0.19369246399738027, t1=3.1, t3=24.0, tts=3.0
gof=2.0570496174818373, fs=0.18576318854025822, t1=3.1, t3=24.0, tts=3.0
gof=1.8131724334651966, fs=0.18963308147071606, t1=3.1, t3=24.0, tts=3.0
gof=1.7979706783609393, fs=0.1881549139476408, t1=3.1, t3=24.0, tts=3.0
gof=1.9599384584593589, fs=0.1882733648181644, t1=3.1, t3=24.0, tts=3.0
gof=2.0924203692210326, fs=0.18803646307711722, t1=3.1, t3=24.0, tts=3.0
gof=2.215179520790236, fs=0.1881549139476408, t1=3.1, t3=24.0, tts=3.0
gof=1.7702448534735669, fs=0.1881549139476408, t1=3.1, t3=25.0, tts=3.0
gof=3.1563307096081576, fs=0.1881549139476408, t1=3.1, t3=26.618034, tts=3.0
gof=1.6047162569372808, fs=0.1881549139476408, t1=3.1, t3=25.0, tts=3.0
gof=1.7416193124389607, fs=0.1881549139476408, t1=3.1, t3=25.618033974844, tts=3.0
gof=1.6298179389934004, fs=0.1881549139476408, t1=3.1, t3=24.618034, tts=3.0
gof=1.4894247729646364, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=3.0
gof=1.6191153961966485, fs=0.1881549139476408, t1=3.1, t3=24.815403774133458, tts=3.0
gof=1.584542146790984, fs=0.1881549139476408, t1=3.1, t3=24.91036236721439, tts=3.0
gof=1.4957099103351938, fs=0.1881549139476408, t1=3.1, t3=24.95403399193865, tts=3.0
gof=1.5808598139448933, fs=0.1881549139476408, t1=3.1, t3=24.935110182652668, tts=3.0
gof=1.480924363427658, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=3.0
gof=4.293412846796572, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=4.0
gof=0.6009130562549286, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.381966
gof=0.47185233094288126, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.8771592919847577
gof=0.6248404779658195, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.8771592919847577
gof=0.6945299613559787, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=2.306046265862508
gof=0.42701251125193057, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.6880122910185078
gof=0.42048445584601063, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.5711130134233324
gof=0.5066903812576338, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.6209314529858956
gof=0.41959091852216285, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.6027086675541311, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.513876632451135
gof=0.528835628379072, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.4838539381370166
gof=7.742634743041396, fs=0.1763098278952816, t1=3.1, t3=25.846829251749355, tts=1e-06
gof=0.4424761597138414, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=inf, fs=1.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=inf, fs=-1.4298790860523591, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.40003718048829207, fs=0.1881549139476408, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=inf, fs=-0.4298790608963592, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=43.20692493638818, fs=0.5701209139476409, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=inf, fs=-0.04791305128762252, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=10.260980788036589, fs=0.33405293910364087, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=7.450079024697545, fs=0.0817577617541237, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5206164127662515, fs=0.1974051164467325, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.404884829511064, fs=0.1832746037019618, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5676697935485422, fs=0.1862150200821551, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.4739768023334821, fs=0.19168817679540887, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5043441847767169, fs=0.18950450022455137, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.48070257301329206, fs=0.1874139404474167, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.7655209840004373, fs=0.18867041001948723, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.521643236593169, fs=0.1878718872636542, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.42112156086546887, fs=0.18835181592021968, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.4184737037295724, fs=0.18804680737726517, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.632858888303922, fs=0.18823012380649887, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.48522145078052237, fs=0.1881136209133807, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5293221592009025, fs=0.1881836415565894, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5499962229532238, fs=0.18813914141251661, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.45889228023099965, fs=0.18816588691752045, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.48268895340229717, fs=0.18814888937548957, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5826246525534484, fs=0.18815910524905385, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.43030423622955843, fs=0.1881526127659145, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.437299288697194, fs=0.18815651488227633, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.46985611899674806, fs=0.18815403497446154, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5482841973249372, fs=0.1881555254502398, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5587437670425335, fs=0.1881545782097714, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.45736525602107886, fs=0.18815514752084253, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.38702322995866895, fs=0.1881547857071898, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.47389544058175037, fs=0.18815470645023089, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5784316748772845, fs=0.1881548346906819, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.39799835718559856, fs=0.18815475543372623, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5506555663065992, fs=0.18815480441721832, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.4967476760145465, fs=0.188154774143756, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5481809581526248, fs=0.18815479285378456, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.49041017262123066, fs=0.18815478129035124, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.43267328834468805, fs=0.188154788436946, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.7675484730922755, fs=0.18815478402010763, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.8106851506234803, fs=0.1881547869995943, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.5330893540762117, fs=0.1881547857071898, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.7723448385676548, fs=0.1881547857071898, t1=3.1, t3=25.923414625874678, tts=1.4988652852940758
gof=2.6922754223319405, fs=0.1881547857071898, t1=3.1, t3=23.305380625874676, tts=1.4988652852940758
gof=0.4570384429608552, fs=0.1881547857071898, t1=3.1, t3=24.923414625874678, tts=1.4988652852940758
gof=0.9788362435273971, fs=0.1881547857071898, t1=3.1, t3=24.305380651030678, tts=1.4988652852940758
gof=0.5682934533684982, fs=0.1881547857071898, t1=3.1, t3=25.305380625874676, tts=1.4988652852940758
gof=0.544529834327105, fs=0.1881547857071898, t1=3.1, t3=24.98614794061211, tts=1.4988652852940758
gof=0.5123329379523831, fs=0.1881547857071898, t1=3.1, t3=24.687346660639413, tts=1.4988652852940758
gof=0.5376815424327204, fs=0.1881547857071898, t1=3.1, t3=24.826864204939803, tts=1.4988652852940758
gof=0.5479977410564624, fs=0.1881547857071898, t1=3.1, t3=24.886535647791867, tts=1.4988652852940758
gof=0.5927745625820084, fs=0.1881547857071898, t1=3.1, t3=24.947376619171674, tts=1.4988652852940758
gof=0.5499240919963859, fs=0.1881547857071898, t1=3.1, t3=24.9093281101323, tts=1.4988652852940758
gof=0.39446812155635164, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.4988652852940758
gof=0.5100189053359573, fs=0.1881547857071898, t1=3.1, t3=24.938223951837205, tts=1.4988652852940758
gof=0.516879991864966, fs=0.1881547857071898, t1=3.1, t3=24.929847658199503, tts=1.4988652852940758
gof=0.48869982439814347, fs=0.1881547857071898, t1=3.1, t3=24.932658819283677, tts=1.4988652852940758
gof=0.5151019797665948, fs=0.1881547857071898, t1=3.1, t3=24.93152848473051, tts=1.4988652852940758
gof=0.6396034873222326, fs=0.1881547857071898, t1=3.1, t3=24.932170503317252, tts=1.4988652852940758
gof=0.4804606599830216, fs=0.1881547857071898, t1=3.1, t3=24.932415732588755, tts=1.4988652852940758
gof=0.5147525644660793, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.4988652852940758
gof=0.8342404559828798, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=2.498865285294076
gof=6.218372794210961, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1e-06
gof=0.46638280230416923, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.4988652852940758
gof=0.5671636977612896, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=0.8808313104500758
gof=0.5239620877841993, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.8808312852940758
gof=0.5542358630719654, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.4496650038138652
gof=0.6646013631590229, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.6730653085222
gof=0.518905998161589, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5654037713664295
gof=0.44020576182947113, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5242807246651884
gof=0.3812401350031034, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4655102918984641, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5117958219000562
gof=0.491413606776131, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5188522500947552
gof=0.4778892595818327, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5215475657465716
gof=0.4554159345722889, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5225770846848332
gof=0.5255631902093083, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5234731661074934
gof=0.38889311984593056, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.5229698815698547
gof=0.4588623463178773, fs=0.1881546574667388, t1=3.1, t3=24.941719959338037, tts=1.5475614394067603
gof=0.4137925401354301, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=inf, fs=1.18815478570719, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=inf, fs=-1.4298792142928103, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.49426890487296077, fs=0.1881547857071898, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=inf, fs=-0.4298791891368102, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=43.74967360717648, fs=0.5701207857071898, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=inf, fs=-0.04791317952807353, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=9.611586735894111, fs=0.33405281086318983, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=4.44166195386264, fs=0.11577146597092416, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.6275699156572899, fs=0.20282327540139825, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.48950660979948435, fs=0.18927193484320706, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4904970576110849, fs=0.19087687905377637, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4837571547117289, fs=0.1898849689635414, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4512827562536638, fs=0.19004378759727533, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.42123686525349135, fs=0.1903620002085492, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.5055600812548346, fs=0.19040752845495112, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.5011970639519613, fs=0.19024045381027138, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4326541173289967, fs=0.19031557361698462, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.4513055502680267, fs=0.1903399280535356, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.46920026641652035, fs=0.1903840723635628, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.6113540052767366, fs=0.1903620002085492, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.7680616665312608, fs=0.1903620002085492, t1=3.1, t3=25.932567292606358, tts=1.523213362350418
gof=2.5529495505251765, fs=0.1903620002085492, t1=3.1, t3=23.314533292606356, tts=1.523213362350418
gof=0.5849208237081307, fs=0.1903620002085492, t1=3.1, t3=24.932567292606358, tts=1.523213362350418
gof=0.9196452692464908, fs=0.1903620002085492, t1=3.1, t3=24.314533317762358, tts=1.523213362350418
gof=0.38727858639466156, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.523213362350418
gof=0.54691267165749, fs=0.1903620002085492, t1=3.1, t3=25.550601267450357, tts=1.523213362350418
gof=0.46585722160837123, fs=0.1903620002085492, t1=3.1, t3=25.25750519247842, tts=1.523213362350418
gof=0.4977878956018337, fs=0.1903620002085492, t1=3.1, t3=25.384323203869258, tts=1.523213362350418
gof=0.5462444617520282, fs=0.1903620002085492, t1=3.1, t3=25.318352952616358, tts=1.523213362350418
gof=0.48443987303767244, fs=0.1903620002085492, t1=3.1, t3=25.310713632596357, tts=1.523213362350418
gof=0.742646546159681, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.523213362350418
gof=1.0170321503481294, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=2.5232133623504183
gof=5.3888137945690415, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1e-06
gof=0.47388107172988203, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.523213362350418
gof=0.6341656984308978, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=0.905179387506418
gof=0.5557791090770953, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.905179362350418
gof=0.45711206671737903, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.4879079169768918
gof=0.3813491772590325, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.49974172218581286, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.0505100435897223
gof=0.5523468169246362, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.350344373140975
gof=0.6372312935910658, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.1832732570347713
gof=0.3964832281753727, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.297799776556145
gof=0.46381064359277674, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.2339842906215228
gof=0.4200150127943699, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.277729527177024
gof=0.5213575048400594, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.25335418127652
gof=0.4473288109326931, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.2700633743026786
gof=0.6582814030385488, fs=0.1903620002085492, t1=3.1, t3=25.314533292606356, tts=1.2607528209304468

In [144]:
fit = optresult.x
print(fit)
ydata, _ = real_s1_wv()
minus, base, plus = s1_models_error(fs=fit[0], t3=fit[1], tts=fit[2], error_pct = 1e-2, error_offset = 1e-4)
comparison_plot(ydata, minus, base, plus, error_pct = 0.5e-2, error_offset = 1e-5)
plt.show()
comparison_plot_2(ydata, minus, base, plus, error_pct = 0.5e-2, error_offset = 1e-5)
plt.show()


[  0.190362    25.31453329   1.26532543]
chi2 = 0.422839

In [230]:
def gof_v_parameter(parameter, variation_range, num, **params):
    params_to_try = np.linspace(*variation_range, num=num)
    gofs = []
    for param_value in params_to_try:
        params[parameter] = param_value
        gofs.append(gof(**params))
    return params_to_try, np.array(gofs)

def gof_v_2_paramters(parameter1, parameter2, variation_range1, variation_range2, num1, num2, **params):
    import time
    start = time.time()
    params_to_try1 = np.linspace(*variation_range1, num=num1)
    params_to_try2 = np.linspace(*variation_range2, num=num2)
    gvd = []
    for par1 in params_to_try1:
        for par2 in params_to_try2:
            params[parameter1] = par1
            params[parameter2] = par2
            gof_value = gof(**params)
            gvd.append([par1, par2, gof_value])
    stop = time.time()
    print('Computation took %d seconds (%.1f s/it)' % ((stop - start), (stop - start) / len(gvd)))
    return np.array(gvd)

In [244]:
nx = 20
ny = 20
ding = gof_v_2_paramters('fs', 't3', (0.16, 0.24), (23., 27.), nx, ny, tts=fit[2],
                         error_pct = 1e-2, error_offset = 1e-4, verbose=False)


Computation took 337 seconds (0.8 s/it)

In [245]:
plt.scatter(ding[:,0], ding[:,1], c=ding[:, 2])
plt.colorbar()


Out[245]:
<matplotlib.colorbar.Colorbar at 0x7f2f06161d68>

In [246]:
x = np.reshape(ding[:, 0], (nx, ny))
y = np.reshape(ding[:, 1], (nx, ny))
z = np.reshape(ding[:, 2], (nx, ny))

In [247]:
plt.pcolormesh(x, y, z/ np.min(z))
plt.colorbar()


Out[247]:
<matplotlib.colorbar.Colorbar at 0x7f2f04823be0>

In [ ]:
edge_x = ding[:, 0]
edge_y = 
plt.figure()
ax = plt.gca()
pc = ax.pcolormesh(edge_x, edge_y, 1000* (h_fg - h_bg).T,  cmap='RdBu', vmin = -3e-1, vmax = 3e-1)

In [157]:
fss, gofs = gof_v_parameter('fs', (0.14, 0.24), 20, fs=fit[0], t3=fit[1], tts=fit[2], error_pct = 1e-2, error_offset = 1e-4)


gof=0.8223248960610716, fs=0.14, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.7518969908565659, fs=0.14526315789473684, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.5375879783198912, fs=0.1505263157894737, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.563174577752749, fs=0.15578947368421053, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.43184031947407575, fs=0.1610526315789474, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.30005747084716083, fs=0.16631578947368422, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.2460460642009813, fs=0.17157894736842105, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.20121751674928934, fs=0.1768421052631579, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.1538217910931106, fs=0.18210526315789474, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.16741921838243617, fs=0.18736842105263157, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.15121393986414677, fs=0.19263157894736843, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.2418855974282201, fs=0.19789473684210526, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.19504252582991816, fs=0.2031578947368421, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.3382930291329042, fs=0.20842105263157895, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.48389300981014327, fs=0.21368421052631578, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.5124172115423162, fs=0.2189473684210526, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.5098031148848247, fs=0.22421052631578947, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.7937584460141178, fs=0.22947368421052633, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=0.7624915604063834, fs=0.23473684210526316, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729
gof=1.0531422169619842, fs=0.24, t1=3.1, t3=25.314533292606356, tts=1.2653254314891729

In [158]:
plt.plot(fss, gofs, marker='.', markersize=5)


Out[158]:
[<matplotlib.lines.Line2D at 0x7f2f06a388d0>]

In [ ]:


In [135]:
optresult_nr = optimize.minimize(
    lambda x: gof(fs=x[0], t3=x[1], tts=x[2], dset = 'nr', error_pct = 1e-2, error_offset = 1e-4),
    [0.2, 24, 3],
    bounds=[[.01, 1], [20, 30], [.1, 5]],
    options=dict(maxfev=1000),
    method='Powell',
)


/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py:394: RuntimeWarning: Method Powell cannot handle constraints nor bounds.
  RuntimeWarning)
gof=5.404165191595393, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=4.901261384945134, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=1.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-1.418034, t1=3.1, t3=24.0, tts=3.0
gof=5.100265985289571, fs=0.2, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-0.418033974844, t1=3.1, t3=24.0, tts=3.0
/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py:1876: RuntimeWarning: invalid value encountered in double_scalars
  tmp2 = (x - v) * (fx - fw)
gof=20.667156369428504, fs=0.581966, t1=3.1, t3=24.0, tts=3.0
gof=inf, fs=-0.036067965235263316, t1=3.1, t3=24.0, tts=3.0
gof=1.5463955247032297, fs=0.34589802515600004, t1=3.1, t3=24.0, tts=3.0
gof=1.6943681213633022, fs=0.31710503490593894, t1=3.1, t3=24.0, tts=3.0
gof=1.9966769477411725, fs=0.34735700541756004, t1=3.1, t3=24.0, tts=3.0
gof=1.5504039444214925, fs=0.3317492787233509, t1=3.1, t3=24.0, tts=3.0
gof=1.5494328170231437, fs=0.3404936850761068, t1=3.1, t3=24.0, tts=3.0
gof=1.665766759993852, fs=0.34383375099304353, t1=3.1, t3=24.0, tts=3.0
gof=1.7171111053948453, fs=0.34589802515600004, t1=3.1, t3=24.0, tts=3.0
gof=2.0321923608937515, fs=0.34589802515600004, t1=3.1, t3=25.0, tts=3.0
gof=1.9879370439724404, fs=0.34589802515600004, t1=3.1, t3=22.381966, tts=3.0
gof=1.6604266196241677, fs=0.34589802515600004, t1=3.1, t3=24.0, tts=3.0
gof=1.7296053834654685, fs=0.34589802515600004, t1=3.1, t3=23.381966025156, tts=3.0
gof=1.7493126792831641, fs=0.34589802515600004, t1=3.1, t3=24.381966, tts=3.0
gof=1.8050421732329962, fs=0.34589802515600004, t1=3.1, t3=23.853374907101102, tts=3.0
gof=1.4616305038363622, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=3.0
gof=1.9075388106097961, fs=0.34589802515600004, t1=3.1, t3=24.236067965235264, tts=3.0
gof=1.670500796676093, fs=0.34589802515600004, t1=3.1, t3=24.090169940079264, tts=3.0
gof=1.839446498436374, fs=0.34589802515600004, t1=3.1, t3=24.180339876488315, tts=3.0
gof=1.7500858334966323, fs=0.34589802515600004, t1=3.1, t3=24.124611791411578, tts=3.0
gof=1.8273471534420773, fs=0.34589802515600004, t1=3.1, t3=24.159053641342, tts=3.0
gof=1.657661491875276, fs=0.34589802515600004, t1=3.1, t3=24.13776740759758, tts=3.0
gof=1.9253259252524055, fs=0.34589802515600004, t1=3.1, t3=24.1509230232481, tts=3.0
gof=1.6679830361325048, fs=0.34589802515600004, t1=3.1, t3=24.14279240568968, tts=3.0
gof=1.8766230091130698, fs=0.34589802515600004, t1=3.1, t3=24.147817403577246, tts=3.0
gof=1.6674872956909201, fs=0.34589802515600004, t1=3.1, t3=24.14443904489444, tts=3.0
gof=1.792266460046405, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=3.0
gof=3.0958202692485135, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=4.0
gof=1.9919909756078493, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.381966
gof=1.7370687264387408, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=3.0
gof=1.7109866877958788, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=2.381966025156
gof=1.6709329968553515, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=2.000000015547263
gof=1.86459792063448, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.7639320347647374
gof=1.9380858749888314, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=2.145898044373474
gof=1.5869436785666675, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.8488534465411777, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.8541019830508416
gof=1.798744894847855, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.94427192539842
gof=1.6892701161273085, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.8885438375178918
gof=1.7344069712660557, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.9229856897166269
gof=13.367786819692501, fs=0.4917960503120001, t1=3.1, t3=24.291796050312, tts=0.8196601463993698
gof=1.7540055989253232, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=inf, fs=1.345898025156, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=inf, fs=-1.272135974844, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.9639673595616576, fs=0.34589802515600004, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=inf, fs=-0.272135949688, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=52.87454502163876, fs=0.727864025156, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=12.129726899003304, fs=0.10983005992073672, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.2972942844861777, fs=0.3033234532747192, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.1569481946015234, fs=0.2988119449116414, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=2.3476145469240457, fs=0.22662729022920552, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.305113766673878, fs=0.2712398611012101, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.2159867595489782, fs=0.2873888255985833, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.0627243087975187, fs=0.29423548493201857, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.0051858729108252, fs=0.2937188595197787, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.6105584870290088, fs=0.2913010017830354, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.0055679728025029, fs=0.2927953200715058, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.3702594228519074, fs=0.2937188595197787, t1=3.1, t3=24.145898025156, tts=1.909830073199685
gof=1.9598290285638795, fs=0.2937188595197787, t1=3.1, t3=25.145898025156, tts=1.909830073199685
gof=0.718748122292832, fs=0.2937188595197787, t1=3.1, t3=22.527864025156, tts=1.909830073199685
gof=2.0162576942393464, fs=0.2937188595197787, t1=3.1, t3=20.51694684612559, tts=1.909830073199685
gof=0.9029065104775797, fs=0.2937188595197787, t1=3.1, t3=22.527864025156, tts=1.909830073199685
gof=0.9315260725890969, fs=0.2937188595197787, t1=3.1, t3=21.759762033950473, tts=1.909830073199685
gof=0.7435275469638049, fs=0.2937188595197787, t1=3.1, t3=23.145898000000003, tts=1.909830073199685
gof=0.8755038555103863, fs=0.2937188595197787, t1=3.1, t3=23.527864009608738, tts=1.909830073199685
gof=0.7612382329234786, fs=0.2937188595197787, t1=3.1, t3=23.050570926699166, tts=1.909830073199685
gof=0.836657954797818, fs=0.2937188595197787, t1=3.1, t3=23.181684908377687, tts=1.909830073199685
gof=0.9186456933552384, fs=0.2937188595197787, t1=3.1, t3=23.102602861964385, tts=1.909830073199685
gof=0.7671858284838113, fs=0.2937188595197787, t1=3.1, t3=23.129360729305088, tts=1.909830073199685
gof=0.7142404390650213, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.909830073199685
gof=0.8800359086770158, fs=0.2937188595197787, t1=3.1, t3=23.1694306886845, tts=1.909830073199685
gof=0.9183002139393013, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.909830073199685
gof=1.3024748348607489, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=2.909830073199685
gof=1.7538957152550838, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=0.2917960731996849
gof=0.8999017353799202, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.909830073199685
gof=0.9549537283309599, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.2917960983556849
gof=1.0248336043798423, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=2.291796073199685
gof=0.847265752712253, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.7078365048144708
gof=0.9144920669588263, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.6538065320797362
gof=0.7589156116278184, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.786666157102007
gof=0.9741773701823139, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.797216308645579
gof=0.786603719227915, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.7854345179310303
gof=0.9423904263829267, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.790695956286499
gof=0.823623655951927, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.7882054033773107
gof=0.6387378577350729, fs=0.2415396938835574, t1=3.1, t3=22.173236739334783, tts=1.6635022410043292
gof=0.9000027038583064, fs=0.2937188595197787, t1=3.1, t3=23.159567382245392, tts=1.786666157102007
gof=0.5679517771913777, fs=0.2415396938835574, t1=3.1, t3=22.173236739334783, tts=1.6635022410043292
gof=1.9599925176805482, fs=0.15711202979251968, t1=3.1, t3=20.57732022386356, tts=1.4642188371851392
gof=0.6051470596115548, fs=0.2415396938835574, t1=3.1, t3=22.173236739334783, tts=1.6635022410043292
gof=0.8855376506999983, fs=0.2092911967413601, t1=3.1, t3=21.563650891586303, tts=1.5873827563811285
gof=0.5813559783577003, fs=0.2614703610649623, t1=3.1, t3=22.549981509684777, tts=1.710546669380495
gof=0.6428880733642607, fs=0.2556568673579759, t1=3.1, t3=22.44009038904968, tts=1.6968244750442822
gof=0.6728717332943599, fs=0.2737881910257547, t1=3.1, t3=22.782822587083263, tts=1.73962172562753
gof=0.7012844852559342, fs=0.26617535330376635, t1=3.1, t3=22.638918884654366, tts=1.7216523523149498
gof=0.7119897598285514, fs=0.2592498041276795, t1=3.1, t3=22.508006837900272, tts=1.705305257698669
gof=0.588446769713751, fs=0.26326750813044936, t1=3.1, t3=22.58395256305241, tts=1.714788662668237
gof=0.6837629186377729, fs=0.26062218381385616, t1=3.1, t3=22.533948612201936, tts=1.7085446283260346
gof=0.5912008367760218, fs=0.26215681014097814, t1=3.1, t3=22.562957297055398, tts=1.7121669665886405
gof=0.68493047662226, fs=0.2611463861930663, t1=3.1, t3=22.543857487964846, tts=1.7097819577670867
gof=0.5488720662354063, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.7113078642589417
gof=0.5842225632591371, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.7113078642589417
gof=1.2668656798590343, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=2.7113078642589414
gof=1.0547438538574492, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=0.09327386425894169
gof=0.6636992928385619, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.7113078642589417
gof=0.6743764272804212, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.0932738894149416
gof=0.7834345052881534, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=2.0932738642589417
gof=0.5094180294542348, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4284074782939662
gof=0.6125553790785518, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.407436125333753
gof=0.6066819087019168, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.5546913723184943
gof=0.6343196253734885, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4766436321589391
gof=0.6306937284768774, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4468320490411544
gof=0.5707948347774392, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4203971344891655
gof=0.5792711253269454, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4354450378839867
gof=0.5601014621034156, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4255784744243165
gof=0.6124644419165034, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4312364821636159
gof=0.5934232284525206, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4284074782939662
gof=1.2425632347363833, fs=0.2617928460500323, t1=3.1, t3=23.556077368420247, tts=1.4284074782939662
gof=1.2336374901965115, fs=0.2617928460500323, t1=3.1, t3=20.938043368420246, tts=1.4284074782939662
gof=0.5468477078691328, fs=0.2617928460500323, t1=3.1, t3=22.556077368420247, tts=1.4284074782939662
gof=0.7010247622387107, fs=0.2617928460500323, t1=3.1, t3=21.938043393576248, tts=1.4284074782939662
gof=0.7498194719590847, fs=0.2617928460500323, t1=3.1, t3=22.938043368420246, tts=1.4284074782939662
gof=0.5510057035723211, fs=0.2617928460500323, t1=3.1, t3=22.406798798052332, tts=1.4284074782939662
gof=0.6231285625334002, fs=0.2617928460500323, t1=3.1, t3=22.510896807168777, tts=1.4284074782939662
gof=0.6306239443454276, fs=0.2617928460500323, t1=3.1, t3=22.701975393576248, tts=1.4284074782939662
gof=0.5847882560317943, fs=0.2617928460500323, t1=3.1, t3=22.611805453496984, tts=1.4284074782939662
gof=0.5830141863339271, fs=0.2617928460500323, t1=3.1, t3=22.577363602164667, tts=1.4284074782939662
gof=0.5258355953329317, fs=0.2617928460500323, t1=3.1, t3=22.538819930161267, tts=1.4284074782939662
gof=0.670156827073521, fs=0.2617928460500323, t1=3.1, t3=22.52815424656432, tts=1.4284074782939662
gof=0.6861606340491522, fs=0.2617928460500323, t1=3.1, t3=22.545411684823296, tts=1.4284074782939662
gof=0.514040957731117, fs=0.2617928460500323, t1=3.1, t3=22.534746001660476, tts=1.4284074782939662
gof=0.6124746876897894, fs=0.2617928460500323, t1=3.1, t3=22.53222817533342, tts=1.4284074782939662
gof=0.5447891868143776, fs=0.2617928460500323, t1=3.1, t3=22.53630210383421, tts=1.4284074782939662
gof=0.5515011501339631, fs=0.2617928460500323, t1=3.1, t3=22.533784277609634, tts=1.4284074782939662
gof=0.541639805608616, fs=0.2617928460500323, t1=3.1, t3=22.535340379783367, tts=1.4284074782939662
gof=0.5654839262111898, fs=0.2617928460500323, t1=3.1, t3=22.534378655771672, tts=1.4284074782939662
gof=0.5118566366298087, fs=0.2617928460500323, t1=3.1, t3=22.534973033894566, tts=1.4284074782939662
gof=0.5587656107945091, fs=0.2617928460500323, t1=3.1, t3=22.534973033894566, tts=1.4284074782939662
gof=0.6165279095458054, fs=0.22986683258028584, t1=3.1, t3=21.93148302006942, tts=1.3530491854509008
gof=1.5277878320291298, fs=0.31345022132854, t1=3.1, t3=23.511440394924122, tts=1.5503397582960028
gof=0.6751303371565298, fs=0.2617928460500323, t1=3.1, t3=22.534973033894566, tts=1.4284074782939662
gof=0.61335784464188, fs=0.2815242070556628, t1=3.1, t3=22.907950365917582, tts=1.474981463557224
gof=0.9892695896816763, fs=0.2937188590234166, t1=3.1, t3=23.138463038337093, tts=1.5037657699654163
gof=0.629302354527487, fs=0.27313025685423253, t1=3.1, t3=22.749281475800466, tts=1.4551683491516656
gof=0.767236444598181, fs=0.28607338562694373, t1=3.1, t3=22.993942431337118, tts=1.48571936327323
gof=0.7045128815272208, fs=0.27831800347302327, t1=3.1, t3=22.847344244635106, tts=1.4674135275001905
gof=0.8968870644491622, fs=0.28326183859782067, t1=3.1, t3=22.94079641117762, tts=1.479082976160148
gof=0.6949707138556176, fs=0.2802995462980163, t1=3.1, t3=22.8848008881958, tts=1.4720907692932632
gof=0.6923934216517842, fs=0.2821879232252947, t1=3.1, t3=22.92049643844138, tts=1.4765481019201125
gof=0.7967676581817236, fs=0.28105642828470756, t1=3.1, t3=22.899108052510105, tts=1.473877316631996
gof=1.0246818526251227, fs=0.2817777240661124, t1=3.1, t3=22.912742539055206, tts=1.4755798661461432
gof=0.7606878182821294, fs=0.2813268934452872, t1=3.1, t3=22.904220592591315, tts=1.474515723703838

In [ ]:


In [137]:
fit = optresult_nr.x
print(fit)
ydata, _ = real_s1_wv(dset='nr')
minus, base, plus = s1_models_error(fs=fit[0], t3=fit[1], tts=fit[2], dset='nr', error_pct = 1e-2, error_offset = 1e-4)
comparison_plot(ydata, minus, base, plus, error_pct = 1e-2, error_offset = 1e-4)
plt.show()
comparison_plot_2(ydata, minus, base, plus, error_pct = 1e-2, error_offset = 1e-4)
for _l in (-15, 125):
    plt.axvline(_l)
plt.xlim(-50, 200)
plt.show()


[  0.28152421  22.90795037   1.47498146]
chi2 = 0.700723

In [ ]:
plt.hist(xams_data['er']['s1'], bins=100, histtype='step', range=(50,100))
plt.hist(xams_data['nr']['s1'], bins=100, histtype='step', range=(50,100))
plt.show()

Fit four parameters

ER


In [4]:
from scipy import optimize
optresult = optimize.minimize(
    lambda x: gof(fs=x[0], t1=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, dst='er'),
    [0.2, 3.1, 24, 3],
    bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],
    options=dict(maxfev=1000),
    method='Powell',
)


/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py:394: RuntimeWarning: Method Powell cannot handle constraints nor bounds.
  RuntimeWarning)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-bc75bb107e61> in <module>()
      5     bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],
      6     options=dict(maxfev=1000),
----> 7     method='Powell',
      8 )

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    438         return _minimize_neldermead(fun, x0, args, callback, **options)
    439     elif meth == 'powell':
--> 440         return _minimize_powell(fun, x0, args, callback, **options)
    441     elif meth == 'cg':
    442         return _minimize_cg(fun, x0, args, jac, callback, **options)

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py in _minimize_powell(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, direc, return_all, **unknown_options)
   2442         direc = asarray(direc, dtype=float)
   2443 
-> 2444     fval = squeeze(func(x))
   2445     x1 = x.copy()
   2446     iter = 0

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
    290     def function_wrapper(*wrapper_args):
    291         ncalls[0] += 1
--> 292         return function(*(wrapper_args + args))
    293 
    294     return ncalls, function_wrapper

<ipython-input-4-bc75bb107e61> in <lambda>(x)
      1 from scipy import optimize
      2 optresult = optimize.minimize(
----> 3     lambda x: gof(fs=x[0], t1=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, dst='er'),
      4     [0.2, 3.1, 24, 3],
      5     bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],

NameError: name 'gof' is not defined

In [ ]:
# fit = optresult.x
# ydata, _ = real_s1_wv()
# minus, base, plus = s1_models_error(fs=fit[0], t1=fit[1], t3=fit[2], tts=fit[3])
# comparison_plot(ydata, minus, base, plus)

In [ ]:
fit = optresult.x
print(fit)
ydata, _ = real_s1_wv()
minus, base, plus = s1_models_error(fs=fit[0], t1=fit[1], t3=fit[2], tts=fit[3], s1_min=30, s1_max = 100)
comparison_plot(ydata, minus, base, plus)
plt.show()
comparison_plot_2(ydata, minus, base, plus)
for _l in (-20, 100):
    plt.axvline(_l)
plt.xlim(-50, 200)
plt.show()

The fit is pushing the singlet livetime to very low values... There is some degeneracy here, and also some mis-modeling, it seems. The sample at 0 is always under-estimated. Why? Maybe because the tts is actually quite low but modeled here as large. The effects may not be symmetric: there are many things causing a delay, but not a negative delay.

NR


In [5]:
from scipy import optimize
optresult = optimize.minimize(
    lambda x: gof(fs=x[0], t1=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, dst='nr'),
    [0.2, 3.1, 24, 3],
    bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],
    options=dict(maxfev=1000),
    method='Powell',
)


/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py:394: RuntimeWarning: Method Powell cannot handle constraints nor bounds.
  RuntimeWarning)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-9c0036ed314d> in <module>()
      5     bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],
      6     options=dict(maxfev=1000),
----> 7     method='Powell',
      8 )

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    438         return _minimize_neldermead(fun, x0, args, callback, **options)
    439     elif meth == 'powell':
--> 440         return _minimize_powell(fun, x0, args, callback, **options)
    441     elif meth == 'cg':
    442         return _minimize_cg(fun, x0, args, jac, callback, **options)

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py in _minimize_powell(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, direc, return_all, **unknown_options)
   2442         direc = asarray(direc, dtype=float)
   2443 
-> 2444     fval = squeeze(func(x))
   2445     x1 = x.copy()
   2446     iter = 0

/home/erik/anaconda3/envs/pax_new/lib/python3.4/site-packages/scipy/optimize/optimize.py in function_wrapper(*wrapper_args)
    290     def function_wrapper(*wrapper_args):
    291         ncalls[0] += 1
--> 292         return function(*(wrapper_args + args))
    293 
    294     return ncalls, function_wrapper

<ipython-input-5-9c0036ed314d> in <lambda>(x)
      1 from scipy import optimize
      2 optresult = optimize.minimize(
----> 3     lambda x: gof(fs=x[0], t1=x[1], t3=x[2], tts=x[3], s1_min=30, s1_max = 100, dst='nr'),
      4     [0.2, 3.1, 24, 3],
      5     bounds=[[.01, 1], [.1, 5], [20, 30], [.1, 5]],

NameError: name 'gof' is not defined

In [ ]:
fit = optresult.x
print(fit)
ydata, _ = real_s1_wv()
minus, base, plus = s1_models_error(fs=fit[0], t1=fit[1], t3=fit[2], tts=fit[3], s1_min=30, s1_max = 100, dset='nr')
comparison_plot(ydata, minus, base, plus)
plt.show()
comparison_plot_2(ydata, minus, base, plus)
for _l in (-20, 100):
    plt.axvline(_l)
plt.xlim(-50, 200)
plt.show()

In [ ]: