def nonlinear_fit(d, e, initial_guess):
        """
        d   : list of values of the set of discretization
              parameters in each experiment:
              d = ((d_1,d_2,d_3),(d_1,d_2,d_3,),...)
              d[i] provides the values of the discretization
              parameters in experiement no. i
        e   : list of error values; e = (e_1, e_2, ...)
              e[i] is the error associated with the parameters
              d[i]

        initial_guess is the starting value for the unknown
        parameters vector.
        """
        if len(d) != len(e):
            raise ValueError, 'len(d) != len(e)'
        # transform d and e to the data format required by
        # the Scientific package:
        data = []
        for d_i, e_i in zip(d, e):
            if isinstance(d_i, (float, int)):
                data.append(((d_i,), e_i))
            else:  # d_i is tuple, list, array, NumArray, ...
                data.append((d_i, e_i))
        sol = leastSquaresFit(ManyDiscretizationPrm.error_model,
                              initial_guess, data)
        # return list of fitted parameters (p in error_model)
        # (sol[1] is a measure of the quality of the fit)
        return sol[0]  
Example #2
0
 def analyze(self, task):
     self.initialGuess(task)
     p = [self.coeff[c] for c in self.varCoeffs]
     (p, self.chisq) = leastSquaresFit(self.func, p, self.chooseXY(task))
     for i in range(len(self.varCoeffs)):
         self.coeff[self.varCoeffs[i]] = p[i]
     del self.func
    def nonlinear_fit(d, e, initial_guess):
        """
        d   : list of values of the (single) discretization
              parameter in each experiment:
              d[i] provides the values of the discretization
              parameter in experiement no. i
        e   : list of error values; e = (e_1, e_2, ...)
              e[i] is the error associated with the parameters
              d[i]

        initial_guess is the starting value for the unknown
        parameters vector.
        """
        if len(d) != len(e):
            raise ValueError, 'd and e must have the same length'
        if not isinstance(d[0], (float,int)):
            raise TypeError, \
            'd must be an array of numbers, not %s' % str(type(d[0]))
        # transform d and e to the data format required by
        # the Scientific package:
        data = []
        for d_i, e_i in zip(d, e):
            data.append(((d_i,) , e_i))  # recall (a,) conversion to tuple
        sol = leastSquaresFit(OneDiscretizationPrm.error_model,
                              initial_guess, data)
        return sol[0]
    def fit(self):
        self.setStartParam()
        fitParam, chi = leastSquaresFit(self.func, self.startParam, self.data)
        fitData, akaike = self.akaike(fitParam)

        # wszystkie model oczekuja parametrow >= 0
        if fitParam[0] <= 0:
            raise Exception, "alfa %f mniejsza od 0! " % fitParam[0]

        if len(fitParam) > 1 and fitParam[1] <= 0:
            raise Exception, "beta %f mniejsza od 0! " % fitParam[1]

        return (fitParam, chi, fitData, akaike)
Example #5
0
 def __call__(self, t, data, param = [10000, 200, 300000, 2000]):
     """
     Sets the Star object parameters to the best fit. param can be changed
     for better performance. Be aware that there might be more than one fix
     point for each data-set, so inspect the result to see wheter it is a 
     realistic fit. Usually this is no problem if the initializing parameters 
     are sensible.
     """
     # create list of tuples
     values = [];
     print len(data);
     print len(t);
     for i in range(len(t)):
         values.append([t[i], data[i]]);
     values = np.asarray(values);
     return leastSquaresFit(self.modelFunction, param, values)
Example #6
0
 def __call__(self, t, data, param=[10000, 200, 300000, 2000]):
     """
     Sets the Star object parameters to the best fit. param can be changed
     for better performance. Be aware that there might be more than one fix
     point for each data-set, so inspect the result to see wheter it is a 
     realistic fit. Usually this is no problem if the initializing parameters 
     are sensible.
     """
     # create list of tuples
     values = []
     print len(data)
     print len(t)
     for i in range(len(t)):
         values.append([t[i], data[i]])
     values = np.asarray(values)
     return leastSquaresFit(self.modelFunction, param, values)
