Example #1
0
def getInfo(data, target, theta):
    afterSigmoid = sigmoid(data.dot(theta))

    firstList = [ ]
    for k in afterSigmoid:
        m = []
        for x in k:
            m.append(bigfloat.log(x,bigfloat.precision(precision)))
        firstList.append(m)

    secondList = [ ]
    for k in afterSigmoid:
        m = []
        for x in k:
            m.append(bigfloat.log( bigfloat.sub(1. , x) , bigfloat.precision(precision)))
        secondList.append(m)
    
    Ein = 0.
    m = []
    for x,y in zip(firstList, secondList):
        for a,b,t in zip(x,y,target):
            value = bigfloat.add( bigfloat.mul(t[0],a, bigfloat.precision(precision)) , bigfloat.mul( bigfloat.sub(1. ,t[0], bigfloat.precision(precision)) ,a, bigfloat.precision(precision)))
            m.append(value)
    for item in m:
        Ein = bigfloat.add(item, Ein, bigfloat.precision(precision))
    
    Ein = - Ein
    print(Ein)
    gradient = -data.T.dot(target-afterSigmoid)
    return (Ein, gradient)
def BICrss(n, k, rss):
	"""Calculate the Bayesian Information Criterion value, using:
	
	- n:   number of observations
	- k:   number of parameters
	- rss: residual sum of squares
	"""

	return n + n * log(2 * pi) + n * log(rss / n) + (log(n)) * (k + 1)
def AICrss(n, k, rss):
	"""Calculate the Akaike Information Criterion value, using:

	- n:   number of observations
	- k:   number of parameters
	- rss: residual sum of squares
	"""

	return n * log((2 * pi) / n) + n + 2 + n * log(rss) + 2 * k
def HQCrss(n, k, rss):
	"""Calculate the Hannan-Quinn information Criterion value, using:
	
	- n:   number of observations
	- k:   number of parameters
	- rss: residual sum of squares
	"""

	return n * log(rss / n) + 2 * k * log(log(n))
Example #5
0
    def degdist_bigfloat(self, maxdeg, n, mindeg=0):
        """Returns the degree distribution from 0 to maxdeg degree.

        It should be use with the (iterated) link probability measure.

        Parameters:
          maxdeg: the maximal degree for we calculate the degree distribution
          n: the size of the network (the number of vertices)

        Returns:
            rho: the degree distribution as a list with length of maxdeg+1.
                The index k gives the probability of having degree k.
        """

        assert isinstance(n, int) and isinstance(maxdeg, int) and n > maxdeg
        import bigfloat
        context = bigfloat.Context(precision=10)
        divs = self.divs
        n_intervals = len(divs)
        lengths = [divs[i] - divs[i - 1] for i in xrange(1, n_intervals)]
        lengths.insert(0, divs[0])
        log_lengths = numpy.log(lengths)
        # Eq. 5, where ...
        avgdeg = [
            bigfloat.BigFloat(n * sum(
                [self.probs[i][j] * lengths[j] for j in xrange(n_intervals)]),
                              context=context) for i in xrange(n_intervals)
        ]
        #log_factorial = [ 0.5*bigfloat.log(2*math.pi, context=context) + (d+.5)*bigfloat.log(d, context=context) - d
        #                 for d in xrange(1,maxdeg+1) ]
        log_factorial = [
            bigfloat.log(bigfloat.factorial(k), context=context)
            for k in xrange(1, maxdeg + 1)
        ]
        log_factorial.insert(0, 0)

        rho = [bigfloat.BigFloat(0, context=context)] * (maxdeg + 1)
        # Eq. 4
        for i in xrange(n_intervals):
            # Eq. 5
            log_rho_i = [
                (bigfloat.mul(k, bigfloat.log(avgdeg[i]), context=context) -
                 log_factorial[k] - avgdeg[i])
                for k in xrange(mindeg, maxdeg + 1)
            ]
            log_rho_i_length = [
                log_rho_i[k] + log_lengths[i]
                for k in xrange(mindeg, maxdeg + 1)
            ]
            for k in xrange(mindeg, maxdeg + 1):
                rho[k] += bigfloat.exp(log_rho_i_length[k], context=context)
        return rho
