Python Logging

I want to calculate something like:

formula

Where f(i)is a function that returns a real number in [-1,1]for any iin {1,2,...,5000}.

Obviously, the result of the sum is somewhere in [-1,1], but when I cannot calculate it in Python using direct coding, as it becomes and becomes , as a result of which the calculated amount turns into .0.550000comb(5000,2000)infNaN

A necessary solution is to use a magazine on both sides.

That is, using the identifier , if I could calculate and , I could calculate the sum, even if large and almost .a × b = 2log(a) + log(b)log(a)log(b)ab0

So, I think I'm asking if there is an easy way to calculate

log2(scipy.misc.comb(5000,2000))

,

sum([2**(log2comb(5000,i)-5000) * f(i) for i in range(1,5000) ])

@abarnert, 5000 , . , , , 5000 1e7, .

, , :

log2(comb(5000,2000)) = sum([log2 (x) for x in 1:5000])-sum([log2 (x) for x in 1:2000])-sum([log2 (x) for x in 1:3000])

?

+1
3

formula

- f n = 5000 p = 0.5.

scipy.stats.binom.expect:

import scipy.stats as stats

def f(i):
    return i
n, p = 5000, 0.5
print(stats.binom.expect(f, (n, p), lb=0, ub=n))
# 2499.99999997

, n , p np np*(1-p). n :

import math
print(stats.norm.expect(f, loc=n*p, scale=math.sqrt((n*p*(1-p))), lb=0, ub=n))
# 2500.0
+8

comb float64, inf.

exact=True, Python int, ( , ).

, np.log2 int, Python math.log2.

:

math.log2(scipy.misc.comb(5000, 2000, exact=True))

, n k, n!k / k!, ? ∏(i=1...k)((n+1-i)/i), .

, , , * (n-i) / (k-i).

, , . , Python 4000 , C 4000 , , , , . :

In [1327]: n, k = 5000, 2000
In [1328]: %timeit math.log2(scipy.misc.comb(5000, 2000, exact=True))
100 loops, best of 3: 1.6 ms per loop
In [1329]: %timeit np.log2(np.arange(n-k+1, n+1)).sum() - np.log2(np.arange(1, k+1)).sum()
10000 loops, best of 3: 91.1 µs per loop

, , ... , , , . 2000 8- 608- . 100000, 20000, 20000 8- 9K . 1000000, 200000, 200000 8- 720K.

, . , listcomp genxpr, 5000, 100000 1000000 Python-float-24MB , 720K ? , , , , :

r = sum(math.log2(n-i) - math.log2(k-i) for i in range(n-k))

, scipy, , ( Python). ( Python 2, ... xrange range .)


, NumPy ( , ) ( , )?

+3

EDIT: @unutbu , , log2comb(n, k) .


comb(n, k) n!/((n-k)! k!) n! Gamma gamma(n+1). Scipy scipy.special.gamma. Scipy gammaln, ( , ) .

So, log(comb(n, k))we can calculate howgammaln(n+1) - gammaln(n-k+1) - gammaln(k+1)

For example, log (comb (100, 8)) (after execution from scipy.special import gammaln):

In [26]: log(comb(100, 8))
Out[26]: 25.949484949043022

In [27]: gammaln(101) - gammaln(93) - gammaln(9)
Out[27]: 25.949484949042962

and log (comb (5000, 2000)):

In [28]: log(comb(5000, 2000))  # Overflow!
Out[28]: inf

In [29]: gammaln(5001) - gammaln(3001) - gammaln(2001)
Out[29]: 3360.5943053174142

(Of course, to get the base-2 logarithm, just divide by log(2).)

For convenience, you can define:

from math import log
from scipy.special import gammaln

def log2comb(n, k):
    return (gammaln(n+1) - gammaln(n-k+1) - gammaln(k+1)) / log(2) 
+3
source

Source: https://habr.com/ru/post/1611228/


All Articles