示例#1
0
 def find_interval(self, y):
     order = int(y, 2)  # convert to integer
     n = self.msg_len
     if n != len(y):
         print("ERROR! Y has invalid length:{}".format(y))
         exit()
     # prob of intervals to be scaled up
     unit_prob = 1 / 2**n
     lb = bf.BigFloat(order) / 2**n
     ub = bf.BigFloat(order + 1) / 2**n
     return order, lb, ub
示例#2
0
def interval_pow10(interval, context_down, context_up):
    lower, upper = interval
    ten_down, ten_up = bf.BigFloat("10", context_down), bf.BigFloat(
        "10", context_up)
    out_lower, out_upper = [
        bf.pow(ten_down, lower, context_down),
        bf.pow(ten_up, upper, context_up)
    ]
    out_interval = [out_lower, out_upper]
    derivs = [out_lower, 0], [0, out_upper]
    return out_interval, derivs
示例#3
0
    def check_ending(self):
        v = self.peak
        # boundaries of decoded real number
        bin_seq, order = self.real_to_bin(v, len(self.seq))
        # bounaries of pmf
        l = len(bin_seq)
        prob_lower_bound = bf.BigFloat(order) / 2**l
        prob_upper_bound = bf.BigFloat(order + 1) / 2**l

        p1 = self.tree.PMF(prob_lower_bound)
        p2 = self.tree.PMF(prob_upper_bound)
        return True if p2 - p1 > 1 - self.errP else False
示例#4
0
文件: mfng.py 项目: horvatha/mfng
    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
示例#5
0
文件: ASTtypes.py 项目: sljiaa/Satire
    def get_noise(obj):
        if float(obj.token.value).is_integer and float(
                obj.token.value) == obj.token.value:
            return 0

        if bf.sub(bf.BigFloat(obj.token.value, bf.precision(113)),
                  bf.BigFloat(obj.token.value, bf.precision(50)),
                  bf.precision(1024)) == 0:
            return 0
        v = math.log(abs(obj.token.value), 2)
        if (v - math.floor(v) != 0.0):
            if (obj.token.value <= 0):
                return -pow(2, math.floor(v))
            else:
                return pow(2, math.floor(v))
        return 0.0  #obj.token.value
示例#6
0
def run_benchmark(log_error_bound: int,
                  benchmark: Benchmark,
                  configuration_increment: Optional[Callable] = None,
                  use_ad: bool = False):
    exact_program, variables = benchmark.benchmark(), benchmark.variables
    init_prec = 50

    context = bf.RoundTowardZero + bf.precision(100000)
    assert log_error_bound < 0, "Log error bound should be negative!"
    error_bound = bf.BigFloat(f'0.{"0"*(-log_error_bound-1)}1', context)
    [var.sample() for var in variables]
    if use_ad:
        params = [
            exact_program, error_bound,
            [init_prec] * exact_program.subtree_size()
        ]
        time, (num_refinements,
               precision_configuration) = time_wrap(evaluate_using_derivatives,
                                                    params)
    else:
        params = [
            exact_program, error_bound, configuration_increment, True,
            init_prec
        ]
        time, (num_refinements,
               precision_configuration) = time_wrap(evaluate, params)
    data = {
        "time": time.total_seconds(),
        "num_refinements": num_refinements,
        "log_error_bound": float(log_error_bound),
        "last_prec_config": precision_configuration,
    }
    return data
示例#7
0
 def FLOAT(self, t):
     #t.value = float(t.value)
     #t.value = np.float128(t.value)
     t.value = bf.BigFloat(t.value, bf.precision(128))
     #t.value = t.value
     #print("value -> ", t.value)
     return t
示例#8
0
def sum_likelihood_result(population):
    """
    Sums all the likelihood results values on a population.

    Args:
        population : LIST[Chromosome(), Chromosome(), ...]
            A list filled with 'Chromosome' objects

    Returns:
        INT
            The sum of all likelihood results on a population
    """
    with bf.quadruple_precision:
        total = bf.BigFloat("0.0")
        for i in range(0, len(population)):
            log_likelihood_result = bf.exp(bf.BigFloat(str(population[i].get_log_likelihood_result())))
            total = bf.add(total, log_likelihood_result)
    return total