Example #6
0
 def extrainfo(self):
     try:
         expected = self.calc_ExpectedToBuy()
         expectedWithBuy = self.calc_ExpectedToBuy(self.lastbuy)
         print "\n==Information=="
         print "To complete the album alone is expected " \
               "you to buy {:.2f} stickers ({:.2f} packs)" \
               ", paying {}{:.2f} for it.".format(expected, expected / self.spp,
                                                self.highersign,
                                                (expected *
                                                 self.valueperunit)
                                                / self.valuetohigher)
         print "You will need to pay " \
               "{}{:.2f} for {:.2f} stickers if you plan to buy the last " \
               "{}{} directly " \
               "from the company.".format(self.highersign,
                                          (expectedWithBuy *
                                           self.valueperunit)
                                          / self.valuetohigher,
                                          expectedWithBuy,
                                          self.lastbuy if self.lastbuy > 1 else "\b",
                                          " stickers" if self.lastbuy > 1 else " sticker")
         print "N=n*(log(n)+Euler constant) = " \
               "{}".format(self.n * bigfloat.log(self.n) +
                           bigfloat.const_euler())
         print "---------------"
     except ZeroDivisionError:
         print("Cannot devide by zero.")
def JoLe_eq(temp, B0, E, E_D, T_pk):
    """Johnson - Lewin model, used for estimating trait values at 
    a given temperature."""
    global K

    function = B0 * exp(1) ** (-E / (K * temp)) / (1 + exp(1) ** (1 / K
                  * (E_D / T_pk + K * log(E / (E_D - E)) - (E_D / temp))))
    return numpy.array(map(log, function), dtype=numpy.float64)
Example #8
0
 def visualize(self):
     print("-" * 80)
     intervals = {'value': [], 'length': [], 'probability': []}
     self.print_intervals(intervals)
     v, l, p = intervals['value'], intervals['length'], intervals[
         'probability']
     h = [bf.log(p[i] / l[i]) for i in range(len(v))]
     plt.bar(v, h, width=l, align='edge')
     plt.show()
     print("-" * 80 + "\n")
Example #9
0
    def degdist_bigfloat(self, maxdeg, n, mindeg=0):
        """Returns the degree distribution from 0 to maxdeg degree.

        It should be use with the (iterated) link probability measure.

        Parameters:
          maxdeg: the maximal degree for we calculate the degree distribution
          n: the size of the network (the number of vertices)

        Returns:
            rho: the degree distribution as a list with length of maxdeg+1.
                The index k gives the probability of having degree k.
        """

        assert isinstance(n, int) and isinstance(maxdeg, int) and n > maxdeg
        import bigfloat
        context = bigfloat.Context(precision=10)
        divs = self.divs
        n_intervals = len(divs)
        lengths = [divs[i] - divs[i-1] for i in xrange(1, n_intervals)]
        lengths.insert(0, divs[0])
        log_lengths = numpy.log(lengths)
        # Eq. 5, where ...
        avgdeg = [bigfloat.BigFloat(n*sum([self.probs[i][j]*lengths[j]
                  for j in xrange(n_intervals)]), context=context) for i in xrange(n_intervals)]
        #log_factorial = [ 0.5*bigfloat.log(2*math.pi, context=context) + (d+.5)*bigfloat.log(d, context=context) - d
        #                 for d in xrange(1,maxdeg+1) ]
        log_factorial = [bigfloat.log(bigfloat.factorial(k), context=context)
                         for k in xrange(1, maxdeg+1)]
        log_factorial.insert(0, 0)

        rho = [bigfloat.BigFloat(0, context=context)] * (maxdeg+1)
        # Eq. 4
        for i in xrange(n_intervals):
            # Eq. 5
            log_rho_i = [(bigfloat.mul(k, bigfloat.log(avgdeg[i]), context=context) - log_factorial[k] - avgdeg[i])
                         for k in xrange(mindeg, maxdeg+1)]
            log_rho_i_length = [log_rho_i[k] + log_lengths[i]
                         for k in xrange(mindeg, maxdeg+1)]
            for k in xrange(mindeg, maxdeg+1):
                rho[k] += bigfloat.exp(log_rho_i_length[k], context=context)
        return rho
Example #10
0
    def GenLn2(self):
        with precision(128):
            ln2 = bigfloat.log(2.0)
            l = bin64(ln2).hex1int() & 0xffffffffe0000000
            ld = bin64(hex_to_double(l))
            print(".ln_lead = ", double_to_hex(ld), ld.hex())

        with precision(128):
            t = ln2 - ld
            tail = bin64(t)
            print(".ln_tail = ", double_to_hex(tail), tail.hex())
