def correctforbackground(e, data, portion):
    ne = len(e)
    aproxx = np.append(e[:int(ne*portion)], e[-int(ne*portion):])
    aproxy = np.append(data[:int(ne*portion)], data[-int(ne*portion):])
    polymodel = ws.wpolyfit(aproxx, aproxy, degree=1)
    valuestofft = abs(data - polymodel(e))
    return valuestofft        
Esempio n. 2
0
    def time_per_point(self, drop=2):
        #logging.debug("time_per_point");
        import wsolve
        if self.columns is {}: return 0
        if not ('PT' in self.columns and 'TIMESTAMP' in self.columns): return 0
        # Measurement point
        current_point = self.columns['PT'][-1]
        all_points = numpy.arange(self.points)
        if current_point < 2: return 0 * all_points
        # Time steps
        t = numpy.asarray(self.columns['TIMESTAMP'])
        dt = t[1:] - t[:-1]
        pt = numpy.arange(1, len(t))
        simple_guess = numpy.mean(dt[0]) * numpy.ones_like(all_points)

        # If you don't have many points to go on, assume the time is constant
        if len(pt) < 5:
            return simple_guess

        # Weight more recent points more strongly
        weight = 1. / numpy.sqrt(1 + numpy.arange(len(dt)))

        # Constant,linear,quadratic models, with two worst residuals removed
        models = []
        bics = []
        for degree in range(3):
            fn = wsolve.wpolyfit(pt, dt, dy=weight, degree=degree)
            if drop > 0:
                idx = numpy.argsort(abs(fn(pt) - dt))[:-drop]
                fn = wsolve.wpolyfit(pt[idx],
                                     dt[idx],
                                     dy=weight[idx],
                                     degree=degree)
                BIC_p = BIC(k=degree + 1, fx=fn(pt[idx]), y=dt[idx])
            else:
                BIC_p = BIC(k=degree + 1, fx=fn(pt), y=dt)
            models.append(fn)
            bics.append(BIC_p)

        if True:
            logfn = wsolve.wpolyfit(pt,
                                    numpy.log(dt - dt[0] + 20),
                                    dy=weight,
                                    degree=1)
            fn = lambda pt: numpy.exp(logfn(pt)) + dt[0] - 20
            BIC_p = BIC(k=2, fx=fn(pt), y=dt)
            models.append(fn)
            bics.append(BIC_p)
        #print "bics",bics

        # Exponential model
        if self.instrument in ['BT4'] and 'ENERGY' in self.independent:
            pars, BIC_p = fit_exp(pt, dt, dy=weight, drop=drop)
            models.append(lambda x, p=pars: exponential(p, x))
            bics.append(BIC_p)

        # Power model
        if False and self.instrument in ['NG7']:
            pars, BIC_p = robust_fit(power, [0, 1, 2],
                                     pt,
                                     dt,
                                     dy=weight,
                                     drop=drop)
            models.append(lambda x, p=pars: power(p, x))
            bics.append(BIC_p)

        # Toss model and associated bic if the model predicts negative measurement time
        valid = zip(*[(m, b) for m, b in zip(models, bics)
                      if (m(all_points) > 0).all()])
        if not valid: return simple_guess
        models, bics = valid
        best = numpy.argmin(bics)  # Choose lowest BIC
        # BIC is too accepting - be selective in its use
        if self.instrument == 'BT4' and 'ENERGY' in self.independent:
            if self.points > 10 and bics[best] < 2 * bics[-1]:
                idx = -1
            else:
                idx = best
            #idx = -1
        elif self.instrument == 'NG7' and 'QZ' in self.columns:
            idx = best
        else:
            # For other scans use the constant approximation
            idx = 0
        model = models[idx]

        # If measurement estimate is for more than one day, then reject
        times = models[idx](all_points)
        if sum(times) > 60 * 60 * 24 * 10:
            times = models[0](all_points)

        # Finally, compute the predicted duration
        return times
