File: mad_source.c

package info (click to toggle)
gsl 2.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,628 kB
  • sloc: ansic: 245,573; sh: 11,435; makefile: 911; python: 68
file content (66 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (7)
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
/* statistics/mad_source.c
 * 
 * Copyright (C) 2018 Patrick Alken
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/*
gsl_stats_mad0()
  Compute median absolute deviation

Inputs: data    - array containing the observations
        stride  - stride
        n       - length of 'data'
        work    - workspace of length n

Return: MAD statistic (without scale/correction factor)
*/

double
FUNCTION(gsl_stats,mad0) (const BASE data[],
                          const size_t stride,
                          const size_t n,
                          double work[])
{
  double median, mad;
  size_t i;

  /* copy input data to work */
  for (i = 0; i < n; ++i)
    work[i] = (double) data[i * stride];

  /* compute median of input data using double version */
  median = gsl_stats_median(work, 1, n);

  /* compute absolute deviations from median */
  for (i = 0; i < n; ++i)
    work[i] = fabs((double) data[i * stride] - median);

  mad = gsl_stats_median(work, 1, n);

  return mad;
}

double
FUNCTION(gsl_stats,mad) (const BASE data[],
                         const size_t stride,
                         const size_t n,
                         double work[])
{
  double mad0 = FUNCTION(gsl_stats,mad0)(data, stride, n, work);
  double mad = 1.482602218505602 * mad0;
  return mad;
}