Example #11
0
    def GenTable(self):
        #newsize = self.table_size +  1
        newsize = self.table_size << 1
        for j in range(0, newsize):  # 0 to 256
            with precision(256):
                f = bigfloat.log(1 + j / newsize)
                f_leadint = bin64(f).hex1int() & 0xfffffffff0000000
                f_tail = f - hex_to_double(f_leadint)

            #print("%x"%f_leadint, double_to_hex(float(f_tail)))
            print("{", "0x%x, %s" % (f_leadint, double_to_hex(f_tail)), "},")
Example #12
0
    def GenLn2(self):
        with precision(128):
            ln2 = bigfloat.log(2.0)
            h = -ln2 / self.table_size
            hint = bin64(h).hex1int() & 0xffffffffffff0000
            hd = bin64(hex_to_double(hint))
            print(".ln2by_tblsz_head = ", hd.hex())

        with precision(128):
            t = h - hd
            tail = bin64(t)
            print(".ln2by_tblsz_tail = ", tail.hex())
def compute_entropy(probabilities):
    # Compute Shannon Entropy of the given distribution
    # https://en.wikipedia.org/wiki/Entropy_(information_theory)
    # SE = - ∑ P_i * log_b( P_i )
    # where
    #  P_i is the probability of sample i
    #  log_b is log-base-b
    # It is not clear in the Pereyra paper what units were used, so we
    # try the two most common bases: 2 and e
    entropy = 0.0
    for probability in probabilities:
        # base e: entropy measured in nats
        entropy -= probability * bigfloat.log(probability)
    return entropy
Example #14
0
def compute_entropy(probabilities):
    # Compute Shannon Entropy of the given distribution
    # https://en.wikipedia.org/wiki/Entropy_(information_theory)
    # SE = - ∑ P_i * log_b( P_i )
    # where
    #  P_i is the probability of sample i
    #  log_b is log-base-b
    # It is not clear in the Pereyra paper what units were used, so we
    # try the two most common bases: 2 and e
    entropy = 0.0
    for probability in probabilities:
        # base e: entropy measured in nats
        entropy -= probability * bigfloat.log(probability)
    return entropy
def JoLe(params, temp, data):
    """Johnson - Lewin model, used by the optimizer."""

    global K

    # Get the parameters.
    E = params['E'].value
    E_D = params['E_D'].value
    T_pk = params['T_pk'].value
    B0 = params['B0'].value

    # Prevent E being higher than E_D, by punishing the optimizer with huge
    # numbers when that is the case.
    if E >= E_D:
        return 1e10

    # Otherwise, fit the model!
    function = B0 * exp(1) ** (-E / (K * temp)) / (1 + exp(1) ** (1 / K
                  * (E_D / T_pk + K * log(E / (E_D - E)) - (E_D / temp))))

    return numpy.array(map(log, function) - data, dtype=numpy.float64)
Example #16
0
    def get_free_energies(self):
        """
        Give the free energy by

        .. math::

           F_{\\text{all configs}}(T, V) = - k_B T \ln Z_{\\text{all configs}}(T, V).

        :return: The free energy on the temperature-volume grid.
        """
        try:
            import bigfloat
        except ImportError:
            raise ImportError(
                "Install ``bigfloat`` package to use {0} object!".format(
                    self.__class__.__name__))

        with bigfloat.precision(self.precision):
            log_z = np.array([bigfloat.log(d) for d in self.total],
                             dtype=float)
        return -K * self.temperature * log_z
def get_E_and_E_D(dataset):
    """Estimate E and E_D using linear regression."""

    global K

    temps = []
    trait_vals = []

    # Convert temps to 1/K*(temps + 273.15).
    for row in dataset:
        temps.append(1 / (K * (float(row[4]) + 273.15)))
        trait_vals.append(log(float(row[5])))

    # Perform a regression of temps vs trait_vals.
    (slope, intercept, r_value, p_value, std_err) = linregress(temps,
                                                               trait_vals)

    # Return arbitrary values if the regression was not successful.
    if numpy.isnan(slope):
        return 10, 30

    # Otherwise, return the slope as E and slope * 10 as E_D.
    else:
        return abs(slope), abs(slope) * 10