示例#9
0
    def get_reserves_graphql(self,
                             token_a: str, token_b: str,
                             block_number: int) -> List[int]:
        import requests
        import bigfloat # type: ignore
        (token0, token1) = UniswapV2Utils.sort_tokens(token_a, token_b)
        pairs = self.get_pair(token_a, token_b)
        query = """
{
  pair (block: {number: %d},
      id: "%s"
  ) {
    id
    reserve0
    reserve1
    token0 {
      id
      decimals
    }
    token1 {
      id
      decimals
    }
    createdAtTimestamp
  }
}
""" % (block_number, pairs.lower())
        request = requests.post(
            'https://api.thegraph.com/subgraphs/name/{}'.format(self.subgraph_endpoint),
            json={'query': query}
        )
        if request.status_code != 200:
            raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
        j = request.json()['data']['pair']
        reserve0 = int(bigfloat.round((bigfloat.BigFloat(j['reserve0']) * \
                    bigfloat.pow(10, int(j['token0']['decimals'])))))
        reserve1 = int(bigfloat.round((bigfloat.BigFloat(j['reserve1']) * \
                    bigfloat.pow(10, int(j['token1']['decimals'])))))
        return [reserve0, reserve1, int(j['createdAtTimestamp'])] \
            if j['token0']['id'] == token_a.lower() \
               else [reserve1, reserve0, int(j['createdAtTimestamp'])]
示例#10
0
def relative_likelihood_result_calculator(population):
    """
    Given a population, this function calculates the relative likelihood result (it is a way to make
    the likelihood result bigger) to every chromosome.

    Args:
        population : LIST[Chromosome(), Chromosome(), ...]
            A list filled with 'Chromosome' objects
    """
    with bf.quadruple_precision:
        total = sum_likelihood_result(population)
        for i in range(0, len(population)):
            log_likelihood_result = bf.exp(bf.BigFloat(str(population[i].get_log_likelihood_result())))
            population[i].set_relative_likelihood_result(float(bf.div(log_likelihood_result, total)))
示例#11
0
def get_score_for_metric(metric, metrics_summary):
    mean_metrics = metrics_summary['means']
    if metric == 'mutual information':
        mi_metrics = metrics_summary['mean mutual inf']
        mean_mi = mi_metrics['mutual inf'] # really conditional entropy
        return bf.BigFloat(mean_mi) # make sure we actually use bigfloat

    elif metric == 'total trace':
        return mean_metrics['mean total trace']
    elif metric == 'final trace':
        return mean_metrics['mean final trace']
    elif metric == 'final differential entropy':
        return mean_metrics['mean final differential entropy']
    elif metric == 'total differential entropy':
        return mean_metrics['mean total differential entropy']

    else:
        raise Exception("Unknown metric: {0}".format(metric))
示例#12
0
 def input(self, c: str, flags: object):
     # NEWIT запятой заведует объект числа
     # NEWIT если новый ввод, создаем новый объект числа
     if flags.IS_NEW_INPUT:
         self._value = bf.BigFloat()
     if c == '.':
         # Ошибка при вводе второй точки
         if self._value.comma:
             raise ValueError(
                 "could not convert string to float: '{0}{1}'".format(
                     self._value, c))
         else:
             self._value.comma = c
     elif self._value.comma:
         self._value.fraction += c
     # в том числе отсекает ввод незанчащих 0 целой части
     # TODO ??? заменить self.__integer на len == 0
     elif self._value.integer or c != '0':
         self._value.integer += c
示例#13
0
    for c in reversed(num_coeffs):
        num = num * z + c
    den = 0.0
    for c in reversed(den_coeffs):
        den = den * z + c
    return num/den


def gamma(z, g, N):
    return (z+g-0.5)**(z-0.5) / exp(z+g-0.5) * L(g, z, N)


# Now express everything as a rational function;  we want to be
# able to figure out the coefficient of p_j z^k in the numerator.
# Let's call this coefficient b(j, k).  It depends on N.
# b(j, N)[k] is the coefficient of p_j z^k in the numerator.
# except: b(0, N)[k] is the coefficient of p_0/2 z^k in the numerator.

p = 300  # bits of precision to use for computation
g = bigfloat.BigFloat(6.024680040776729583740234375)
N = 13

with bigfloat.precision(p):
    print("Numerator coefficients:")
    for c in Lg_num_coeffs(g, N):
        print(c)

    print("Denominator coefficients:")
    for c in Lg_den_coeffs(g, N):
        print(c)
