File: signpr_l1fit.c

package info (click to toggle)
gramofile 1.6-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 840 kB
  • ctags: 538
  • sloc: ansic: 10,238; makefile: 55
file content (162 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download | duplicates (6)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* 
   signpr_l1fit.c

   Does L1 norm fit to supplied data, using integer arithmetic.

   Copyright (C) 1999 S.J. Tappin

   Licensed under the terms of the GNU General Public License.
   ABSOLUTELY NO WARRANTY.
   See the file `COPYING' in this directory.
 */


#include <stdlib.h>
#include <math.h>
#include "signpr_general.h"
#include "signpr_l1fit.h"

long
mdfunc (b, x, y, a, n, tmp1)
     double b, *a;
     signed short *x, *y, *tmp1;
     int n;
{
  int i;
  long result;
  double dtmp;

  for (i = 0; i < n; i++)
    {
      tmp1[i] = (signed short) rint (y[i] - b * x[i]);
    }

  *a = (double) median (tmp1, n);

  result = 0;
  for (i = 0; i < n; i++)
    {
      dtmp = (y[i] - (b * x[i] + *a));
      if (y[i] != 0)
	dtmp = dtmp / abs (y[i]);
      if (dtmp > 0)
	{
	  result += x[i];
	}
      else if (dtmp < 0)
	{
	  result -= x[i];
	}
    }


  return (result);
}

void
l1fit (x, y, n, a, b)
     signed short *x, *y;
     double *a, *b;
     int n;
{
  int sx, sy, sxx, sxy;
  int del;
  double aa, bb, c2, sigb;
  double b1, b2, delb;
  long f1, f2, f;
  int i;

  signed short *tmp1;

  sx = 0;
  sy = 0;
  sxx = 0;
  sxy = 0;

  for (i = 0; i < n; i++)
    {
      sx += x[i];
      sy += y[i];
      sxx += x[i] * x[i];
      sxy += x[i] * y[i];
    }

  del = n * sxx - sx * sx;

  if (del == 0)
    {				/* All X's the same only fit horizontal */
      *a = (double) median (y, n);
      *b = 0.;
      return;
    }

  /*
     Use the normal least squares (L2) fit as a starting point.
   */

  aa = (sxx * sy - sx * sxy) / (double) del;
  bb = (n * sxy - sx * sy) / (double) del;

  c2 = 0.;
  for (i = 0; i < n; i++)
    c2 += (y[i] - (aa + bb * x[i])) * (y[i] - (aa + bb * x[i]));
  sigb = sqrt (c2 / del);

  /* Perfect fit, L1 must give the same answer so go straight back */

  if (c2 == 0.)
    {
      *a = aa;
      *b = bb;
      return;
    }

  tmp1 = malloc (n * sizeof (short));

  b1 = bb;
  f1 = mdfunc (b1, x, y, &aa, n, tmp1);
  if (f1 >= 0)
    {
      delb = sigb * 3.;
    }
  else
    {
      delb = -sigb * 3.;
    }

  b2 = b1 + delb;
  f2 = mdfunc (b2, x, y, &aa, n, tmp1);

  while (f1 * f2 > 0)
    {
      b1 = b2;
      f1 = f2;
      b2 = b1 + delb;
      f2 = mdfunc (b2, x, y, &aa, n, tmp1);
    }

  sigb *= 0.01;

  while (fabs (b1 - b2) > sigb)
    {
      bb = (b1 + b2) / 2.;
      if (bb == b1 || bb == b2)
	break;
      f = mdfunc (bb, x, y, &aa, n, tmp1);
      if (f * f1 >= 0)
	{
	  f1 = f;
	  b1 = bb;
	}
      else
	{
	  f2 = f;
	  b2 = bb;
	}
    }

  free (tmp1);

  *a = aa;
  *b = bb;
}