Example #18
0
    def get_free_energies(self):
        """
        The free energy calculated from the partition function :math:`Z_{\\text{all configs}}(T, V)` by

        .. math::

           F_{\\text{all configs}}(T, V) = - k_B T \ln Z_{\\text{all configs}}(T, V).

        :return: The free energy on a temperature-volume grid.
        """
        try:
            import bigfloat
        except ImportError:
            raise ImportError(
                "Install ``bigfloat`` package to use {0} object!".format(
                    self.__class__.__name__))

        with bigfloat.precision(self.precision):
            log_z = np.array([
                bigfloat.log(d)
                for d in self.partition_functions_for_each_configuration
            ],
                             dtype=float)
        return -K * self.temperature * log_z
Example #19
0
def absErrorAE(predictedTime, survivalTime):
    return abs(bf.log(predictedTime) - bf.log(survivalTime))
Example #20
0
def fit_sharpe_schoolfield(dataset, B0_start, E_start, T_pk_start, E_D_start):
    """Fit the Sharpe-Schoolfield model."""

    global K

    # Store temperatures and log-transformed trait values.
    temps = []
    trait_vals = []

    for row in dataset:

        # Convert temperatures to Kelvin.
        temps.append(float(row[4]) + 273.15)
        trait_vals.append(log(float(row[5])))

    # Convert temps and trait_vals to numpy arrays.
    temps = numpy.array(temps, dtype=numpy.float64)
    trait_vals = numpy.array(trait_vals, dtype=numpy.float64)

    # Prepare the parameters and their bounds.
    params = Parameters()
    params.add('B0', value=B0_start)
    params.add('E', value=E_start, min=0.00001, max=10)
    params.add('E_D', value=E_D_start, min=0.00001, max=30)
    params.add('T_pk', value=T_pk_start, min=273.15 - 10, max=273.15 + 150)

    try:

        # Try and fit!
        result = minimize(sharpe_schoolf,
                          params,
                          args=(temps, trait_vals),
                          xtol=1e-12,
                          ftol=1e-12,
                          maxfev=100000)

    except Exception:

        # If fitting failed, return.
        return None

    # Check if we have a covariance matrix (just in case we don't for any reason).
    if result.covar is None:
        return None

    # Since we're still here, fitting was successful!

    # In the highly unlikely scenario that E == E_D, add a tiny number to
    # E_D to avoid division by zero.
    if params['E'].value == params['E_D'].value:
        params['E_D'].value += 0.000000000000001

    # Calculate B(T_ref), B_pk, and the operational niche width.
    B_T_ref = exp(1)**sharpe_schoolf_eq(numpy.array([273.15]),
                                        params['B0'].value, params['E'].value,
                                        params['E_D'].value,
                                        params['T_pk'].value)

    B_pk = exp(1)**sharpe_schoolf_eq(numpy.array([params['T_pk'].value]),
                                     params['B0'].value, params['E'].value,
                                     params['E_D'].value, params['T_pk'].value)

    operational_niche_width = calculate_operational_niche_width(params,
                                                                B_pk[0],
                                                                T_ref=273.15)

    robjects.r("rm(list=ls())")
    robjects.r.assign("K", K)
    robjects.r.assign("covar_mat", result.covar)
    robjects.r.assign(
        "Schoolf_coeffs",
        robjects.FloatVector((params['B0'].value, params['E'].value,
                              params['E_D'].value, params['T_pk'].value)))

    B_T_ref_stderr = robjects.r(
        "deltamethod( ~ x1 / (1 + (x2/(x3 - x2)) * exp(x3 / K * (1 / x4 - 1 / 273.15))), Schoolf_coeffs, covar_mat)^2"
    )[0]
    B_T_ref_1_4_stderr = robjects.r(
        "deltamethod( ~ (x1 / (1 + (x2/(x3 - x2)) * exp(x3 / K * (1 / x4 - 1 / 273.15))))^(1/4), Schoolf_coeffs, covar_mat)^2"
    )[0]
    log_E_stderr = robjects.r(
        "deltamethod(~ log(x2), Schoolf_coeffs, covar_mat)^2")[0]
    T_pk_squared_stderr = robjects.r(
        "deltamethod(~ x4^2, Schoolf_coeffs, covar_mat)^2")[0]
    log_E_D_stderr = robjects.r(
        "deltamethod(~ log(x3), Schoolf_coeffs, covar_mat)^2")[0]
    B_pk_stderr = robjects.r(
        "deltamethod( ~ (x1 * exp((-x2) * (1/(K * x4) - 1/(K * 273.15)))) / (1 + (x2/(x3 - x2))), Schoolf_coeffs, covar_mat)^2"
    )[0]
    log_B_pk_stderr = robjects.r(
        "deltamethod( ~ log((x1 * exp((-x2) * (1/(K * x4) - 1/(K * 273.15)))) / (1 + (x2/(x3 - x2)))), Schoolf_coeffs, covar_mat)^2"
    )[0]

    [operational_niche_width_stderr, log_operational_niche_width_stderr
     ] = bootstrap_operational_niche_width(dataset)

    # Calculate the fitted trait values.
    pred = exp(1)**sharpe_schoolf_eq(temps, params['B0'].value,
                                     params['E'].value, params['E_D'].value,
                                     params['T_pk'].value)
    pred = numpy.array(pred, dtype=numpy.float64)

    # Collect measured trait values without log transformation.
    trait_vals_no_log = []
    for row in dataset:
        trait_vals_no_log.append(float(row[5]))

    # Calculate the residual sum of squares.
    residuals = trait_vals_no_log - pred
    rss = sum(residuals**2)

    # Temperatures ...
    temperatures = ""
    for element in temps:
        temperatures += str(element - 273.15) + ','
    temperatures = temperatures[:-1]

    # Trait Values ...
    trait_values = ""
    for element in trait_vals_no_log:
        trait_values += str(element) + ','
    trait_values = trait_values[:-1]

    # If, for whatever reason, the residual sum of squares is
    # 'not a number' or infinite, then return.
    if numpy.isnan(rss) or numpy.isinf(rss):
        return None

    # Calculate the total sum of squares.
    tss = sum((trait_vals_no_log - numpy.mean(trait_vals_no_log))**2)

    # Calculate the R-squared value.
    if tss == 0:
        fit_goodness = 1
    else:
        fit_goodness = 1 - (rss / tss)

    # Number of data points before and after T_pk ...
    count_temps = []
    points_before_pk = 0
    points_after_pk = 0

    for row in dataset:
        if float(row[4]) in count_temps:
            continue
        else:
            count_temps.append(float(row[4]))

            if float(row[4]) < (params['T_pk'].value - 273.15):
                points_before_pk += 1
            elif float(row[4]) > (params['T_pk'].value - 273.15):
                points_after_pk += 1

    result_line = [
        dataset[0][1], dataset[0][7], dataset[0][8], temperatures,
        trait_values, dataset[0][2],
        str(B_T_ref[0]**(1 / 4.0)),
        str(B_T_ref_1_4_stderr),
        str(log(params['E'].value)),
        str(log_E_stderr),
        str(log(operational_niche_width)),
        str(log_operational_niche_width_stderr),
        str((params['T_pk'].value - 273.15)**2),
        str(T_pk_squared_stderr),
        str(log(B_pk[0])),
        str(log_B_pk_stderr),
        str(log(params['E_D'].value)),
        str(log_E_D_stderr),
        str(fit_goodness),
        str(points_before_pk),
        str(points_after_pk)
    ]

    return result_line
