forked from Argonne-National-Laboratory/Frhodo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
172,839 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Development/Random Testing/loss func integral/loss_integral.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This file is part of Frhodo. Copyright © 2020, UChicago Argonne, LLC | ||
# and licensed under BSD-3-Clause. See License.txt in the top-level | ||
# directory for license and copyright information. | ||
|
||
import numpy as np | ||
from scipy import integrate # used to integrate weights numerically | ||
|
||
def generalized_loss_fcn(x, mu=0, a=2, c=1): # defaults to L2 loss | ||
x_c_2 = ((x-mu)/c)**2 | ||
|
||
if a == 1: # generalized function reproduces | ||
loss = (x_c_2 + 1)**(0.5) - 1 | ||
if a == 2: | ||
loss = 0.5*x_c_2 | ||
elif a == 0: | ||
loss = np.log(0.5*x_c_2+1) | ||
elif a == -2: # generalized function reproduces | ||
loss = 2*x_c_2/(x_c_2 + 4) | ||
elif a <= -1000: # supposed to be negative infinity | ||
loss = 1 - np.exp(-0.5*x_c_2) | ||
else: | ||
loss = np.abs(a-2)/a*((x_c_2/np.abs(a-2) + 1)**(a/2) - 1) | ||
|
||
return loss*c**a + mu # multiplying by c^2 is not necessary, but makes order appropriate | ||
|
||
alpha_vec = np.geomspace(-60, -1, 1000) | ||
alpha_vec = np.concatenate([alpha_vec, np.linspace(-1,2, int(3/(alpha_vec[-1] - alpha_vec[-2])-1))[1:]]) | ||
tau = 100 | ||
for alpha in alpha_vec: | ||
for c in np.geomspace(1, 5, 100): | ||
int_func = lambda x: np.exp(-generalized_loss_fcn(x, a=alpha, c=c)) | ||
val, err = integrate.quadrature(int_func, -tau, tau, tol=1E-14, maxiter=10000) | ||
print(f'{alpha:0.3f} {c:0.3f} {val:0.14e}') | ||
|
Oops, something went wrong.