如何在Julia中进行任意精度计算?

如何在Julia中进行任意精度计算?,julia,probability,Julia,Probability,我来自Python背景,使用Python的Decimal模块进行任意精度计算的一个重要方面是知道何时需要指定Decimal类型的数字,例如,概率计算 在Julia计算中,必须多久为数字指定一次任意精度类型 我通过例子学习得最好,我想解决的例子如下 在不更换26 ABC至Z区块26的桶时,计算至少一次成功的概率!,326!, 还有526!时代 鉴于: 一次试验成功=按正确顺序随机抽出试块:A、B、C至Z 随机试验的数量为n=26!或3n或5n 一次随机试验的成功概率,p=1/n 一次随机试验的失败

我来自Python背景,使用Python的Decimal模块进行任意精度计算的一个重要方面是知道何时需要指定Decimal类型的数字,例如,概率计算

在Julia计算中,必须多久为数字指定一次任意精度类型

我通过例子学习得最好,我想解决的例子如下

在不更换26 ABC至Z区块26的桶时,计算至少一次成功的概率!,326!, 还有526!时代

鉴于:

一次试验成功=按正确顺序随机抽出试块:A、B、C至Z

随机试验的数量为n=26!或3n或5n

一次随机试验的成功概率,p=1/n

一次随机试验的失败概率为f=1–p

尽可能少地使用型号规格进行计算:

使用CL=1–f^n的n次试验中至少一次成功的置信水平CL

使用CL=1-f^3n进行3n试验的CL


使用CL=1-f^5n进行的5n试验的CL非常适用于大数类型规范的计算。 例如,在下面的代码中,我只将一个数字指定为big26,其他所有数字都是自动的

我刚刚开始使用Julia,但多年来一直以各种方式进行任意精度计算。朱莉娅为我提供了我有过的这类事情中最愉快的经历

# Set precision to 150 bits which should be adequate precision.
setprecision(150)

# Note that we only have to specify a "big" number here.
n = factorial(big"26")
println("n = factorial(big\"26\") = ", n)
println("Note that we never have to use \"big\" again in the following code.")
println("typeof(n) = ", typeof(n), "\n")  

# p is the probability of success on 1 trial.
p = 1/n
println("p = 1/n = ", p)
# Note we did not have to specify the type of p.
println("typeof(p) = ", typeof(p), "\n")

# f is the probability of failure on 1 trial.
f = 1 - p
println("f = 1 - p = ", f)
println("typeof(f) = ", typeof(f), "\n")   

# CL is the probability of at least 1 success in n trials.
# CL stands for confidence level.   
CL = 1 - f^n
println("The 63% CL for n trials = 1 - f^n = ", CL)
println("typeof(CL) = ", typeof(CL), "\n")   

# Here is the 95% conf. level using 3n random trials.
CL95 = 1 - f^(3n)
println("The 95% CL for 3n trials = ", CL95)
println("typeof(CL95) = ", typeof(CL95), "\n")

# Here is the 99% conf. level using 5n random trials.
CL99 = 1 - f^(5n)
println("The 99% CL for 5n trials = ", CL99)
println("typeof(CL99) = ", typeof(CL99), "\n")

""" ============================= Output ==============================
n = factorial(big"26") = 403291461126605635584000000
Note that we never have to use "big" again in the following code.
typeof(n) = BigInt

p = 1/n = 2.4795962632247974600749435458479566174226555415e-27
typeof(p) = BigFloat

f = 1 - p = 9.9999999999999999999999999752040373677520254001e-01
typeof(f) = BigFloat

The 63% CL for n trials = 1 - f^n = 6.3212055882855767839219205578358958187929158048e-01
typeof(CL) = BigFloat

The 95% CL for 3n trials = 9.5021293163213605701567013782477488392169554992e-01
typeof(CL95) = BigFloat

The 99% CL for 5n trials = 9.9326205300091453290223898909666750856240017783e-01
typeof(CL99) = BigFloat
"""

如果您想要像Python中的十进制那样的精度十进制浮点,我自己已经编写了一个。可以将十进制精度设置为任何正整数值。如果您需要模块的副本,请与我联系

julia> factorial(BigInt(26))
403291461126605635584000000

julia> println("How many decimal digits do we need?")
How many decimal digits do we need?

julia> length(string(  factorial(BigInt(26))  ))
27

julia> using PDFPs