Example #21
0
def parallel_ONW_for_bootstrap(arg):
    global temporary_dataset

    numpy.random.seed(arg)

    resampled_indices = numpy.floor(
        numpy.random.rand(len(temporary_dataset)) *
        len(temporary_dataset)).astype(int)
    resampled_dataset = numpy.array(temporary_dataset)[
        resampled_indices, :].tolist()

    (B0_start, E_start, T_pk_start,
     E_D_start) = generate_starting_values(resampled_dataset)

    temps = []
    trait_vals = []

    for row in resampled_dataset:

        # Convert temperatures to Kelvin.
        temps.append(float(row[4]) + 273.15)
        trait_vals.append(log(float(row[5])))

    # Convert temps and trait_vals to numpy arrays.
    temps = numpy.array(temps, dtype=numpy.float64)
    trait_vals = numpy.array(trait_vals, dtype=numpy.float64)

    # Prepare the parameters and their bounds.
    params = Parameters()
    params.add('B0', value=B0_start)
    params.add('E', value=E_start, min=0.00001, max=10)
    params.add('E_D', value=E_D_start, min=0.00001, max=30)
    params.add('T_pk', value=T_pk_start, min=273.15 - 10, max=273.15 + 150)

    try:

        # Try and fit!
        result = minimize(sharpe_schoolf,
                          params,
                          args=(temps, trait_vals),
                          xtol=1e-12,
                          ftol=1e-12,
                          maxfev=100000)

    except Exception:

        # If fitting failed, return None.
        result = None

    if result is not None:

        # In the highly unlikely scenario that E == E_D, add a tiny number to
        # E_D to avoid division by zero.
        if params['E'].value == params['E_D'].value:
            params['E_D'].value += 0.000000000000001

        B_pk = exp(1)**sharpe_schoolf_eq(numpy.array([params['T_pk'].value]),
                                         params['B0'].value, params['E'].value,
                                         params['E_D'].value,
                                         params['T_pk'].value)
        current_operational_niche_width = calculate_operational_niche_width(
            params, B_pk[0], T_ref=273.15)
        return (current_operational_niche_width)
    else:
        return (None)
