-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoisson_Likelihood_Comparison.py
More file actions
610 lines (496 loc) · 25.6 KB
/
Copy pathPoisson_Likelihood_Comparison.py
File metadata and controls
610 lines (496 loc) · 25.6 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import pandas as pd
import math
import matplotlib
import numpy as np
import functions as fn
import time
import scipy.special as scispec
import scipy.optimize as scopt
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
"""Methodology for Conducting Gaussian Regression for 2-D"""
def mean_func_zero(c): # Prior mean function taken as 0 for the entire sampling range
if np.array([c.shape]).size == 1:
mean_c = np.zeros(1) # Make sure this is an array
else:
mean_c = np.zeros(c.shape[1])
return mean_c # Outputs a x and y coordinates, created from the mesh grid
def mean_func_scalar(mean, c): # Assume that the prior mean is a constant to be optimised
if np.array([c.shape]).size == 1:
mean_c = np.ones(1) * mean
else:
mean_c = np.ones(c.shape[1]) * mean
return mean_c
def squared_exp_2d(sigma_exp, length_exp, x1, x2): # Only for 2-D
# Define horizontal and vertical dimensions of covariance matrix c
if np.array([x1.shape]).size == 1 and np.array([x2.shape]).size != 1 and x1.size == x2.shape[0]:
rows = 1
columns = x2.shape[1]
elif np.array([x2.shape]).size == 1 and np.array([x1.shape]).size != 1 and x2.size == x1.shape[0]:
rows = x1.shape[1]
columns = 1
elif np.array([x1.shape]).size == 1 and np.array([x2.shape]).size == 1 and x1.size == x2.size:
rows = 1
columns = 1
else:
rows = x1.shape[1]
columns = x2.shape[1]
c = np.zeros((rows, columns))
for i in range(c.shape[0]):
for j in range(c.shape[1]):
if np.array([x1.shape]).size == 1 and np.array([x2.shape]).size != 1:
diff = x1 - x2[:, j]
elif np.array([x1.shape]).size != 1 and np.array([x2.shape]).size == 1:
diff = x1[:, i] - x2
elif np.array([x1.shape]).size == 1 and np.array([x2.shape]).size == 1:
diff = x1 - x2
else:
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
exp_power = np.exp(-1 * (euclidean ** 2) * (length_exp ** -2))
c[i, j] = (sigma_exp ** 2) * exp_power
return c # Note that this creates the covariance matrix directly
# This is way faster than the function above beyond n=10
def fast_squared_exp_2d(sigma_exp, length_exp, x1, x2): # there are only two variables in the matern function
"""
This is much much faster than iteration over every point beyond n = 10. This function takes advantage of the
symmetry in the covariance matrix and allows for fast regeneration.
:param sigma_exp: coefficient factor at the front
:param length_exp: length scale
:param x1: First set of coordinates for iteration
:param x2: Second set of coordinates for iteration
:return: Covariance matrix with squared exponential kernel - indicating infinite differentiability
"""
# Note that this function only takes in 2-D coordinates, make sure there are 2 rows and n columns
n = x1.shape[1]
cov_matrix = np.zeros((n, n))
for i in range(n):
cov_matrix[i, i] = sigma_exp ** 2
for j in range(i + 1, n):
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
exp_power = np.exp(-1 * (euclidean ** 2) * (length_exp ** -2))
cov_matrix[i, j] = (sigma_exp ** 2) * exp_power
cov_matrix[j, i] = cov_matrix[i, j]
return cov_matrix
def matern_2d(v_value, sigma_matern, length_matern, x1, x2): # there are only two variables in the matern function
# Define horizontal and vertical dimensions of covariance matrix c
if np.array([x1.shape]).size == 1 and np.array([x2.shape]).size != 1 and x1.size == x2.shape[0]:
rows = 1
columns = x2.shape[1]
elif np.array([x2.shape]).size == 1 and np.array([x1.shape]).size != 1 and x2.size == x1.shape[0]:
rows = x1.shape[1]
columns = 1
elif np.array([x1.shape]).size == 1 and np.array([x2.shape]).size == 1 and x1.size == x2.size:
rows = 1
columns = 1
else:
rows = x1.shape[1]
columns = x2.shape[1]
c = np.zeros((rows, columns))
if v_value == 1/2:
for i in range(c.shape[0]):
for j in range(c.shape[1]):
if np.array([x1.shape]).size == 1 and np.array([x2.shape]).size != 1:
diff = x1 - x2[:, j]
elif np.array([x1.shape]).size != 1 and np.array([x2.shape]).size == 1:
diff = x1[:, i] - x2
elif np.array([x1.shape]).size == 1 and np.array([x2.shape]).size == 1:
diff = x1 - x2
else:
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
exp_term = np.exp(-1 * euclidean * (length_matern ** -1))
c[i, j] = (sigma_matern ** 2) * exp_term
if v_value == 3/2:
for i in range(c.shape[0]):
for j in range(c.shape[1]):
if np.array([x1.shape]).size == 1 and np.array([x2.shape]).size != 1:
diff = x1 - x2[:, j]
elif np.array([x1.shape]).size != 1 and np.array([x2.shape]).size == 1:
diff = x1[:, i] - x2
elif np.array([x1.shape]).size == 1 and np.array([x2.shape]).size == 1:
diff = x1 - x2
else:
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
coefficient_term = (1 + np.sqrt(3) * euclidean * (length_matern ** -1))
exp_term = np.exp(-1 * np.sqrt(3) * euclidean * (length_matern ** -1))
c[i, j] = (sigma_matern ** 2) * coefficient_term * exp_term
return c
# Both kernel functions take in numpy arrays of one row (create a single column first)
# This is way faster than the function above beyond n=10
def fast_matern_2d(sigma_matern, length_matern, x1, x2): # there are only two variables in the matern function
"""
This is much much faster than iteration over every point beyond n = 10. This function takes advantage of the
symmetry in the covariance matrix and allows for fast regeneration. For this function, v = 3/2
:param sigma_matern: coefficient factor at the front
:param length_matern: length scale
:param x1: First set of coordinates for iteration
:param x2: Second set of coordinates for iteration
:return: Covariance matrix with matern kernel
"""
# Note that this function only takes in 2-D coordinates, make sure there are 2 rows and n columns
n = x1.shape[1]
cov_matrix = np.zeros((n, n))
for i in range(n):
cov_matrix[i, i] = sigma_matern ** 2
for j in range(i + 1, n):
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
coefficient_term = (1 + np.sqrt(3) * euclidean * (length_matern ** -1))
exp_term = np.exp(-1 * np.sqrt(3) * euclidean * (length_matern ** -1))
cov_matrix[i, j] = (sigma_matern ** 2) * coefficient_term * exp_term
cov_matrix[j, i] = cov_matrix[i, j]
return cov_matrix
def fast_matern_1_2d(sigma_matern, length_matern, x1, x2):
"""
Much faster method of obtaining the Matern v=1/2 covariance matrix by exploiting the symmetry of the
covariance matrix. This is the once-differentiable (zero mean squared differentiable) matern
:param sigma_matern: Coefficient at the front
:param length_matern: Length scale
:param x1: First set of coordinates for iteration
:param x2: Second set of coordinates for iteration
:return: Covariance matrix with matern kernel
"""
# Note that this function only takes in 2-D coordinates, make sure there are 2 rows and n columns
n = x1.shape[1]
cov_matrix = np.zeros((n, n))
for i in range(n):
cov_matrix[i, i] = sigma_matern ** 2
for j in range(i + 1, n):
diff = x1[:, i] - x2[:, j]
euclidean = np.sqrt(np.matmul(diff, np.transpose(diff)))
exp_term = np.exp(-1 * euclidean * (length_matern ** -1))
cov_matrix[i, j] = (sigma_matern ** 2) * exp_term
cov_matrix[j, i] = cov_matrix[i, j]
return cov_matrix
def rational_quadratic_2d(alpha_rq, length_rq, x1, x2):
"""
Rational Quadratic Coveriance function with 2 parameters to be optimized, using
power alpha and length scale l. The Rational Quadratic Kernel is used to model the
volatility of equity index returns, which is equivalent to a sum of Squared
Exponential Kernels. This kernel is used to model multi-scale data
This is a fast method of generating the rational quadratic kernel, by exploiting the symmetry
of the covariance matrix
:param alpha_rq: power and denominator
:param length_rq: length scale
:param x1: First set of coordinates for iteration
:param x2: Second set of coordinates for iteration
:return: Covariance matrix with Rational Quadratic Kernel
"""
# Note that this function only takes in 2-D coordinates, make sure there are 2 rows and n columns
n = x1.shape[1]
cov_matrix = np.zeros((n, n))
for i in range(n):
cov_matrix[i, i] = 1
for j in range(i + 1, n):
diff = x1[:, i] - x2[:, j]
euclidean_squared = np.matmul(diff, np.transpose(diff))
fraction_term = euclidean_squared / (2 * alpha_rq * (length_rq ** 2))
cov_matrix[i, j] = (1 + fraction_term) ** (-1 * alpha_rq)
cov_matrix[j, i] = cov_matrix[i, j]
return cov_matrix
def mu_post(xy_next, c_auto, c_cross, mismatch): # Posterior mean
if c_cross.shape[1] != (np.linalg.inv(c_auto)).shape[0]:
print('First Dimension Mismatch!')
if (np.linalg.inv(c_auto)).shape[1] != (np.transpose(mismatch)).shape[0]:
print('Second Dimension Mismatch!')
else:
mean_post = mean_func_zero(xy_next) + fn.matmulmul(c_cross, np.linalg.inv(c_auto), np.transpose(mismatch))
return mean_post
def var_post(c_next_auto, c_cross, c_auto): # Posterior Covariance
c_post = c_next_auto - fn.matmulmul(c_cross, np.linalg.inv(c_auto), np.transpose(c_cross))
return c_post
def log_gp_likelihood(param, *args): # Param includes both sigma and l, arg is passed as a pointer
"""
Function in format for optimization using Nelder-Mead Simplex Algorithm - change to include the scalar mean as a
value to be optimised as well - total of 4 hyper-parameters to be optimised. Note that Matern v=3/2 is used in the
fast_matern_2d function
:param param: amplitude sigma, length scale and noise amplitude
:param args: locations of quads, xy_quad and histogram values for each quad
:return: the log-likelihood of the gaussian process
"""
# Define parameters to be optimised
sigma = param[0] # param is a tuple containing 2 things, which has already been defined in the function def
length = param[1]
noise = param[2] # Over here we have defined each parameter in the tuple, include noise
mean = param[3]
# Define arguments to be entered
xy_coordinates = args[0] # This argument is a constant passed into the function
histogram_data = args[1] # Have to enter histogram data as well
# Tabulate prior mean as a scalar to be optimized
prior_mu = mean_func_scalar(mean, xy_coordinates) # This creates a matrix with 2 rows
# Tabulate auto-covariance matrix using fast matern function plus noise
c_auto = fast_matern_2d(sigma, length, xy_coordinates, xy_coordinates)
c_noise = np.eye(c_auto.shape[0]) * (noise ** 2) # Fronecker delta function
c_overall = c_auto + c_noise
# 3 components to log_gp_likelihood: model_fit, model_complexity and model_constant
model_fit = - 0.5 * fn.matmulmul(histogram_data - prior_mu, np.linalg.inv(c_overall),
np.transpose(histogram_data - prior_mu))
model_complexity = - 0.5 * math.log(np.linalg.det(c_overall))
model_constant = - 0.5 * len(histogram_data) * math.log(2*np.pi)
log_model_evid = model_fit + model_complexity + model_constant
# Taking the minimum of the negative log_gp_likelihood, to obtain the maximum of log_gp_likelihood
return -log_model_evid
# For the case where gaussian prior with zero mean is assumed
def log_gp_likelihood_zero_mean(param, *args):
"""
Log marginal likelihood which is taken as the objective function for the optimization of the hyper-parameters,
assuming a zero prior mean.
:param param: sigma amplitude, length scale and noise
:param args: coordinates of each quad and histogram data
:return: the negative of the log marginal likelihood for optimization using Nelder-Mead/ DE
"""
# Define parameters to be optimised
sigma = param[0] # param is a tuple containing 2 things, which has already been defined in the function def
length = param[1]
noise = param[2] # Over here we have defined each parameter in the tuple, include noise
# Define arguments to be entered
xy_coordinates = args[0] # This argument is a constant passed into the function
histogram_data = args[1] # Have to enter histogram data as well
# Tabulate prior mean as a scalar to be optimized
prior_mu = mean_func_zero(xy_coordinates) # This creates a matrix with 2 rows
# Tabulate auto-covariance matrix using fast matern function plus noise
# c_auto = fast_matern_2d(sigma, length, xy_coordinates, xy_coordinates)
c_auto = fast_matern_1_2d(sigma, length, xy_coordinates, xy_coordinates)
# c_auto = fast_squared_exp_2d(sigma, length, xy_coordinates, xy_coordinates)
c_noise = np.eye(c_auto.shape[0]) * (noise ** 2) # Fronecker delta function
c_overall = c_auto + c_noise
# 3 components to log_gp_likelihood: model_fit, model_complexity and model_constant
model_fit = - 0.5 * fn.matmulmul(histogram_data - prior_mu, np.linalg.inv(c_overall),
np.transpose(histogram_data - prior_mu))
model_complexity = - 0.5 * math.log(np.linalg.det(c_overall))
model_constant = - 0.5 * len(histogram_data) * math.log(2*np.pi)
log_model_evid = model_fit + model_complexity + model_constant
# Taking the minimum of the negative log_gp_likelihood, to obtain the maximum of log_gp_likelihood
return -log_model_evid
# Matern Covariance 1/2
def short_log_integrand_data(param, *args):
"""
1. Shorter version that tabulates only the log of the GP prior. Includes only terms
containing the covariance matrix elements that are made up of the kernel hyper-parameters
2. Kernel: Matern(3/2), Matern(1/2), Squared Exponential
3. Assume a constant latent intensity, even at locations without any incidences
:param param: hyperparameters - sigma, length scale and noise, prior scalar mean - array of 4 elements
:param args: xy coordinates for input into the covariance function and the histogram
:return: the log of the GP Prior, log[N(prior mean, covariance matrix)]
"""
# Generate Matern Covariance Matrix
# Enter parameters
sigma = param[0]
length = param[1]
noise = param[2]
scalar_mean = param[3]
# Enter Arguments
xy_coordinates = args[0]
data_array = args[1] # Note that this is refers to the optimised log-intensity array
# Set up inputs for generation of objective function
p_mean = mean_func_scalar(scalar_mean, xy_coordinates)
# Change_Param
# c_auto = fast_matern_2d(sigma, length, xy_coordinates, xy_coordinates)
c_auto = fast_matern_1_2d(sigma, length, xy_coordinates, xy_coordinates)
# c_auto = fast_squared_exp_2d(sigma, length, xy_coordinates, xy_coordinates)
c_noise = np.eye(c_auto.shape[0]) * (noise ** 2) # Fro-necker delta function
cov_matrix = c_auto + c_noise
"""Generate Objective Function = log[g(v)]"""
# Generate Determinant Term (after taking log)
determinant = np.exp(np.linalg.slogdet(cov_matrix))[1]
det_term = -0.5 * np.log(2 * np.pi * determinant)
# Generate Euclidean Term (after taking log)
data_diff = data_array - p_mean
inv_covariance_matrix = np.linalg.inv(cov_matrix)
euclidean_term = -0.5 * fn.matmulmul(data_diff, inv_covariance_matrix, data_diff)
"""Summation of all terms change to correct form to find minimum point"""
log_gp = det_term + euclidean_term
log_gp_minimization = -1 * log_gp # Make the function convex for minimization
return log_gp_minimization
# Matern 3/2
def short_log_integrand_data_rq(param, *args):
"""
Optimization using the Rational Quadratic Kernel, with hyper-parameters alpha and
length scale, while taking in coordinates and histo quad as inputs
:param param: alpha, length_scale
:param args: Coordinates and values of data points after taking the histogram
:return: the negative of the marginal log likelihood (which we then have to minimize)
"""
# Generate Rational Quadratic Covariance Matrix
# Enter parameters
alpha = param[0]
length = param[1]
noise = param[2]
scalar_mean = param[3]
# Enter Arguments
xy_coordinates = args[0]
data_array = args[1] # Note that this is refers to the optimised log-intensity array
# Set up inputs for generation of objective function
p_mean = mean_func_scalar(scalar_mean, xy_coordinates)
# Create Rational Quadratic Covariance Matrix including noise
c_auto = rational_quadratic_2d(alpha, length, xy_coordinates, xy_coordinates)
c_noise = np.eye(c_auto.shape[0]) * (noise ** 2) # Fro-necker delta function
cov_matrix = c_auto + c_noise
# Generate Determinant Term (after taking log)
determinant = np.exp(np.linalg.slogdet(cov_matrix))[1]
det_term = -0.5 * np.log(2 * np.pi * determinant)
# Generate Euclidean Term (after taking log)
data_diff = data_array - p_mean
inv_covariance_matrix = np.linalg.inv(cov_matrix)
euclidean_term = -0.5 * fn.matmulmul(data_diff, inv_covariance_matrix, data_diff)
"""Summation of all terms change to correct form to find minimum point"""
log_gp = det_term + euclidean_term
log_gp_minimization = -1 * log_gp # Make the function convex for minimization
return log_gp_minimization
def log_poisson_likelihood_opt(param, *args):
"""
Considers only the log-likelihood of the Poisson distribution in front of the gaussian process to optimize
latent values - note that there are no hyper-parameters here to consider. The log-likelhood is taken as
the natural log is monotically increasing
:param param: v_array containing the latent intensities
:param args: k_array which is the data set
:return: log of the combined poisson distributions
"""
# Define parameters and arguments
v_array = param
k_array = args[0]
# Generate Objective Function: log(P(D|v))
exp_term = -1 * np.sum(np.exp(v_array))
product_term = np.matmul(v_array, np.transpose(k_array))
factorial_k = scispec.gamma(k_array + np.ones_like(k_array))
factorial_term = - np.sum(np.log(factorial_k)) # summation of logs = log of product
log_p_likelihood = exp_term + product_term + factorial_term
log_p_likelihood_convex = -1 * log_p_likelihood
return log_p_likelihood_convex
def log_poisson_likelihood(lambda_array, k_array):
"""
Takes in the intensity array (lambda) and the observations array
:param lambda_array: Assumed, actual intensity array
:param k_array: observations array
:return: log of the combined poisson distributions
"""
exp_term = -1 * np.sum(lambda_array)
product_term_array = fn.log_special(lambda_array) * k_array
product_term = np.sum(product_term_array)
factorial_k = scispec.gamma(k_array + np.ones_like(k_array)) # Gamma(k+1) = k!
factorial_term = - np.sum(np.log(factorial_k)) # summation of logs = log of product
log_p_likelihood = exp_term + product_term + factorial_term
# Note this will be a negative value due to the very small likelihood
return log_p_likelihood
# ------------------------------------------Start of Data Collection
# Aedes Occurrences in Brazil
aedes_df = pd.read_csv('Aedes_PP_Data.csv') # generates dataframe from csv - zika data
# Setting boolean variables required for the data
brazil = aedes_df['COUNTRY'] == "Brazil"
taiwan = aedes_df['COUNTRY'] == "Taiwan"
aegyp = aedes_df['VECTOR'] == "Aedes aegypti"
albop = aedes_df['VECTOR'] == "Aedes albopictus"
year_2014 = aedes_df['YEAR'] == "2014"
year_2013 = aedes_df['YEAR'] == "2013"
year_2012 = aedes_df['YEAR'] == "2012"
# Extract data for Brazil and make sure to convert data type to float64
aedes_brazil = aedes_df[brazil] # Extracting Brazil Data
aedes_brazil_2014 = aedes_df[brazil & year_2014]
aedes_brazil_2013 = aedes_df[brazil & year_2013]
aedes_brazil_2012 = aedes_df[brazil & year_2012]
aedes_brazil_2013_2014 = aedes_brazil_2013 & aedes_brazil_2014
x_2014 = aedes_brazil_2014.values[:, 5].astype('float64')
y_2014 = aedes_brazil_2014.values[:, 4].astype('float64')
x_2013 = aedes_brazil_2013.values[:, 5].astype('float64')
y_2013 = aedes_brazil_2013.values[:, 4].astype('float64')
x_2013_2014 = aedes_brazil_2013_2014.values[:, 5].astype('float64')
y_2013_2014 = aedes_brazil_2013_2014.values[:, 4].astype('float64')
# ------------------------------------------End of Data Collection
# ------------------------------------------Start of Selective Binning
# *** Decide on the year to consider ***
# Change_Param
year = 2014
if year == 2013:
y_values, x_values = y_2013, x_2013
elif year == 2014:
y_values, x_values = y_2014, x_2014
else:
y_values, x_values = y_2013_2014, x_2013_2014 # Have to check this out! ***
# Define Regression Space by specifying intervals and creating boolean variables for filter
# Note this is for 2014
maximum_x = -32.43
minimum_x = -72.79
maximum_y = 4.72
minimum_y = -32.21
# To allow for selection of range for regression, ignoring the presence of all other data points
x_upper = -43
x_lower = -63
y_upper = -2
y_lower = -22
x_window = (x_values > x_lower) & (x_values < x_upper)
y_window = (y_values > y_lower) & (y_values < y_upper)
x_within_window = x_values[x_window & y_window]
y_within_window = y_values[x_window & y_window]
print('Number of scatter points = ', x_within_window.shape)
print('Number of scatter points = ', y_within_window.shape)
# First conduct a regression on the 2014 data set
quads_on_side = 20 # define the number of quads along each dimension
# histo, x_edges, y_edges = np.histogram2d(theft_x, theft_y, bins=quads_on_side) # create histogram
histo, y_edges, x_edges = np.histogram2d(y_within_window, x_within_window, bins=quads_on_side)
x_mesh, y_mesh = np.meshgrid(x_edges, y_edges) # creating mesh-grid for use
x_mesh = x_mesh[:-1, :-1] # Removing extra rows and columns due to edges
y_mesh = y_mesh[:-1, :-1]
x_quad_all = fn.row_create(x_mesh) # Creating the rows from the mesh
y_quad_all = fn.row_create(y_mesh)
# *** Centralising the coordinates to be at the centre of the quads
# Note that the quads will not be of equal length, depending on the data set
quad_length_x = (x_quad_all[-1] - x_quad_all[0]) / quads_on_side
quad_length_y = (y_quad_all[-1] - y_quad_all[0]) / quads_on_side
x_quad_all = x_quad_all + 0.5 * quad_length_x
y_quad_all = y_quad_all + 0.5 * quad_length_y
xy_quad_all = np.vstack((x_quad_all, y_quad_all)) # stacking the x and y coordinates vertically together
k_quad_all = fn.row_create(histo) # histogram array
# For graphical plotting
x_mesh_centralise_all = x_quad_all.reshape(x_mesh.shape)
y_mesh_centralise_all = y_quad_all.reshape(y_mesh.shape)
# ------------------------------------------End of Selective Binning
# ------------------------------------------Start of Zero Point Exclusion
# This is so as to account for boundaries whereby the probability of incidence is definitely zero in some areas
# of the map - such as on the sea, etc
# Plan is to exclude the points where the histogram is zero
# Create Boolean variable to identify only points with non-zero incidences
non_zero = (k_quad_all > -1)
x_quad_non_zero = x_quad_all[non_zero]
y_quad_non_zero = y_quad_all[non_zero]
k_quad_non_zero = k_quad_all[non_zero]
xy_quad_non_zero = np.vstack((x_quad_non_zero, y_quad_non_zero))
k_mesh = histo
# Another Boolean variable for the mesh shape
non_zero_mesh = (k_mesh > -1)
x_mesh_centralise_non_zero = x_mesh_centralise_all[non_zero_mesh]
y_mesh_centralise_non_zero = y_mesh_centralise_all[non_zero_mesh]
# ------------------------------------------End of Zero Point Exclusion
# ------------------------------------------Start of SELECTION FOR EXCLUSION OF ZERO POINTS
exclusion_sign = 'include' # Toggle between exclusion(1) and inclusion(0) of 'out-of-boundary' points
if exclusion_sign == 'exclude':
xy_quad = xy_quad_non_zero
x_quad = x_quad_non_zero
y_quad = y_quad_non_zero
k_quad = k_quad_non_zero
x_mesh_centralise = x_mesh_centralise_non_zero
y_mesh_centralise = y_mesh_centralise_non_zero
else:
xy_quad = xy_quad_all
x_quad = x_quad_all
y_quad = y_quad_all
k_quad = k_quad_all
x_mesh_centralise = x_mesh_centralise_all
y_mesh_centralise = y_mesh_centralise_all
# ------------------------------------------End of SELECTION FOR EXCLUSION OF ZERO POINTS
# Calculate MLE - which is actually just the mean
k_mle = np.sum(k_quad) / k_quad.size
k_mle_array = np.ones_like(k_quad) * k_mle
"""Homogeneous Poisson Distribution"""
homo_p = log_poisson_likelihood(k_mle_array, k_quad)
homo_mse = fn.mean_squared_error(k_mle_array, k_quad)
print('Homogeneous Poisson Likelihood = ', homo_p)
print('Homogeneous MSE = ', homo_mse)
"""Inhomogeneous Poisson Distribution"""
inhomo_p = log_poisson_likelihood(k_quad, k_quad)
inhomo_mse = fn.mean_squared_error(k_quad, k_quad)
print('Inhomogeneous Poisson Likelihood = ', inhomo_p)
print('Homogeneous MSE = ', inhomo_mse)