Example #7
0
def equilibrate(atoms, dyn, temp):
    print "Equilibrating ..."
    tstart = time.time()
    temperatures = []
    for i in xrange(1,nequil/nminor+1):
        dyn.run(nminor)
        ekin = atoms.get_kinetic_energy() / len(atoms)
        temperatures.append((i*nminor*timestep, 2.0/3.0 * ekin))
        if i % nequilprint == 0:
            #data = array(temperatures)
            #print data.shape
            try:
                (a, b, c) = leastSquaresFit(targetfunc, (0.1, 2*frict, temp),
                                            temperatures)[0]
            except OverflowError:
                print "leastSquaresFit failed (this is OK)."
                a = b = c = 0.0
            print "%.6f  T_inf = %.6f (goal: %f)  k = %.6f" % \
                  (ekin, c, temp, b)
    tequil = time.time() - tstart
    print "This took %.1f minutes." % (tequil / 60)
    print "Taking data - this takes", repeats*nsteps/nequil, "times longer!"
Example #8
0
C = 1
a = 2
D = 2
b = 1
p = (C, a, D, b)
dx = 0.5
dt = 1.0
for i in range(7):
    dx /= 2
    dt /= 2
    e = error_model(p, (dx, dt))  # use the model to generate data
    e += random.gauss(0, 0.01 * e)  # perturb data randomly
    data.append(((dx, dt), e))

from Scientific.Functions.LeastSquares import leastSquaresFit
parameter_guess = (1, 2, 2, 1)  # exact guess...
r = leastSquaresFit(error_model, parameter_guess, data)
print r
# try different initial guesses:
array_output_precision(4)
for i in range(6):
    pg = array(p,
               Float) + i * 0.1 * sign(random.uniform(-1, 1)) * ones(4, Float)
    r = leastSquaresFit(error_model, pg, data)
    print "guess:", str(pg)
    deviation = array(p) - array(r[0])
    print " deviation:", deviation

import sys
sys.exit(0)
def rateFilter(sampledRGB, filterRadius, exposedTime, flowRate, bcgradient, gradient, parenttags=None, level=logging.ERROR):
    ''' Computes the BCArea and BCVolume subject to the params
    
    Keyword arguments:
    sampledRGB  -- The sampled RGB values of the BCFilter
    exposedTime -- The duration for which the filter was exposed to air from the pump 
    flowRate    -- The flow rate of the pump
    bcgradient  -- The calibration value obtained from the database
    gradient    -- The values corresponding to the color values from GrayBar objects
    parenttags  -- The tag string of the calling function
    level       -- The logging level
    
    Returns:
    BCCResult object
    '''
    
    # Set the logging level
    log.setLevel(level)
    tags = parenttags + " BCCCOMPUTATION"
        
    try:
        fitParam = BCCCalculatorConstants.FittingParameters
        stop     = BCCCalculatorConstants.StoppingLimit
        expmod   = Rating.expmod
        rsquared = Rating.rsquared
    
        # The results of this computation
        bccResult = BCCResult()
    
        # separate by color leaving off the black and white
        gradientRed, gradientGreen, gradientBlue = zip(*gradient[1:-1])
    
        # fit the gradient
        bccResult.fitRed, chi = leastSquaresFit(expmod, fitParam, zip(gradientRed, bcgradient), stopping_limit=stop)
        bccResult.fitGreen, chi = leastSquaresFit(expmod, fitParam, zip(gradientGreen, bcgradient), stopping_limit=stop)
        bccResult.fitBlue, chi = leastSquaresFit(expmod, fitParam, zip(gradientBlue, bcgradient), stopping_limit=stop)
    
        # compute the rsquared value
        bccResult.rSquaredRed = rsquared(expmod, bccResult.fitRed, pylab.array(gradientRed), bcgradient)
        bccResult.rSquaredGreen = rsquared(expmod, bccResult.fitGreen, pylab.array(gradientRed), bcgradient)
        bccResult.rSquaredBlue = rsquared(expmod, bccResult.fitBlue, pylab.array(gradientRed), bcgradient)
    
        red, green, blue = sampledRGB
        
        bccResult.BCAreaRed = expmod(bccResult.fitRed, red)
        bccResult.BCAreaGreen = expmod(bccResult.fitGreen, green)
        bccResult.BCAreaBlue = expmod(bccResult.fitBlue, blue)
    
        log.info('Computing Black Carbon Concentration: ', extra=tags)
        bccResult.BCVolRed   = computeBCC(filterRadius, bccResult.BCAreaRed, exposedTime ,flowRate)
        bccResult.BCVolGreen = computeBCC(filterRadius, bccResult.BCAreaGreen, exposedTime, flowRate)
        bccResult.BCVolBlue = computeBCC(filterRadius, bccResult.BCAreaBlue, exposedTime, flowRate)
    
        log.info('Black carbon per cm^2: %s', ([bccResult.BCAreaRed, bccResult.BCAreaGreen, bccResult.BCAreaBlue],), extra=tags)
        log.info('Black carbon per cm^3: %s', ([bccResult.BCVolRed, bccResult.BCVolGreen, bccResult.BCVolBlue],), extra=tags)
        
        log.info('Done Computing Black Carbon Concentration: ', extra=tags)
        return bccResult, ExitCode.Success
    
    except Exception, err:
        log.error('Error %s' % str(err), extra=tags)
        return None, ExitCode.BCCComputationError