Example #22
0
def absErrorAE(predictedTime, survivalTime):
    return abs(bf.log(predictedTime) - bf.log(survivalTime))
def schoolfield_model(dataset, B0_start, E_start, T_pk_start, E_D_start):
    """Prepare the Schoolfield model."""

    global K

    # Store temperatures and log-transformed trait values.
    temps = []
    trait_vals = []

    for row in dataset:

        # Convert temperatures to Kelvin.
        temps.append(float(row[4]) + 273.15)
        trait_vals.append(log(float(row[5])))

    # Convert temps and trait_vals to numpy arrays.
    temps = numpy.array(temps, dtype=numpy.float64)
    trait_vals = numpy.array(trait_vals, dtype=numpy.float64)

    # Prepare the parameters and their bounds.
    params = Parameters()
    params.add('B0', value=B0_start)
    params.add('E', value=E_start, min=0.00001, max=30)
    params.add('E_D', value=E_D_start, min=0.00001, max=50)
    params.add('T_pk', value=T_pk_start, min=273.15, max=273.15 + 150)

    try:

        # Set the random seed.
        numpy.random.seed(1337)

        # Try and fit!
        minimize(schoolf, params, args=(temps, trait_vals))
    except Exception:

        # If fitting failed, return.
        return None, None, None, None

	# Since we're still here, fitting was successful!
    points_before_pk = 0
    points_after_pk = 0

	# Collect measured trait values without log transformation.
    trait_vals_no_log = []
    for row in dataset:
        trait_vals_no_log.append(float(row[5]))
        
        # Get the number of data points before and after T_pk.
        if float(row[4]) < (params['T_pk'].value - 273.15):
            points_before_pk += 1
        elif float(row[4]) > (params['T_pk'].value - 273.15):
            points_after_pk += 1

    # Calculate the estimated trait values.
    pred = exp(
        1) ** schoolf_eq(temps,
                         params['B0'].value,
                         params['E'].value,
                         params['E_D'].value,
                         params['T_pk'].value)
    pred = numpy.array(pred, dtype=numpy.float64)

    # Calculate the residual sum of squares.
    residuals = trait_vals_no_log - pred
    rss = sum(residuals ** 2)

    # If, for whatever reason, the residual sum of squares is
    # 'not a number' or infinite, then return.
    if numpy.isnan(rss) or numpy.isinf(rss):
        return None, None, None, None

    # Calculate the total sum of squares.
    tss = sum((trait_vals_no_log - numpy.mean(trait_vals_no_log)) ** 2)

    # If the total sum of squares is 0, then we have a perfect fit!
    if tss == 0:
        fit_goodness = 1

    # Otherwise, calculate the R-Squared value.
    else:
        fit_goodness = 1 - (rss / tss)

    # Get the values of the three Information Criteria for this fit.
    AIC_schoolfield = AICrss(len(temps), 4, rss)
    BIC_schoolfield = BICrss(len(temps), 4, rss)
    HQC_schoolfield = HQCrss(len(temps), 4, rss)

    # Create a list of Species, Standardised Species, Reference, Trait,
    # Latitude, Longitude ...
    result_line = [
      dataset[0][0], dataset[0][1], dataset[0][2], dataset[0][3],
      dataset[0][7], dataset[0][8]
    ]

    # Temperatures ...
    temperatures = ""
    for element in temps:
        temperatures += str(element - 273.15) + ','
    temperatures = temperatures[:-1]
    result_line.append(temperatures)

    # Trait Values ...
    trait_values = ""
    for element in trait_vals_no_log:
        trait_values += str(element) + ','
    trait_values = trait_values[:-1]
    result_line.append(trait_values)

    # B0 and B0_stderr ...
    result_line.append(str(params['B0'].value))
    result_line.append(str(params['B0'].stderr))
    
    # E and E_stderr ...
    result_line.append(str(params['E'].value))
    result_line.append(str(params['E'].stderr))

    # T_pk and T_pk_stderr ...
    result_line.append(str(params['T_pk'].value - 273.15))
    result_line.append(str(params['T_pk'].stderr))

    # E_D and E_D_stderr ...
    result_line.append(str(params['E_D'].value))
    result_line.append(str(params['E_D'].stderr))

    # No theta and theta_stderr for this model!
    result_line.append("NA")
    result_line.append("NA")
    
    # R-squared ...
    result_line.append(str(fit_goodness))
    
    # Formula ...
    result_line.append(
       "log(Trait_value) = log(B0 * exp(-E * ((1/(" + str(K) \
        + " * temp)) - (1/(" + str(K) \
        + " * 273.15))))/(1 + (E/(E_D - E)) * exp(E_D/" \
        + str(K) + " * (1/T_pk - 1/temp))))")
    
    # Model name ...
    result_line.append("Schoolfield")
    
    # Implementation ...
    result_line.append("lmfit.minimize (Python package)")
    
    # Number of data points before and after T_pk ...
    result_line.append(str(points_before_pk))
    result_line.append(str(points_after_pk))

    return AIC_schoolfield, BIC_schoolfield, HQC_schoolfield, result_line
