|
6 | 6 |
|
7 | 7 |
|
8 | 8 | def huber(x, M): |
9 | | - """Huber loss function, for outlier-robust applications, `see here <https://www.cvxpy.org/api_reference/cvxpy.atoms.elementwise.html#huber>`_""" |
| 9 | + """Huber loss function, for outlier-robust applications, |
| 10 | + `see here <https://www.cvxpy.org/api_reference/cvxpy.atoms.elementwise.html#huber>`_ |
| 11 | +
|
| 12 | + :param np.array[float] x: data points on which to evaluate the Huber function pointwise |
| 13 | + :param float M: where the loss turns from quadratic to linear |
| 14 | + :return: (np.array[float]) -- pointwise evaluations of the Huber function |
| 15 | + """ |
10 | 16 | absx = np.abs(x) |
11 | 17 | return np.where(absx <= M, 0.5*x**2, M*(absx - 0.5*M)) |
12 | 18 |
|
13 | 19 | def huber_const(M): |
14 | 20 | """Scale that makes :code:`sum(huber())` interpolate :math:`\\sqrt{2}\\|\\cdot\\|_1` and :math:`\\frac{1}{2}\\|\\cdot\\|_2^2`, |
15 | | - from https://jmlr.org/papers/volume14/aravkin13a/aravkin13a.pdf, with correction for missing sqrt""" |
| 21 | + from https://jmlr.org/papers/volume14/aravkin13a/aravkin13a.pdf, with correction for missing sqrt |
| 22 | +
|
| 23 | + :param float M: Huber parameter, where the function turns from quadratic to linear |
| 24 | + :return: (float) -- appropriate scale factor to normalize the Huber function |
| 25 | + """ |
16 | 26 | a = 2*np.exp(-M**2 / 2)/M |
17 | 27 | b = np.sqrt(2*np.pi)*(2*norm.cdf(M) - 1) |
18 | 28 | return np.sqrt((2*a*(1 + M**2)/M**2 + b)/(a + b)) |
@@ -55,18 +65,27 @@ def estimate_integration_constant(x, x_hat, M=6): |
55 | 65 |
|
56 | 66 |
|
57 | 67 | def mean_kernel(window_size): |
58 | | - """A uniform boxcar of total integral 1""" |
| 68 | + """A uniform boxcar of total integral 1 |
| 69 | + :param int window_size: the width of the return value |
| 70 | + :return: **kernel** (np.array[float]) -- samples of the kernel function |
| 71 | + """ |
59 | 72 | return np.ones(window_size)/window_size |
60 | 73 |
|
61 | 74 | def gaussian_kernel(window_size): |
62 | | - """A truncated gaussian""" |
| 75 | + """A truncated gaussian |
| 76 | + :param int window_size: the width of the return value |
| 77 | + :return: **kernel** (np.array[float]) -- samples of the kernel function |
| 78 | + """ |
63 | 79 | sigma = window_size / 6. |
64 | 80 | t = np.linspace(-2.7*sigma, 2.7*sigma, window_size) |
65 | 81 | ker = 1/np.sqrt(2*np.pi*sigma**2) * np.exp(-(t**2)/(2*sigma**2)) # gaussian function itself |
66 | 82 | return ker / np.sum(ker) |
67 | 83 |
|
68 | 84 | def friedrichs_kernel(window_size): |
69 | | - """A bump function""" |
| 85 | + """A bump function |
| 86 | + :param int window_size: the width of the return value |
| 87 | + :return: **kernel** (np.array[float]) -- samples of the kernel function |
| 88 | + """ |
70 | 89 | x = np.linspace(-0.999, 0.999, window_size) |
71 | 90 | ker = np.exp(-1/(1-x**2)) |
72 | 91 | return ker / np.sum(ker) |
@@ -104,8 +123,7 @@ def slide_function(func, x, dt, kernel, *args, stride=1, pass_weights=False, **k |
104 | 123 | :param bool pass_weights: whether weights should be passed to func via update to kwargs |
105 | 124 | :param dict kwargs: passed to func |
106 | 125 |
|
107 | | - :return: tuple[np.array, np.array] of\n |
108 | | - - **x_hat** -- estimated (smoothed) x |
| 126 | + :return: - **x_hat** -- estimated (smoothed) x |
109 | 127 | - **dxdt_hat** -- estimated derivative of x |
110 | 128 | """ |
111 | 129 | if len(kernel) % 2 == 0: raise ValueError("Kernel window size should be odd.") |
@@ -148,8 +166,7 @@ def peakdet(x, delta, t=None): |
148 | 166 | if it has the maximal value, and was preceded (to the left) by a value lower by delta. |
149 | 167 | :param np.array[float] t: optional domain points where data comes from, to make indices into locations |
150 | 168 |
|
151 | | - :return: tuple[np.array, np.array] of\n |
152 | | - - **maxtab** -- indices or locations (column 1) and values (column 2) of maxima |
| 169 | + :return: - **maxtab** -- indices or locations (column 1) and values (column 2) of maxima |
153 | 170 | - **mintab** -- indices or locations (column 1) and values (column 2) of minima |
154 | 171 | """ |
155 | 172 | maxtab = [] |
|
0 commit comments