summaryrefslogtreecommitdiffstats
path: root/quantiles.c
blob: 52953db46cf534103261fb7645ae1db82f968b1a (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2022
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * 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.
 *
 **********************************************************************

  =======================================================================

  Estimation of quantiles using the Frugal-2U streaming algorithm
  (https://arxiv.org/pdf/1407.1121v1.pdf)
  */

#include "config.h"

#include "logging.h"
#include "memory.h"
#include "quantiles.h"
#include "regress.h"
#include "util.h"

/* Maximum number of repeated estimates for stabilisation */
#define MAX_REPEAT 64

struct Quantile {
  double est;
  double step;
  int sign;
};

struct QNT_Instance_Record {
  struct Quantile *quants;
  int n_quants;
  int repeat;
  int q;
  int min_k;
  double min_step;
  int n_set;
};

/* ================================================== */

QNT_Instance
QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step)
{
  QNT_Instance inst;
  long seed;

  if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q ||
      repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0)
    assert(0);

  inst = MallocNew(struct QNT_Instance_Record);
  inst->n_quants = (max_k - min_k + 1) * repeat;
  inst->quants = MallocArray(struct Quantile, inst->n_quants);
  inst->repeat = repeat;
  inst->q = q;
  inst->min_k = min_k;
  inst->min_step = min_step;

  QNT_Reset(inst);

  /* Seed the random number generator, which will not be isolated from
     other instances and other random() users */
  UTI_GetRandomBytes(&seed, sizeof (seed));
  srandom(seed);

  return inst;
}

/* ================================================== */

void
QNT_DestroyInstance(QNT_Instance inst)
{
  Free(inst->quants);
  Free(inst);
}

/* ================================================== */

void
QNT_Reset(QNT_Instance inst)
{
  int i;

  inst->n_set = 0;

  for (i = 0; i < inst->n_quants; i++) {
    inst->quants[i].est = 0.0;
    inst->quants[i].step = inst->min_step;
    inst->quants[i].sign = 1;
  }
}

/* ================================================== */

static void
insert_initial_value(QNT_Instance inst, double value)
{
  int i, j, r = inst->repeat;

  if (inst->n_set * r >= inst->n_quants)
    assert(0);

  /* Keep the initial estimates repeated and ordered */
  for (i = inst->n_set; i > 0 && inst->quants[(i - 1) * r].est > value; i--) {
    for (j = 0; j < r; j++)
      inst->quants[i * r + j].est = inst->quants[(i - 1) * r].est;
  }

  for (j = 0; j < r; j++)
    inst->quants[i * r + j].est = value;
  inst->n_set++;

  /* Duplicate the largest value in unset quantiles */
  for (i = inst->n_set * r; i < inst->n_quants; i++)
    inst->quants[i].est = inst->quants[i - 1].est;
}

/* ================================================== */

static void
update_estimate(struct Quantile *quantile, double value, double p, double rand,
                double min_step)
{
  if (value > quantile->est && rand > (1.0 - p)) {
    quantile->step += quantile->sign > 0 ? min_step : -min_step;
    quantile->est += quantile->step > 0.0 ? fabs(quantile->step) : min_step;
    if (quantile->est > value) {
      quantile->step += value - quantile->est;
      quantile->est = value;
    }
    if (quantile->sign < 0 && quantile->step > min_step)
      quantile->step = min_step;
    quantile->sign = 1;
  } else if (value < quantile->est && rand > p) {
    quantile->step += quantile->sign < 0 ? min_step : -min_step;
    quantile->est -= quantile->step > 0.0 ? fabs(quantile->step) : min_step;
    if (quantile->est < value) {
      quantile->step += quantile->est - value;
      quantile->est = value;
    }
    if (quantile->sign > 0 && quantile->step > min_step)
      quantile->step = min_step;
    quantile->sign = -1;
  }
}

/* ================================================== */

void
QNT_Accumulate(QNT_Instance inst, double value)
{
  double p, rand;
  int i;

  /* Initialise the estimates with first received values */
  if (inst->n_set * inst->repeat < inst->n_quants) {
    insert_initial_value(inst, value);
    return;
  }

  for (i = 0; i < inst->n_quants; i++) {
    p = (double)(i / inst->repeat + inst->min_k) / inst->q;
    rand = (double)random() / ((1U << 31) - 1);

    update_estimate(&inst->quants[i], value, p, rand, inst->min_step);
  }
}

/* ================================================== */

int
QNT_GetMinK(QNT_Instance inst)
{
  return inst->min_k;
}

/* ================================================== */

double
QNT_GetQuantile(QNT_Instance inst, int k)
{
  double estimates[MAX_REPEAT];
  int i;

  if (k < inst->min_k || k - inst->min_k >= inst->n_quants)
    assert(0);

  for (i = 0; i < inst->repeat; i++)
    estimates[i] = inst->quants[(k - inst->min_k) * inst->repeat + i].est;

  return RGR_FindMedian(estimates, inst->repeat);
}