Example #24
0
def interval_log(interval, context_down, context_up):
    lower, upper = interval
    out_interval = [bf.log(lower, context_down), bf.log(upper, context_up)]
    derivs = [1 / lower, 0], [0, 1 / upper]
    return out_interval, derivs
Example #25
0
def fit_sharpe_schoolfield(dataset, B0_start, E_start, T_pk_start, E_D_start):
    """Fit the Sharpe-Schoolfield model."""

    global K

    # Store temperatures and log-transformed trait values.
    temps = []
    trait_vals = []

    for row in dataset:

        # Convert temperatures to Kelvin.
        temps.append(float(row[4]) + 273.15)
        trait_vals.append(log(float(row[5])))

    # Convert temps and trait_vals to numpy arrays.
    temps = numpy.array(temps, dtype=numpy.float64)
    trait_vals = numpy.array(trait_vals, dtype=numpy.float64)

    # Prepare the parameters and their bounds.
    params = Parameters()
    params.add('B0', value=B0_start)
    params.add('E', value=E_start, min=0.00001, max=30)
    params.add('E_D', value=E_D_start, min=0.00001, max=50)
    params.add('T_pk', value=T_pk_start, min=273.15 - 10, max=273.15 + 150)

    try:

        # Try and fit!
        minimize(sharpe_schoolf,
                 params,
                 args=(temps, trait_vals),
                 xtol=1e-12,
                 ftol=1e-12,
                 maxfev=100000)

    except Exception:

        # If fitting failed, return.
        return None

    # Since we're still here, fitting was successful!

    # In the highly unlikely scenario that E == E_D, add a tiny number to
    # E_D to avoid division by zero.
    if params['E'].value == params['E_D'].value:
        params['E_D'].value += 0.000000000000001

    # Calculate the fitted trait values.
    pred = exp(1)**sharpe_schoolf_eq(temps, params['B0'].value,
                                     params['E'].value, params['E_D'].value,
                                     params['T_pk'].value)
    pred = numpy.array(pred, dtype=numpy.float64)

    # Collect measured trait values without log transformation.
    trait_vals_no_log = []
    for row in dataset:
        trait_vals_no_log.append(float(row[5]))

    # Calculate the residual sum of squares.
    residuals = trait_vals_no_log - pred
    rss = sum(residuals**2)

    # If, for whatever reason, the residual sum of squares is
    # 'not a number' or infinite, then return.
    if numpy.isnan(rss) or numpy.isinf(rss):
        return None

    # Calculate the total sum of squares.
    tss = sum((trait_vals_no_log - numpy.mean(trait_vals_no_log))**2)

    # Calculate the R-squared value.
    if tss == 0:
        fit_goodness = 1
    else:
        fit_goodness = 1 - (rss / tss)

    result_line = [
        dataset[0][0], dataset[0][2], dataset[0][3],
        str(params['B0'].value),
        str(params['E'].value),
        str(params['T_pk'].value - 273.15),
        str(params['E_D'].value),
        str(fit_goodness)
    ]

    return result_line
