-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECNoise.py
95 lines (84 loc) · 2.89 KB
/
ECNoise.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from math import sqrt, fabs
import numpy as np
def higham(t, L):
f = t
for k in range(1, L + 1):
f = sqrt(f)
for k in range(1, L + 1):
f = f**2
f = f**2
return f
#
# Determines the noise of a function from the function values
#
# [fnoise,level,inform] = ECnoise(nf,fval)
#
# The user must provide the function value at nf equally-spaced points.
# For example, if nf = 7, the user could provide
#
# f(x-3h), f(x-2h), f(x-h), f(x), f(x+h), f(x+2h), f(x+3h)
#
# in the array fval. Although nf >= 4 is allowed, the use of at least
# nf = 7 function evaluations is recommended.
#
# Noise will not be detected by this code if the function values differ
# in the first digit.
#
# If noise is not detected, the user should increase or decrease the
# spacing h according to the ouput value of inform. In most cases,
# the subroutine detects noise with the initial value of h.
#
# On exit:
# fnoise is set to an estimate of the function noise;
# fnoise is set to zero if noise is not detected.
#
# level is set to estimates for the noise. The k-th entry is an
# estimate from the k-th difference.
#
# inform is set as follows:
# inform = 1 Noise has been detected.
# inform = 2 Noise has not been detected; h is too small.
# Try 100*h for the next value of h.
# inform = 3 Noise has not been detected; h is too large.
# Try h/100 for the next value of h.
#
# Argonne National Laboratory
# Jorge More' and Stefan Wild. November 2009.
def ECNoise(nf, fval):
level = np.zeros((nf-1))
dsgn = np.zeros((nf-1))
fnoise = 0.0
gamma = 1.0 # = gamma(0)
# Compute the range of function values.
fmin = np.amin(fval)
fmax = np.amax(fval)
if (fmax-fmin)/max(fabs(fmax), fabs(fmin)) > .1:
inform = 3
return fnoise, level, inform
# Construct the difference table.
for j in range(nf-1):
for i in range(nf-(j+1)):
fval[i] = fval[i+1] - fval[i]
# h is too small only when half the function values are equal.
if (j==0 and sum([fval[k] == 0 for k in range(nf - 1)]) >= nf/2):
inform = 2
return fnoise, level, inform
gamma = 0.5*((j+1.)/(2.*(j+1.)-1.))*gamma
# Compute the estimates for the noise level.
level[j] = sqrt(gamma*np.mean(np.square(fval[0:nf-(j+1)])))
# Determine differences in sign.
emin = np.amin(fval[0:nf-(j+1)])
emax = np.amax(fval[0:nf-(j+1)])
if (emin*emax < 0.0):
dsgn[j] = 1
# Determine the noise level.
for k in range(nf-3):
emin = np.amin(level[k:k+3])
emax = np.amax(level[k:k+3])
if (emax<=4*emin and dsgn[k]):
fnoise = level[k]
inform = 1
return fnoise, level, inform
# If noise not detected then h is too large.
inform = 3
return fnoise, level, inform