julia> PDFP_setDefaultPrecision(40)
40

julia> n = PDFP(    factorial(BigInt(26))    )
PDFP(0, 26, [4, 0, 3, 2, 9, 1, 4, 6, 1, 1, 2, 6, 6, 0, 5, 6, 3, 5, 5, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

julia> disp(a::PDFP) = println(PDFP_toShortCommonString(a))
disp (generic function with 1 method)

julia> p = 1/n
PDFP(0, -27, [2, 4, 7, 9, 5, 9, 6, 2, 6, 3, 2, 2, 4, 7, 9, 7, 4, 6, 0, 0, 7, 4, 9, 4, 3, 5, 4, 5, 8, 4, 7, 9, 5, 6, 6, 1, 7, 4, 2, 2])

julia> disp(p)
2.47960E-27

julia> println("Let's call failure q as per convention in probability questions")
Let's call failure q as per normal in probability questions

julia> q = 1 - p
PDFP(0, -1, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 5, 2, 0, 4, 0, 3, 7, 3, 6, 7, 7, 5, 2])

julia> disp(q)
1.00000

julia> PDFP_toFortranString(q)
"9.999999999999999999999999975204037367752E-1"

julia> at_least_one_success_in_n_trials = 1 - q^n
PDFP(0, -1, [6, 3, 2, 1, 2, 0, 5, 5, 8, 8, 2, 8, 5, 5, 8, 0, 5, 5, 6, 0, 2, 0, 5, 7, 9, 5, 3, 4, 5, 6, 9, 0, 2, 1, 9, 8, 7, 6, 4, 9])

julia> disp(at_least_one_success_in_n_trials)
0.632121

julia> at_least_one_success_in_3n_trials = 1 - q^(3*n)
PDFP(0, -1, [9, 5, 0, 2, 1, 2, 9, 3, 1, 6, 3, 2, 1, 3, 6, 2, 1, 0, 1, 6, 5, 3, 1, 1, 5, 3, 8, 4, 6, 6, 4, 3, 9, 0, 4, 8, 2, 9, 1, 0])

julia> disp(at_least_one_success_in_3n_trials)
0.950213

julia> at_least_one_success_in_5n_trials = 1 - q^(5*n)
PDFP(0, -1, [9, 9, 3, 2, 6, 2, 0, 5, 3, 0, 0, 0, 9, 1, 4, 5, 6, 7, 4, 4, 6, 4, 3, 7, 4, 3, 4, 3, 4, 4, 7, 4, 8, 6, 8, 9, 6, 2, 8, 1])

julia> disp(at_least_one_success_in_5n_trials)
0.993262

将精度设置为300位以获得足够的精度。但是你只设置了150位的精度??设定精度150这是个错误吗???好的,史蒂文!谢谢你指出这一点。我的注释与代码不匹配,我需要检查其含义。再次感谢您仔细阅读此代码。我讨厌犯错误,但那是一个。然而,我认为这不会改变任何结果。当我做了一些测试后,我会做出回应。@StevenSiew我根据您的好捕获编辑了注释,并通过运行脚本版本检查了代码。一切正常。再次感谢!如果您看到任何其他问题或有疑问,请让我知道。我想知道这个问题是否是关于SO的主题。没有“我希望其他人向我解释”。此外,没有实际问题需要解决。我想知道这样的演讲是否更适合在博客或类似的地方进行。@rickhg12hs您的观点被很好地采纳了。根据你的评论,我已经重做了问题和答案。感谢您的反馈。@rickhg12hs在我的编辑之后,您是否仍然觉得问题和答案令人反感?作为Julia的新手,根据我使用Python Decimal模块的经验,我真的没有想到任意精度的计算会如此容易地工作。似乎其他人也没有意识到朱莉娅的类型推断在应用于此类计算时效果如何。我从来没有写过博客。关于Julia相关编程博客的位置/方式的任何建议。我希望用户正在阅读Julia的文档。有一节是关于。也许作为一个目标,你可以贡献。谢谢你的建议。我去看看。谢谢你慷慨的提议,史蒂文!我应该如何与您联系?发送电子邮件至StevenSiew2@gmail.comAn电子邮件应该在路上了。为什么不把它做成一个包放到github上呢?因为:1。我还没有完成所有的测试用例。2.我不知道如何做一个包裹。3.我一生中从未使用过Github 4。未优化中的代码