Example #26
0
    def calculate_log_probability(self,
                                  bag,
                                  camera_network,
                                  max_steps,
                                  verbose=False):
        prior_prob = 1.0
        log_motion_model_prob = np.log(prior_prob)
        prob_movements = []
        n_skipped = 0
        timesteps = 0
        # quite important we discount ones without enough steps
        # because each step adds like 10^-3 to the probability...
        if bag.get_message_count() < max_steps:
            raise Exception("Bag does not have enough messages")

        for i, msg in enumerate(bag):
            if i >= max_steps:
                break
            timesteps += 1
            msg = msg.message
            true_odom = msg.true_odom
            ekf_odom = msg.ekf_odom
            true_pos = point_to_numpy(true_odom.pose.pose.position)

            ekf_pos = point_to_numpy(ekf_odom.pose.pose.position)
            true_rpy = quat_to_rpy(
                quat_to_numpy(true_odom.pose.pose.orientation))
            true_heading = true_rpy[2]
            ekf_rpy = quat_to_rpy(quat_to_numpy(
                ekf_odom.pose.pose.orientation))
            ekf_heading = ekf_rpy[2]
            ekf_vel = point_to_numpy(ekf_odom.twist.twist.linear)
            ekf_time = ekf_odom.header.stamp.to_sec()
            ekf_positional_cov = np.array(ekf_odom.pose.covariance).reshape(
                6, 6)[:2, :2]

            last_ekf_pos = np.array(list(msg.last_ekf_state[:2]) + [0.0])
            last_ekf_heading = msg.last_ekf_state[2]
            last_true_pos = point_to_numpy(msg.last_true_pos)
            last_true_heading = msg.last_true_heading

            camera_update = msg.camera_update
            if msg.has_camera_update:

                # no longer need to correct since sampling normal => add to true pos has replaced direct raytracing :'(
                #             corrected_camera_position = apply_time_correction(camera_update, ekf_vel, ekf_time)
                corrected_camera_position = point_to_numpy(
                    camera_update.position)
                prob_sensor_measurements = self.sensor_model(
                    camera_network, camera_update.source_camera_id,
                    corrected_camera_position, ekf_pos)

                eta = self.get_normalizer(camera_network,
                                          corrected_camera_position[:2],
                                          camera_update.source_camera_id,
                                          ekf_pos[:2],
                                          ekf_positional_cov,
                                          n_samples=1000)

            else:
                prob_sensor_measurements = 1  # ie. there were none
                eta = 1

            if np.array_equal(last_true_pos, true_pos):
                continue

            log_prob_movement = self.log_motion_model(last_ekf_pos,
                                                      ekf_pos,
                                                      last_ekf_heading,
                                                      ekf_heading,
                                                      last_true_pos,
                                                      true_pos,
                                                      last_true_heading,
                                                      true_heading,
                                                      scaling=1,
                                                      verbose=verbose)

            value = log_prob_movement + bf.log(prob_sensor_measurements / eta)
            #         value = log_prob_movement

            if value == 0.0:
                print "*********** 0.0!! ********"
                n_skipped += 1
                continue
            log_motion_model_prob += value
            if verbose:
                print("Log prob xt -> xt+1: {0}".format(log_prob_movement))
                print("prob sensor measurement: {0}".format(
                    prob_sensor_measurements))
                print("eta: {0}".format(eta))
                print("Current log prob: {0}".format(log_motion_model_prob))

    #         prob_movements.append(prob_movement)
    #         if motion_model_prob > 1000:
    #             print(motion_model_prob)

        return log_motion_model_prob, timesteps, n_skipped