Example #10
0
    def fit(self):
        """
        Determine of data for optimal fit and fit
        """
        Yr = [x.real for x in self.admitance]
        maxYr = max(Yr)
        maxYrIndex = Yr.index(maxYr)
        fRes = self.frequency[maxYrIndex]

        # Hladanie P% +- bodov
        P = 10
        minYr = min(Yr)
        hod = maxYr - (((maxYr - minYr) / 100) * P)

        lava = maxYrIndex
        for i in range(maxYrIndex, 0, -1):
            if Yr[i] > hod:
                lava = i

        prava = maxYrIndex
        for i in range(maxYrIndex, len(self.frequency), 1):
            if Yr[i] > hod:
                prava = i

        #prava = maxYrIndex + (maxYrIndex - lava)

        # vyrez
        self.vyrez = Yr[lava:prava]
        self.vyref = self.frequency[lava:prava]

        self.vyref_fit = []
        for i in range(len(self.vyref)):
            self.vyref_fit.append(i)

        # Fitovanie Realnej zlozky admitancie
        datafit = []
        for i in range(len(self.vyref)):
            datafit.append((i, self.vyrez[i]))
        guess = (1, 1, 1)
        fit_params, fit_error = leastSquaresFit(func, guess, datafit)
        #print fit_params, fit_error
        self.fResonanceFit = (- fit_params[1] ) / (2 * fit_params[0]) * ((self.vyref[-1] - self.vyref[0]) / (len(self.vyref) - 1)) + self.vyref[0]
        #print self.fResonanceFit

    
        for i in range(len(self.vyref)):
            self.yre_fit.append(func(fit_params, i))

        maxY = max(self.yre_fit)
        indexmax = self.yre_fit.index(max(self.yre_fit))
        self.fResonance = self.vyref[indexmax]

        # fitovanie Imaginarnej zlozky admitancie
        Yi = [x.imag for x in self.admitance]
        self.vyrezimag = Yi[lava:prava]

        datafitimag = []

        for i in range(len(self.vyref)):
            datafitimag.append((i, self.vyrezimag[i]))

        guessimag = (-1.0, 1.0)

        fit_params, fit_error = leastSquaresFit(funcimag, guessimag, datafitimag)

        #print fit_params, fit_error
        xim = []
        for i in range(len(self.vyref)):
            xim.append(self.vyref[i])
            self.yim_fit.append(funcimag(fit_params, i))

        Yimag = self.yim_fit[indexmax]

        # Determine R1
        self.R1 = 1.0 / maxY

        # Determine C0
        w0 = 2 * math.pi * self.fResonance
        self.C0 = Yimag / w0

        # Find X1
        w = []
        for f in self.frequency[lava:prava]:
            w.append(2 * math.pi * f)

        Yinn = []           # z ofitovanych hodnot real a imag urobi komplexne cislo...
        for i in range(len(self.vyref)):
            Yinn.append(self.yre_fit[i] + 1j * self.yim_fit[i])

        X1 = []
        for i in range(len(self.frequency[lava:prava])):
            X1.append((1 / (Yinn[i] - 1j * w[i] * self.C0)).imag)

        # Determine C1
        C1 = []
        f1 = []
        for i in range(len(X1)):
            try:
                C1.append((((w[i] / w0) ** 2) - 1) / (w[i] * X1[i]))
                f1.append(self.frequency[lava + i])
            except ZeroDivisionError:
                pass

        #self.C1 = Numeric.average(C1)    ISVOSA - OLD
        self.C1 = numpy.average(C1)

        #Determine L1
        self.L1 = 1.0 / (self.C1 * (w0 ** 2))
