aboutsummaryrefslogtreecommitdiff
path: root/src/statistics.c
blob: e0a2e71a85f23f5437e037131d8840e5b9c7356f (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * statistics.c
 *
 * Structure-factor statistics
 *
 * (c) 2006-2010 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_min.h>
#include <gsl/gsl_statistics.h>

#include "statistics.h"
#include "utils.h"


struct r_params {
	const double *ref1;
	const double *ref2;
	ReflItemList *items;  /* Which reflections to use */
	int fom;              /* Which FoM to use (see the enum just below) */
};

enum {
	R_2,
	R_MERGE,
};


/* Return the least squares optimal scaling factor when comparing intensities.
 * ref1,ref2 are the two intensity lists to compare.  "items" is a ReflItemList
 * containing the reflections which should be taken into account.
 */
double stat_scale_intensity(const double *ref1, const double *ref2,
                            ReflItemList *items)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	for ( i=0; i<num_items(items); i++ ) {

		double i1, i2;
		struct refl_item *it;
		signed int h, k, l;

		it = get_item(items, i);
		h = it->h;  k = it->k;  l = it->l;

		i1 = lookup_intensity(ref1, h, k, l);
		i2 = lookup_intensity(ref2, h, k, l);

		top += i1 * i2;
		bot += i2 * i2;

	}

	return top/bot;
}


/* Return the least squares optimal scaling factor when comparing the square
 * roots of the intensities (i.e. one approximation to the structure factor
 * moduli).
 * ref1,ref2 are the two intensity lists to compare (they contain intensities,
 * not square rooted intensities).  "items" is a ReflItemList containing the
 * reflections which should be taken into account.
 */
static double stat_scale_sqrti(const double *ref1, const double *ref2,
                               ReflItemList *items)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	for ( i=0; i<num_items(items); i++ ) {

		double i1, i2, f1, f2;
		struct refl_item *it;
		signed int h, k, l;

		it = get_item(items, i);
		h = it->h;  k = it->k;  l = it->l;

		i1 = lookup_intensity(ref1, h, k, l);
		f1 = i1 > 0.0 ? sqrt(i1) : 0.0;
		i2 = lookup_intensity(ref2, h, k, l);
		f2 = i2 > 0.0 ? sqrt(i2) : 0.0;

		top += f1 * f2;
		bot += f2 * f2;

	}

	return top/bot;
}


static double internal_r2(const double *ref1, const double *ref2,
                          ReflItemList *items, double scale)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	for ( i=0; i<num_items(items); i++ ) {

		double i1, i2;
		struct refl_item *it;
		signed int h, k, l;

		it = get_item(items, i);
		h = it->h;  k = it->k;  l = it->l;

		i1 = lookup_intensity(ref1, h, k, l);
		i2 = scale * lookup_intensity(ref2, h, k, l);

		top += pow(i1 - i2, 2.0);
		bot += pow(i1, 2.0);

	}

	return sqrt(top/bot);
}


static double internal_rmerge(const double *ref1, const double *ref2,
                              ReflItemList *items, double scale)
{
	double top = 0.0;
	double bot = 0.0;
	int i;

	for ( i=0; i<num_items(items); i++ ) {

		double i1, i2, f1, f2;
		struct refl_item *it;
		signed int h, k, l;

		it = get_item(items, i);
		h = it->h;  k = it->k;  l = it->l;

		i1 = lookup_intensity(ref1, h, k, l);
		f1 = i1 > 0.0 ? sqrt(i1) : 0.0;
		i2 = lookup_intensity(ref2, h, k, l);
		f2 = i2 > 0.0 ? sqrt(i2) : 0.0;
		f2 *= scale;

		top += fabs(f1 - f2);
		bot += f1 + f2;

	}

	return 2.0*top/bot;
}


static double calc_r(double scale, void *params)
{
	struct r_params *rp = params;

	switch ( rp->fom) {
	case R_MERGE :
		return internal_rmerge(rp->ref1, rp->ref2, rp->items, scale);
	case R_2 :
		return internal_r2(rp->ref1, rp->ref2, rp->items, scale);
	}

	ERROR("No such FoM!\n");
	abort();
}


static double r_minimised(const double *ref1, const double *ref2,
                          ReflItemList *items, double *scalep, int fom)
{
	gsl_function F;
	gsl_min_fminimizer *s;
	int status;
	double scale = 1.0;
	struct r_params rp;
	int iter = 0;

	rp.ref1 = ref1;
	rp.ref2 = ref2;
	rp.items = items;
	rp.fom = fom;

	F.function = &calc_r;
	F.params = &rp;

	s = gsl_min_fminimizer_alloc(gsl_min_fminimizer_brent);

	/* Initial guess */
	switch ( fom ) {
	case R_MERGE :
		scale = stat_scale_sqrti(ref1, ref2, items);
		break;
	case R_2 :
		scale = stat_scale_intensity(ref1, ref2, items);
		break;
	}
	//STATUS("Initial scale factor estimate: %5.2e\n", scale);

	/* Probably within an order of magnitude either side */
	gsl_min_fminimizer_set(s, &F, scale, scale/10.0, scale*10.0);

	do {

		double lo, up;

		/* Iterate */
		gsl_min_fminimizer_iterate(s);

		/* Get the current estimate */
		scale = gsl_min_fminimizer_x_minimum(s);
		lo = gsl_min_fminimizer_x_lower(s);
		up = gsl_min_fminimizer_x_upper(s);

		/* Check for convergence */
		status = gsl_min_test_interval(lo, up, 0.001, 0.0);

		iter++;

	} while ( status == GSL_CONTINUE );

	if (status != GSL_SUCCESS) {
		ERROR("Scale factor minimisation failed.\n");
	}

	gsl_min_fminimizer_free(s);

	//STATUS("Final scale factor: %5.2e\n", scale);
	*scalep = scale;
	return calc_r(scale, &rp);
}


double stat_rmerge(const double *ref1, const double *ref2,
                   ReflItemList *items, double *scalep)
{
	return r_minimised(ref1, ref2, items, scalep, R_MERGE);
}


double stat_r2(const double *ref1, const double *ref2,
               ReflItemList *items, double *scalep)
{
	return r_minimised(ref1, ref2, items, scalep, R_2);
}


double stat_pearson(const double *ref1, const double *ref2, ReflItemList *items)
{
	double *vec1, *vec2;
	int i = 0;
	int ni = num_items(items);
	double val;

	vec1 = malloc(ni*sizeof(double));
	vec2 = malloc(ni*sizeof(double));

	for ( i=0; i<ni; i++ ) {

		double i1, i2, f1, f2;
		struct refl_item *it;
		signed int h, k, l;

		it = get_item(items, i);
		h = it->h;  k = it->k;  l = it->l;

		i1 = lookup_intensity(ref1, h, k, l);
		f1 = i1 > 0.0 ? sqrt(i1) : 0.0;
		i2 = lookup_intensity(ref2, h, k, l);
		f2 = i2 > 0.0 ? sqrt(i2) : 0.0;

		vec1[i] = f1;
		vec2[i] = f2;

	}

	val = gsl_stats_correlation(vec1, 1, vec2, 1, i);
	free(vec1);
	free(vec2);

	return val;
}