<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Retro hotspotting
Step1:  Optimising the bandwidth
Originally, I noticed that these results were not reproducible.  This was caused by converting from a "continuous" prediction to a grid based prediction, where we carry out a monte carlo integration step (sample the kernel at some smallish number of points in each grid cell, and average).  Apparently this introduces enough noise that you get quite different results.
We now check that running twice gives the same result (because we now sample to a sub-grid).
Result is that it doesn't really seem to matter
Step2:  Grid based algorithm
150m seems to win.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import matplotlib.pyplot as plt
import open_cp.scripted
import open_cp.scripted.analysis as analysis
loaded = open_cp.scripted.Loader("retro_preds.pic.xz")
times = [x[1] for x in loaded]
preds = [x[2] for x in loaded]
fig, axes = plt.subplots(ncols=2, figsize=(16,7))
for ax, i in zip(axes, [0, 60]):
    analysis.plot_prediction(loaded, preds[i], ax)
    ax.set_title(times[i])
betas = analysis.hit_counts_to_beta("retro.csv")
fig, ax = plt.subplots(figsize=(12,8))
analysis.plot_betas(betas, ax)
fig, ax = plt.subplots(figsize=(12,8))
analysis.plot_betas(betas, ax, range(1,21))
Explanation: Retro hotspotting
End of explanation
all_betas = analysis.hit_counts_to_beta("retro_opt.csv")
all_betas1 = analysis.hit_counts_to_beta("retro_opt1.csv")
sample = [ "RetroHotspotCtsProvider(Weight=Quartic(bandwidth={}))".format(x)
         for x in [50, 100, 150, 200, 250, 300] ]
betas = {k:all_betas[k] for k in sample}
betas1 = {k:all_betas1[k] for k in sample}
fig, axes = plt.subplots(ncols=2, figsize=(16,7))
analysis.plot_betas(betas, axes[0], range(1,21))
analysis.plot_betas(betas, axes[1], range(1,21), plot_sds=False)
#for ax in axes:
#    ax.legend([])
fig, axes = plt.subplots(ncols=2, figsize=(16,7))
analysis.plot_betas(betas1, axes[0], range(1,21))
analysis.plot_betas(betas1, axes[1], range(1,21), plot_sds=False)
#for ax in axes:
#    ax.legend([])
Explanation: Optimising the bandwidth
Originally, I noticed that these results were not reproducible.  This was caused by converting from a "continuous" prediction to a grid based prediction, where we carry out a monte carlo integration step (sample the kernel at some smallish number of points in each grid cell, and average).  Apparently this introduces enough noise that you get quite different results.
We now check that running twice gives the same result (because we now sample to a sub-grid).
Result is that it doesn't really seem to matter: 50m seems fine.
End of explanation
all_betas = analysis.hit_counts_to_beta("retro_grid_opt.csv")
sample = [ "RetroHotspotProvider(Weight=Quartic(bandwidth={}))".format(x)
         for x in [50, 100, 150, 200, 250, 300] ]
betas = {k:all_betas[k] for k in sample}
fig, axes = plt.subplots(ncols=2, figsize=(16,7))
analysis.plot_betas(betas, axes[0], range(1,21))
analysis.plot_betas(betas, axes[1], range(1,21), plot_sds=False)
#for ax in axes:
#    ax.legend([])
import importlib
importlib.reload(analysis)
fig, ax = plt.subplots(figsize=(10,6))
normed = analysis.plot_betas_means_against_max(all_betas, ax, range(1,21))
#ax.legend()
None
{key for key in normed if all(x>=0.95 for x in normed[key])}
{key : min(normed[key]) for key in normed}
import re
import numpy as np
def label(x):
    m = re.search("width=(\d+)", x)
    return int(m.group(1))
avg = {label(key) : np.mean(normed[key]) for key in normed}
fig, ax = plt.subplots(figsize=(8,6))
x = np.sort(list(avg))
y = [avg[t] for t in x]
ax.plot(x, y)
avg
r = normed
highlight = {140, 150}
fig, ax = plt.subplots(figsize=(10,6))
x = list(range(1, 21))
for k in r:
    if label(k) not in highlight:
        ax.plot(x, r[k], color="black", label="_")
    else:
        ax.plot(x, r[k], label=label(k))
ax.legend()
None
Explanation: Grid based algorithm
150m seems to win.
End of explanation
<END_TASK>