Example #11
0
D = 2
b = 1
p = (C, a, D, b)
dx = 0.5
dt = 1.0
for i in range(7):
    dx /= 2
    dt /= 2
    e = error_model(p, (dx, dt))  # use the model to generate data
    e += random_number.gauss(0, 0.01 * e)  # perturb data randomly
    data.append(((dx, dt), e))

from Scientific.Functions.LeastSquares import leastSquaresFit

parameter_guess = (1, 2, 2, 1)  # exact guess...
r = leastSquaresFit(error_model, parameter_guess, data)
print r
# try different initial guesses:
for i in range(6):
    # exact p +/- [1-6]*(randomly chosen -1 or 1):
    pg = array(p, float) + i * 0.05 * sign(random_number.uniform(-1, 1)) * ones(4)
    r = leastSquaresFit(error_model, pg, data)
    print "guess:", str(pg)
    deviation = array(p) - array(r[0])
    print "     deviation:", deviation

print "\n\nTesting statistical computations:"
import Scientific.Statistics as S

nsamples = 100000
data = random.normal(1.0, 0.5, nsamples)
Example #12
0
def test(temp, frict):
    output = file('Langevin.dat', 'w')
    
    # Make a small perturbation of the momenta
    atoms.set_momenta(1e-6 * np.random.random([len(atoms), 3]))
    print 'Initializing ...'
    predyn = VelocityVerlet(atoms, 0.5)
    predyn.run(2500)

    dyn = Langevin(atoms, timestep, temp, frict)
    print ''
    print ('Testing Langevin dynamics with T = %f eV and lambda = %f' %
           (temp, frict))
    ekin = atoms.get_kinetic_energy()/len(atoms)
    print ekin
    output.write('%.8f\n' % ekin)

    print 'Equilibrating ...'

    # Initial guesses for least-squares fit
    a = 0.04
    b = 2*frict
    c = temp
    params = (a,b,c)
    fitdata = [(0, 2.0 / 3.0 * ekin)]

    tstart = time.time()
    for i in xrange(1,nequil+1):
        dyn.run(nminor)
        ekin = atoms.get_kinetic_energy() / len(atoms)
        fitdata.append((i*nminor*timestep, 2.0/3.0 * ekin))
        if usescipy and i % nequilprint == 0:
            (params, chisq) = leastSquaresFit(targetfunc, params, fitdata)
            print '%.6f  T_inf = %.6f (goal: %f), tau = %.2f,  k = %.6f' % \
                  (ekin, params[2], temp, 1.0/params[1], params[0])
        output.write('%.8f\n' % ekin)
    tequil = time.time() - tstart
    print 'This took %s minutes.' % (tequil / 60)
    output.write('&\n')
    assert abs(temp-params[2]) < 0.25*temp, 'Least-squares fit is way off'
    assert nequil*nminor*timestep > 3.0/params[1], 'Equiliberation was too short'
    fitdata = np.array(fitdata)

    print 'Recording statistical data - this takes ten times longer!'
    temperatures = []
    tstart = time.time()
    for i in xrange(1,nsteps+1):
        dyn.run(nminor)
        ekin = atoms.get_kinetic_energy() / len(atoms)
        temperatures.append(2.0/3.0 * ekin)
        if i % nprint == 0:
            tnow = time.time() - tstart
            tleft = (nsteps-i) * tnow / i
            print '%.6f    (time left: %.1f minutes)' % (ekin, tleft/60)
        output.write('%.8f\n' % ekin)
    output.write('&\n')
    output.close()

    temperatures = np.array(temperatures)
    mean = sum(temperatures) / len(temperatures)
    print 'Mean temperature:', mean, 'eV'
    print
    print 'This test is statistical, and may in rare cases fail due to a'
    print 'statistical fluctuation.'
    print
    assert abs(mean - temp) <= reltol*temp, 'Deviation is too large.'
    print 'Mean temperature:', mean, ' in ', temp, ' +/- ', reltol*temp

    return fitdata, params, temperatures