是否可以使用基于整数的概率进行泊松分布



在坚固性和以太坊EVM和小数中不存在。有什么方法可以在数学上仍然可以使用整数创建泊松分布?它不必是完美的,即舍入或失去一些数字可能是可以接受的。

让我序言,说明下面的内容不会(直接)以乙醚/固体对您有所帮助。但是,它会产生您可能可以用于工作的概率表。

我最终对您在表达泊松概率作为理性的概率方面的准确性有多大吸引人,因此我在Ruby中将以下脚本放在一起以尝试以下内容:

def rational_poisson(lmbda)
  Hash.new.tap do |h|   # create a hash and pass it to this block as 'h'.
    # Make all components of the calculations rational to allow
    # cancellations to occur wherever possible when dividing
    e_to_minus_lambda = Math.exp(-lmbda).to_r
    factorial = 1r
    lmbda = lmbda.to_r
    power = 1r
    (0...).each do |x|
      unless x == 0
        power *= lmbda
        factorial *= x
      end
      value = (e_to_minus_lambda / factorial) * power
      # the following double inversion/conversion bounds the result
      # by the significant bits in the mantissa of a float
      approx = Rational(1, (1 / value).to_f)
      h[x] = approx
      break if x > lmbda && approx.numerator <= 1
    end
  end
end
if __FILE__ == $PROGRAM_NAME
  lmbda = (ARGV.shift || 2.0).to_f  # read in a lambda (defaults to 2.0)
  pmf = rational_poisson(lmbda)     # create the pmf for a Poisson with that lambda
  pmf.each { |key, value| puts "p(#{key}) = #{value} = #{value.to_f}" }
  puts "cumulative error = #{1.0 - pmf.values.inject(&:+)}"  # does it sum to 1?
end

当您浏览代码时要知道的事情。将.to_r附加到值或表达式上,将其转换为有理的,即两个整数的比率;r后缀的值是理性常数;(0...).each是一个开放式迭代器,将循环直到满足break条件。

小脚本产生的结果,例如:

localhost:pjs$ ruby poisson_rational.rb 1.0
p(0) = 2251799813685248/6121026514868073 = 0.36787944117144233
p(1) = 2251799813685248/6121026514868073 = 0.36787944117144233
p(2) = 1125899906842624/6121026514868073 = 0.18393972058572117
p(3) = 281474976710656/4590769886151055 = 0.061313240195240384
p(4) = 70368744177664/4590769886151055 = 0.015328310048810096
p(5) = 17592186044416/5738462357688819 = 0.003065662009762019
p(6) = 1099511627776/2151923384133307 = 0.0005109436682936699
p(7) = 274877906944/3765865922233287 = 7.299195261338141e-05
p(8) = 34359738368/3765865922233287 = 9.123994076672677e-06
p(9) = 67108864/66196861914257 = 1.0137771196302974e-06
p(10) = 33554432/330984309571285 = 1.0137771196302975e-07
p(11) = 33554432/3640827405284135 = 9.216155633002704e-09
p(12) = 4194304/5461241107926203 = 7.68012969416892e-10
p(13) = 524288/8874516800380079 = 5.907792072437631e-11
p(14) = 32768/7765202200332569 = 4.2198514803125934e-12
p(15) = 256/909984632851473 = 2.8132343202083955e-13
p(16) = 16/909984632851473 = 1.7582714501302472e-14
p(17) = 1/966858672404690 = 1.0342773236060278e-15
cumulative error = 0.0

最新更新