Esempio n. 3
0
    def time_per_point(self,drop=2):
        import wsolve
        if self.columns is {}: return 0
        if not ('PT' in self.columns and 'TIMESTAMP' in self.columns): return 0
        # Measurement point
        current_point = self.columns['PT'][-1]
        all_points = numpy.arange(self.points)
        if current_point < 2: return 0*all_points
        # Time steps
        t = numpy.asarray(self.columns['TIMESTAMP'])
        dt = t[1:]-t[:-1]
        pt = numpy.arange(1,len(t))

        # If you don't have many points to go on, assume the time is constant
        if len(pt) < 5:
            return numpy.mean(dt[0])*numpy.ones_like(all_points)

        # Weight more recent points more strongly
        weight = 1./numpy.arange(len(dt))

        # Constant,linear,quadratic models, with two worst residuals removed
        models = []
        bics = []
        for degree in range(3):
            fn = wsolve.wpolyfit(pt,dt,dy=weight,degree=degree)
            if drop > 0:
                idx = numpy.argsort(abs(fn(pt)-dt))[:-drop]
                fn = wsolve.wpolyfit(pt[idx], dt[idx], dy=weight[idx], degree=degree)
                BIC_p = BIC(k=degree+1, fx=fn(pt[idx]), y=dt[idx])
            else:
                BIC_p = BIC(k=degree+1, fx=fn(pt), y=dt)
            models.append(fn)
            bics.append(BIC_p)

        if True:
            logfn = wsolve.wpolyfit(pt,numpy.log(dt-dt[0]+20),
                                    dy=weight, degree=1)
            fn = lambda pt: numpy.exp(logfn(pt))+dt[0]-20
            BIC_p = BIC(k=2, fx=fn(pt), y=dt)
            models.append(fn)
            bics.append(BIC_p)
        #print "bics",bics

        # Exponential model
        if self.instrument in ['BT4']:
            pars,BIC_p = fit_exp(pt,dt,dy=weight,drop=drop)
            models.append(lambda x,p=pars: exponential(p,x))
            bics.append(BIC_p)

        # Power model
        if False and self.instrument in ['NG7']:
            pars,BIC_p = robust_fit(power, [0,1,2], pt, dt,
                                    dy=weight, drop=drop)
            models.append(lambda x,p=pars: power(p,x))
            bics.append(BIC_p)

        # Toss model and associated bic if the model predicts negative measurement time
        models,bics = zip(*[(m,b) for m,b in zip(models,bics)
                            if (m(all_points)>0).all()])
        best = numpy.argmin(bics) # Choose lowest BIC
        # BIC is too accepting - be selective in its use
        if self.instrument == 'BT4':
            if self.points>20 and bics[best] < 2*bics[-1]:
                idx = -1
            else:
                idx = best
            idx = -1
        elif self.instrument == 'NG7' and 'QZ' in self.columns:
            idx = best
        else:
            # For other scans use the constant approximation
            idx = 0
        model = models[idx]

        # If measurement estimate is for more than one day, then reject
        times = models[idx](all_points)
        if sum(times) > 60*60*24*2:
            times = models[0](all_points)

        # Finally, compute the predicted duration
        return times
Esempio n. 4
0
bounds=(-20,-inf),(40,inf)
sigma = 1
data = fn((1,1)) + randn(*x.shape)*sigma  # Fake data


# Sample from the posterior density function
n=2
model = Simulation(f=fn, data=data, sigma=sigma, bounds=bounds,
                   labels=["x","y"])
sampler = Dream(model=model,
                population=randn(5*n,4,n),
                thinning=1,
                draws=20000,
                )
mc = sampler.sample()
mc.title = 'Strong anti-correlation'

# Create a derived parameter without the correlation
mc.derive_vars(lambda p: (p[0]+p[1]), labels=['x+y'])

# Compare the MCMC estimate for the derived parameter to a least squares fit
from wsolve import wpolyfit
poly = wpolyfit(x,data,degree=1,origin=True)
print("x+y from linear fit", poly.coeff[0], poly.std[0])
points,logp = mc.sample(portion=0.5)
print("x+y from MCMC",mean(points[:,2]), std(points[:,2],ddof=1))

# Plot the samples
plot_all(mc, portion=0.5)
show()