示例#14
0
    def transmit(self, seq, max_channel_use=None, err_num=None, msg_len=4):
        # PMS settings
        self.msg_point = self.bin_to_real(seq)
        print("Message: {}, Px: {}".format(self.msg_point, self.XoverP))

        # hamming code settings
        self.msg_len = msg_len  # hamming code message length
        self.redundant_bits = HammingCode.calc_redundant_bits(msg_len)
        self.block_len = msg_len + self.redundant_bits
        print("Hamming Code ({}, {})".format(self.block_len, msg_len))
        self.undecodable = False  #TODO
        # h_err_p = self.XoverP
        # h_err_p = hamming_err_prob(self.XoverP, self.msg_len, self.block_len)
        h_err_p = hamming_err_prob(self.XoverP / 2, self.msg_len,
                                   self.block_len)
        # estimated by leading order
        # h_err_p = hamming_LOEP(self.XoverP, self.block_len)
        # h_err_p = 0.1179648

        max_default_use = 500
        MCU = max_channel_use if max_channel_use is not None else max_default_use
        for i in range(MCU):
            # np.random.seed(i) # debug mode
            # split probability tree, figure out which block msg belongs to
            msg_pmf = self.tree.PMF(self.msg_point)
            msg_seq, msg_order = self.real_to_bin(msg_pmf, msg_len)
            self.X = msg_seq
            # print("X: {}".format(self.X))

            # hamming encoding:
            h = HammingCode(self.X)
            U = h.encode()  # msg to be send thru channel
            # print("U: {}".format(U))

            # Binary Symmetric Channel transmission:
            v = self.channel_transmit(U, err_num)
            # print("V: {}".format(v))

            # hamming decoding:
            err_pos = h.detectError(v)  # reverse order
            if err_pos == 0:  # no error
                self.Y = self.X
            elif err_pos <= len(v):  # able to recover u from v
                err_pos = len(v) - err_pos
                lost_bit = '1' if v[
                    err_pos] == '0' else '0'  # flip the error bit
                correct_v = v[:err_pos] + lost_bit + v[err_pos + 1:]
                self.Y = h.decode(correct_v[::-1])
            else:  #TODO
                # print("Hamming code can't correct error in {} with error position".format(v, err_pos))
                self.undecodable = True
                # self.Y = h.decode(v)
                continue

            # print("Y: {}".format(self.Y))
            '''
                Update probability: scale up the prob block msg belongs to, and
            scale down the other prob block. Divide tree into three parts by l-
            ower/upper bounds of Y's interval: the left part, the middle part, 
            and the right part. Assume the crossover probability is a, so we s-
            hould scale up P([lb, ub]) by a, and scale down P([0,lb], [ub,1]) 
            by 1 - a. The procedures consist of two steps:
                1. Scale down the left part and scale up the other parts.
                2. Scale up the middle part and scale down the right part.
                
                Note that if either the left part or the right part is empty, 
            the situation is the same as standard posterior matching scheme.
            '''
            # probability lower/upper bounds of Y's interval
            Y_order, Y_pmf_lb, Y_pmf_ub = self.find_interval(self.Y)
            if Y_order == 0:  # left part is empty
                Y_node_ub = self.tree.quantile(Y_pmf_ub)
                self.peak = Y_node_ub.start_value
                self.tree = Y_node_ub.parent.rotate()
                self.tree.left.p, self.tree.right.p = 1 - h_err_p, h_err_p
            elif Y_order == 2**self.msg_len - 1:  # right part is empty
                Y_node_lb = self.tree.quantile(Y_pmf_lb)
                self.peak = Y_node_lb.start_value
                self.tree = Y_node_lb.parent.rotate()
                self.tree.left.p, self.tree.right.p = h_err_p, 1 - h_err_p
            else:
                # nodes of lower/upper bounds of Y's interval
                Y_node_lb = self.tree.quantile(Y_pmf_lb)
                Y_node_ub = self.tree.quantile(Y_pmf_ub)
                # number of intervals in the left\right part
                left_num = Y_order
                right_num = 2**self.msg_len - 1 - Y_order

                unit_prob = bf.div(h_err_p, (2**self.msg_len - 1))
                self.peak = (Y_node_lb.start_value + Y_node_ub.start_value) / 2

                # step 1
                self.tree = Y_node_lb.parent.rotate()
                self.tree.left.p = unit_prob * left_num
                self.tree.right.p = 1 - self.tree.left.p

                # step 2
                sub = self.tree.right
                sub.parent = None
                self.tree.right = None
                sub = Y_node_ub.parent.rotate()
                sub_total = 1 - h_err_p + unit_prob * right_num
                sub.left.p = bf.BigFloat(1 - h_err_p) / sub_total
                sub.right.p = 1 - sub.left.p
                self.tree.right = sub
                sub.parent = self.tree
            # print("-"*80)

            # self.tree.visualize()

            if self.check_ending():
                bin_seq, _ = self.real_to_bin(self.peak, len(self.seq))
                return bin_seq, i + 1, self.block_len

        bin_seq, _ = self.real_to_bin(self.peak, len(self.seq))
        print("You have reached the maximum expected channel use!")
        return bin_seq, MCU, self.block_len
