libcootapi
 
Loading...
Searching...
No Matches
coot-density-stats.hh
1/* coot-utils/coot-coord-rama.hh
2 *
3 * Copyright 2014 by Medical Research Council
4 * Author: Paul Emsley
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
20 */
21
22#ifndef COOT_DENSITY_STATS_HH
23#define COOT_DENSITY_STATS_HH
24
25#include <vector>
26#include <cmath>
27
28namespace coot {
29
30 enum map_stats_t {
31 SIMPLE,
32 WITH_KOLMOGOROV_SMIRNOV_DIFFERENCE_MAP_TEST};
33
34 namespace util {
35
36 class density_stats_info_t {
37
38 public:
39 // these are doubles because we can potentially do lots (millions) of tiny adds.
40 double n;
41 double sum_sq; // sum of squared elements
42 double sum;
43 double sum_weight;
44
45 density_stats_info_t() {
46 n = 0.0;
47 sum = 0.0;
48 sum_sq = 0.0;
49 sum_weight = 0.0;
50 }
51 void add(const double &v) {
52 n += 1.0;
53 sum += v;
54 sum_sq += v*v;
55 sum_weight += 1.0;
56 }
57 void add(const double &v, const double &weight) {
58 n += weight;
59 sum += weight*v;
60 sum_sq += weight*v*v;
61 sum_weight += 1.0;
62 }
63 std::pair<double, double> mean_and_variance() const {
64 double mean = -1;
65 double var = -1;
66 if (n > 0) {
67 mean = sum/sum_weight;
68 var = sum_sq/sum_weight - mean*mean;
69 }
70 return std::pair<double, double> (mean, var);
71 }
72 };
73
75 public:
76 double top;
77 double var_x, var_y;
78 };
79
82 public:
83 double n;
84 double sum_xy;
85 double sum_sqrd_x;
86 double sum_sqrd_y;
87 double sum_x;
88 double sum_y;
89 // for doing KS tests (against normal distribution) , we want
90 // all the density samples
91 std::vector<double> density_values;
94 n = 0;
95 sum_xy = 0;
96 sum_sqrd_x = 0;
97 sum_sqrd_y = 0;
98 sum_x = 0;
99 sum_y = 0;
100 }
101
103 double sum_xy_in,
104 double sum_sqrd_x_in,
105 double sum_sqrd_y_in,
106 double sum_x_in,
107 double sum_y_in) {
108 n = n_in;
109 sum_xy = sum_xy_in;
110 sum_sqrd_x = sum_sqrd_x_in;
111 sum_sqrd_y = sum_sqrd_y_in;
112 sum_x = sum_x_in;
113 sum_y = sum_y_in;
114 }
115 double var_x() const {
116 double mean_x = sum_x/n;
117 return (sum_sqrd_x/n - mean_x * mean_x);
118 }
119 double var_y() const {
120 double mean_y = sum_y/n;
121 return (sum_sqrd_y/n - mean_y * mean_y);
122 }
123 void add(const double &x, const double &y) {
124 n += 1;
125 sum_x += x;
126 sum_y += y;
127 sum_xy += x * y;
128 sum_sqrd_x += x * x;
129 sum_sqrd_y += y * y;
130 }
132 double correlation() const {
133 double top = n * sum_xy - sum_x * sum_y;
134 double b_1 = n * sum_sqrd_x - sum_x * sum_x;
135 double b_2 = n * sum_sqrd_y - sum_y * sum_y;
136 if (b_1 < 0) b_1 = 0;
137 if (b_2 < 0) b_2 = 0;
138 double c = top/(std::sqrt(b_1) * std::sqrt(b_2));
139 return c;
140 }
141
143 double top = n * sum_xy - sum_x * sum_y;
144 double b_1 = n * sum_sqrd_x - sum_x * sum_x;
145 double b_2 = n * sum_sqrd_y - sum_y * sum_y;
146 if (b_1 < 0) b_1 = 0;
147 if (b_2 < 0) b_2 = 0;
149 c.top = top; c.var_x = b_1; c.var_y = b_2;
150 return c;
151 }
152 bool operator<(const density_correlation_stats_info_t &dcsi) const {
153 return sum_xy < dcsi.sum_xy; // or something.
154 };
155 };
156
157 }
158}
159
160#endif // COOT_DENSITY_STATS_HH
Definition coot-density-stats.hh:74
density correlation state - between map and model typically.
Definition coot-density-stats.hh:81
density_correlation_stats_info_t()
constructor
Definition coot-density-stats.hh:93
density_correlation_stats_info_t(double n_in, double sum_xy_in, double sum_sqrd_x_in, double sum_sqrd_y_in, double sum_x_in, double sum_y_in)
constructor
Definition coot-density-stats.hh:102
correlation_parts_t correlation_by_parts() const
the correlation by parts
Definition coot-density-stats.hh:142
double correlation() const
the correlation
Definition coot-density-stats.hh:132