From 78d8de381f0b1e99ad98364709cbf876689628b2 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Fri, 29 Jun 2018 15:43:10 -0400 Subject: completely removed measurement during the simulation, opting to just save binary data points to files throughout --- lib/measurement.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 8 deletions(-) (limited to 'lib/measurement.c') diff --git a/lib/measurement.c b/lib/measurement.c index ad824f6..b30cf6b 100644 --- a/lib/measurement.c +++ b/lib/measurement.c @@ -1,6 +1,15 @@ +#include "convex.h" #include "measurement.h" +meas_t *meas_initialize(count_t W) { + meas_t *m = (meas_t *)calloc(1, sizeof(meas_t)); + m->W = W; + m->xx = (double *)calloc(2 * W + 1, sizeof(double)); + + return m; +} + double add_to_avg(double mx, double x, count_t n) { return mx * (n / (n + 1.0)) + x / (n + 1.0); } @@ -10,24 +19,42 @@ void meas_update(meas_t *m, double x) { m->x = add_to_avg(m->x, x, n); m->x2 = add_to_avg(m->x2, pow(x, 2), n); + m->x4 = add_to_avg(m->x4, pow(x, 4), n); m->m2 = add_to_avg(m->m2, pow(x - m->x, 2), n); m->m4 = add_to_avg(m->m4, pow(x - m->x, 4), n); - /* - if (n > 1) { - double s2 = n / (n - 1.) * (m->x2 - pow(m->x, 2)); - m->dx = sqrt(s2 / n); - m->c = s2; - m->dc = sqrt((m->m4 - (n - 3.)/(n - 1.) * pow(m->m2, 2)) / n); + dll_t *tmp_window = m->x_window; + dll_t *pos_save; + count_t t = 0; + + while (tmp_window != NULL) { + m->xx[t] = add_to_avg(m->xx[t], x * (tmp_window->x), m->n - t - 1); + t++; + if (t == 2 * (m->W)) { + pos_save = tmp_window; + } + tmp_window = tmp_window->next; } - */ + + if (t == 2 * (m->W) + 1) { + if (2 * (m->W) + 1 == 1) { + free(m->x_window); + m->x_window = NULL; + } else { + free(pos_save->next); + pos_save->next = NULL; + } + } + + stack_push_d(&(m->x_window), x); (m->n)++; } double meas_dx(const meas_t *m) { - return sqrt(1. / (m->n - 1.) * (m->x2 - pow(m->x, 2))); + return 2 * get_tau(m) * Cxx(m, 0) / m->n; +// return sqrt(1. / (m->n - 1.) * (m->x2 - pow(m->x, 2))); } double meas_c(const meas_t *m) { @@ -74,3 +101,105 @@ double rho(const autocorr_t *o, count_t i) { return (o->OO[i] - pow(o->O, 2)) / (o->O2 - pow(o->O, 2)); } +double Cxx(const meas_t *m, count_t t) { + return m->xx[t] - pow(m->x, 2); +} + +double rho_m(const meas_t *m, count_t t) { + return Cxx(m, t) / Cxx(m, 0); +} + +double get_tau(const meas_t *m) { + double *Gammas = (double *)malloc((m->W + 1) * sizeof(double)); + + Gammas[0] = 1 + rho_m(m, 0); + for (uint64_t i = 0; i < m->W; i++) { + Gammas[1 + i] = rho_m(m, 2 * i + 1) + rho_m(m, 2 * i + 2); + } + + uint64_t n; + for (n = 0; n < m->W + 1; n++) { + if (Gammas[n] <= 0) { + break; + } + } + + double *conv_Gamma = get_convex_minorant(n, Gammas); + + double tau = - 0.5; + + for (uint64_t i = 0; i < n + 1; i++) { + tau += conv_Gamma[i]; + } + + free(Gammas); + + return tau; +} + +void print_meas(const meas_t *m, const char *sym, FILE *outfile) { + fprintf(outfile, "%s-><|n->%" PRIcount ",x->%.15f,x^2->%.15f,x^4->%.15f,xx->{", sym, m->n, m->x, m->x2, m->x4); + for (count_t i = 0; i < 2 * (m->W) + 1; i++) { + fprintf(outfile, "%.15f", m->xx[i]); + if (i < 2 * (m->W)) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "}|>"); +} + +void print_vec_meas(q_t q, const meas_t **m, const char *sym, FILE *outfile) { + fprintf(outfile, "%s-><|n->{", sym); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%" PRIcount, m[i]->n); + if (i < q - 1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},x->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", m[i]->x); + if (i < q - 1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},x^2->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", m[i]->x2); + if (i < q - 1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},x^4->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", m[i]->x4); + if (i < q - 1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},xx->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "{"); + for (count_t j = 0; j < 2 * (m[i]->W) + 1; j++) { + fprintf(outfile, "%.15f", m[i]->xx[j]); + if (j < 2 * (m[i]->W)) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "}"); + if (i < q - 1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "}|>"); +} + +void free_meas(meas_t *m) { + free(m->xx); + while (m->x_window != NULL) { + stack_pop_d(&(m->x_window)); + } + free(m); +} + + -- cgit v1.2.3-70-g09d2