-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_param_tests.py
58 lines (42 loc) · 1.48 KB
/
random_param_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from src.bounding_pairs_mp import bounding_pairs_mp
from src.fixed_mp import fixed_mp
from mpmath import mp, mpf, log, exp, loggamma
from utils.utils import logdiffexp
from random import random
from time import time
# function of each term in log scale
# function of each term in log scale
def f(theta: tuple, k: int):
"""
terms of the normalization contant series
theta : (log_lambda, nu)
k : k-th term
"""
return (mpf(k)) * theta[0] - theta[1] * loggamma(mpf(k)+1)
if __name__ == "__main__":
mp.dps = 400
M = 10**7
eps = mpf(2)**mpf(-52)
initial_k = 0
L = mpf(0)
mu_l = 0
mu_u = 1000
nu_l = 0
nu_u = 100
F = 0
N = 10000
for i in range(1, N+1):
t0 = time()
mu = mu_u * random() + mu_l
nu = nu_u * random() + nu_l
lamb = mu**nu
loglamb = log(lamb)
theta = (loglamb, nu)
k, bp_value = bounding_pairs_mp(f, theta, M, L, eps, initial_k)
fixed_value = fixed_mp(f, theta, M=k*10, initial_k=initial_k)[1]
if (i % 50 == 0) and exp(logdiffexp(bp_value, fixed_value)) < eps:
print(f"Pass test {i}/{N}. (k = {k} | time = {(time()-t0):.2f}s)")
if exp(logdiffexp(bp_value, fixed_value)) > eps:
print(f"Fail test {i}: theta = {theta} | bp_value = {bp_value} | fixed_value = {fixed_value} | error = {float(exp(logdiffexp(bp_value, fixed_value)))}.")
F+=1
print(f"Tests faileds {F/N*100:.2f}% ({F}/{N}).")