示例#15
0
        means = metrics['means']
        # multiply partial means and variances by number of bags
        num_bags = means['num_bags']
        for key in agg_means:
            if key == 'num_bags':
                agg_means[key] += num_bags
            elif not key.startswith('stddev'):
                agg_means[key] += num_bags * means[key] # stddevs will be * 0 so ignore

        if args.compute_MI:
            # also expand mutual information same way
            mi = metrics['mean mutual inf']
            mi_num_bags = mi['num_bags']
            # add how many bags were comptued over
            agg_MI['num_bags'] += mi_num_bags 
            agg_MI['mutual inf'] += mi_num_bags * bf.BigFloat(mi['mutual inf'])
            agg_MI['variance'] += mi_num_bags * bf.BigFloat(mi['variance']) # since variance is just mean of squared diffs
        
    # re-average to compute overall means
    total_num_bags_means = agg_means['num_bags']
    for key in agg_means:
        if key == 'num_bags':
            continue
        agg_means[key] = agg_means[key]/total_num_bags_means
        if key.startswith('var'):
            # also insert a stddev key, should already exist or python complains
            stddev_key = 'stddev' + key[3:]
            agg_means[stddev_key] = agg_means[key]**0.5

    if args.compute_MI:
        # re-average MI
示例#16
0
 def clear(self):
     # NEWIT просто создаем новый пустой объект
     self._value = bf.BigFloat()
示例#17
0
 def __init__(self, name: str):
     self.__name = name  # буква регистра
     # TODO Можно закрыть изменение извне, если реализовать свойства в класса регистра Z ???
     self._value = bf.BigFloat(
     )  # значение регистра как объект числа BigFloat
示例#18
0
def norm(ts):
    vs = ts.values
    vs = [ 0.0-float( bigfloat.log10(bigfloat.BigFloat(v)) ) for v in vs]
    ts = pd.Series(vs,index=ts.index)
    return ts
示例#19
0
            codedfile.write(sigm)
            """
         bytearray=np.ubyte(np.append(bytearray,sigma%(2**8))) #LSB of sigma
         bytearray=np.ubyte(np.append(bytearray, sigma*(2**(-8)))) #MSB of sigma
         """

            #Convert prec in BigFloat to number of bytes in int:
            numbytes = int(np.ceil(prec / 8.0))
            print("number of bytes in subband ", k, "and channel", chan, "=",
                  numbytes, "for", numblocks, " samples")
            print("Hence bytes per sample: ", numbytes * 1.0 / numblocks)
            #Convert BigFloat number which encodes many samples into a byte array:
            for bytenum in range(numbytes):
                #print("bytenum=", bytenum)
                bytearray = np.append(
                    bytearray, np.ubyte(
                        encblock * (bigfloat.BigFloat(2)**
                                    8)))  #left shift by 8 bits, keep int part
                encblock = encblock * (bigfloat.BigFloat(2)**
                                       8) % 1  #keep fractional part

    ba = struct.pack('B' * len(bytearray), *bytearray)  #'B' for unsigned byte
    codedfile.write(ba)
    totalbytes += len(bytearray)

numsamples = np.prod(x.shape)
print("Total number of bytes=", totalbytes)
print("Total number of samples:", numsamples)
print("bytes per sample=", totalbytes * 1.0 / numsamples)
print("Hence bis per sample=", 8 * 1.0 * totalbytes / numsamples)
示例#20
0
 def __init__(self, start_value, length, prob):
     self.start_value = bf.BigFloat(start_value)
     self.length, self.p = bf.BigFloat(length), bf.BigFloat(prob)
     self.parent, self.left, self.right = None, None, None