Quadrat Statistics

Author

Serge Rey

Introduction

We now turn to the methods that are used to link observed point patterns back to the process that generated the pattern.

More specifically, the challenge that we face is as follows. Given an observed point pattern, we wish to make inferences about the process that generated the observed pattern.

The general approach that is used is to construct measures that characterise the observed point pattern, and then compare these against the proporties of the theoretical process models we explored previously.

For example, if we assume that the underlying process is CSR, we know what kinds of properties the empirical patterns from such a process should exhibit. The critical thing to keep in mind is that we never actually see the underlying process - we only see outcomes of the process (i.e., the pattern).

This raises a number of challenges that we will need to address later on, but for now we are going to build up an inituition of the general strategy for analyzing point patterns.

Example Patterns

To begin we are going to create two different point patterns, one from a CSR process and one from a clustered process. We will use these two patterns to introduce the different statistical methods used to analyze the patterns. Here we are in the rare circumstance in which we actually know what process generated the pattern.

CSR n=60

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
np.random.seed(12345)
n = 60
xy = np.random.rand(60,2)
df = pd.DataFrame(data=xy, columns=['x', 'y'])
sns.scatterplot(x='x', y='y', data=df);

import pointpats as pp
csr = pp.PointPattern(xy)
csr.summary()
Point Pattern
60 points
Bounding rectangle [(0.00838829794155349,0.024676210429265266), (0.9940145858999619,0.9613067360728214)]
Area of window: 0.9231676681785911
Intensity estimate for window: 64.99361066054225
          x         y
0  0.929616  0.316376
1  0.183919  0.204560
2  0.567725  0.595545
3  0.964515  0.653177
4  0.748907  0.653570
w = pp.Window([(0,0), (0,1), (1,1), (1,0), (0,0)])
draw = pp.PoissonClusterPointProcess(w, n, 2, 0.05, 1, asPP=True, conditioning=False)
draw.realizations[0].plot(window=True, title='Contagion Point Process (2 parents)')

clustered = draw.realizations[0]
clustered.summary()
Point Pattern
60 points
Bounding rectangle [(0.47331760265312733,0.023178703349462502), (0.9696584457277277,0.6150208352748628)]
Area of window: 1.0
Intensity estimate for window: 60.0
          x         y
0  0.513060  0.541971
1  0.473318  0.578385
2  0.508373  0.536200
3  0.881716  0.060328
4  0.894221  0.059273

Quadrat Statistics

import pointpats.quadrat_statistics as qs
csr_qr = qs.QStatistic(csr, shape='rectangle', nx=3, ny=3)
csr_qr.plot()

csr_qr.chi2
np.float64(10.8)
csr_qr.chi2_pvalue
np.float64(0.21329101843394052)
clustered_qr = qs.QStatistic(clustered, shape='rectangle', nx=3, ny=3)
clustered_qr.plot()

clustered_qr.chi2
np.float64(209.99999999999994)
clustered_qr.chi2_pvalue
np.float64(4.976940117448032e-41)

Quadrat Shape

csr_qr_hex = qs.QStatistic(csr, shape='hexagon', lh=0.2)
csr_qr_hex.plot()

csr_qr_hex.chi2
np.float64(20.733333333333334)
csr_qr_hex.df
13
csr_qr_hex.chi2_pvalue
np.float64(0.07837318677184964)
clustered_qr_hex = qs.QStatistic(clustered, shape='hexagon', lh=0.2)
clustered_qr_hex.plot()

clustered_qr_hex.chi2_pvalue
np.float64(2.005280432194402e-14)

Simulation Inference

from pointpats import PoissonPointProcess as poissonpp
clustered_sim = poissonpp(clustered.window, clustered.n, 999, asPP=True)
clustered_sim_hex = qs.QStatistic(clustered, shape='hexagon',
                                  lh=0.2,
                                  realizations=clustered_sim)
clustered_sim_hex.chi2_r_pvalue
np.float64(0.001)
clustered_sim_hex.chi2_realizations
array([26.8       , 18.86666667, 20.26666667, 19.8       , 15.6       ,
       30.53333333, 15.6       , 28.2       , 23.53333333, 17.        ,
       35.66666667, 11.4       , 19.33333333, 21.66666667, 18.4       ,
       15.6       , 17.46666667, 24.93333333, 17.        , 20.73333333,
       19.33333333, 15.6       , 38.        , 38.        , 16.06666667,
       24.46666667, 21.2       , 21.2       ,  7.66666667, 18.86666667,
       17.46666667, 24.93333333, 15.6       , 25.4       , 14.66666667,
       26.8       , 21.2       , 18.86666667, 14.66666667, 24.        ,
       11.86666667, 10.        , 17.        , 17.        ,  9.06666667,
       31.93333333, 24.93333333, 31.        , 21.2       , 17.93333333,
       11.4       , 17.        , 21.2       , 11.86666667, 21.2       ,
       19.33333333, 19.8       , 17.        , 38.        , 31.        ,
       34.73333333, 16.53333333, 33.8       , 17.93333333, 18.86666667,
       10.        ,  9.53333333, 15.6       , 25.4       , 14.66666667,
       24.46666667, 24.93333333, 18.4       , 14.2       , 24.        ,
       21.2       , 23.06666667, 21.2       , 19.33333333, 37.06666667,
       15.6       , 29.6       , 31.        , 20.26666667, 23.06666667,
       20.73333333, 32.86666667, 19.33333333, 22.6       , 15.6       ,
       13.26666667, 46.4       , 15.6       , 20.26666667, 33.33333333,
       11.4       , 17.        , 26.33333333, 17.93333333, 22.13333333,
       17.        , 33.33333333, 24.93333333, 12.33333333, 28.66666667,
       42.2       , 17.        , 22.6       , 16.53333333, 22.6       ,
       12.33333333, 15.6       , 18.4       , 17.        , 17.46666667,
       30.06666667, 26.8       , 17.        , 24.46666667, 21.66666667,
       21.66666667, 20.26666667, 17.93333333, 18.4       , 28.2       ,
       29.13333333, 14.2       , 26.33333333,  9.06666667, 20.26666667,
       21.66666667, 27.26666667, 22.6       , 22.13333333, 21.66666667,
       21.2       , 10.93333333, 21.66666667, 22.13333333, 33.33333333,
       11.4       , 15.13333333, 22.6       , 17.93333333, 18.86666667,
       22.13333333, 17.        , 10.93333333, 16.53333333, 14.2       ,
       19.33333333, 14.66666667, 16.53333333, 16.06666667, 15.13333333,
       36.6       , 10.        , 14.66666667, 17.46666667, 12.33333333,
       45.        , 26.33333333, 16.06666667, 21.2       , 32.4       ,
       22.13333333, 23.53333333, 37.06666667, 22.13333333, 28.2       ,
       27.73333333, 12.8       , 20.73333333, 18.4       , 16.06666667,
       24.93333333, 11.4       , 20.73333333, 21.2       , 17.        ,
       24.        , 13.73333333, 37.53333333, 18.86666667, 15.13333333,
       15.13333333, 27.73333333, 16.06666667, 32.4       , 20.73333333,
       33.8       , 24.46666667, 10.46666667, 27.73333333, 18.86666667,
       15.13333333, 21.2       , 25.86666667, 15.6       , 25.4       ,
       11.4       , 21.2       , 27.73333333, 20.73333333, 23.53333333,
       30.06666667, 17.        , 17.46666667, 19.8       , 19.33333333,
       22.6       , 13.26666667, 32.4       , 29.6       , 37.53333333,
       21.2       , 20.26666667, 17.        , 23.06666667, 26.33333333,
       26.33333333, 17.        , 39.86666667, 16.06666667, 24.93333333,
       20.26666667, 13.26666667, 20.73333333, 28.66666667, 23.53333333,
       19.33333333, 19.8       , 48.26666667, 19.8       , 16.06666667,
       26.33333333, 20.26666667, 10.46666667, 15.13333333, 16.06666667,
       23.53333333, 26.33333333, 15.6       , 23.53333333, 19.33333333,
        6.26666667, 27.26666667, 27.73333333, 17.        , 32.4       ,
       22.6       , 13.73333333, 28.66666667, 20.73333333, 17.46666667,
       20.73333333, 31.        , 22.6       , 19.33333333, 23.53333333,
       24.        , 18.4       , 16.06666667, 21.66666667, 23.53333333,
       12.8       , 31.        , 14.2       , 16.53333333,  8.6       ,
       14.66666667, 27.26666667, 17.46666667, 20.73333333, 31.93333333,
       28.2       , 24.        , 27.73333333, 24.46666667, 30.53333333,
       17.46666667, 15.13333333, 26.8       , 36.6       , 29.6       ,
       32.86666667, 11.86666667, 15.13333333, 14.2       , 19.8       ,
       24.46666667, 31.93333333, 35.66666667, 18.86666667, 13.73333333,
       14.66666667, 14.2       , 23.06666667, 21.2       , 16.53333333,
       31.93333333, 18.4       , 10.93333333, 25.4       , 21.66666667,
       21.66666667, 20.73333333, 29.6       , 26.33333333, 33.33333333,
       18.86666667, 13.73333333, 23.06666667, 20.26666667, 14.2       ,
       27.73333333, 36.6       , 16.06666667, 18.86666667, 14.2       ,
       25.4       , 27.26666667, 21.2       , 18.86666667, 24.        ,
       28.66666667, 30.06666667, 19.8       , 24.        , 17.46666667,
       24.        , 14.2       , 18.86666667, 24.        , 22.13333333,
       23.53333333, 30.53333333, 37.06666667, 22.6       , 21.66666667,
       23.53333333, 19.8       , 20.73333333, 19.8       , 21.2       ,
       25.86666667, 27.73333333, 14.2       , 30.06666667, 17.        ,
       17.        , 23.06666667, 30.06666667, 29.6       , 10.93333333,
       35.2       , 30.53333333, 17.        , 17.46666667, 41.26666667,
       21.66666667, 15.13333333, 24.46666667, 28.2       , 31.        ,
       17.46666667, 29.13333333, 19.8       , 17.93333333, 15.13333333,
       17.46666667, 25.4       , 17.93333333, 30.06666667, 16.06666667,
       27.73333333, 14.2       , 15.13333333, 20.26666667, 36.13333333,
       24.46666667, 25.4       , 25.4       , 36.13333333, 15.6       ,
       20.73333333, 23.53333333, 14.2       , 25.86666667, 18.86666667,
       25.4       , 18.4       , 23.53333333, 14.66666667, 17.        ,
       22.13333333, 23.53333333, 18.4       , 20.26666667, 28.2       ,
       20.26666667, 40.33333333, 28.2       , 37.06666667, 17.46666667,
       31.        , 20.26666667, 26.8       , 35.2       , 14.66666667,
       23.06666667, 23.06666667, 13.73333333, 35.2       , 23.53333333,
       31.        , 31.93333333, 17.46666667, 38.        , 16.53333333,
       31.46666667, 34.26666667, 22.13333333, 22.6       , 20.73333333,
       16.53333333, 17.93333333, 29.6       , 10.46666667, 27.26666667,
       18.86666667, 22.6       , 36.6       , 25.4       , 15.6       ,
       15.13333333, 18.86666667, 19.8       , 29.6       , 22.6       ,
       13.73333333, 46.86666667, 35.2       , 14.2       , 25.4       ,
       41.73333333, 25.4       , 13.73333333, 23.53333333, 26.8       ,
       25.4       , 16.06666667, 21.66666667, 37.53333333, 30.06666667,
        7.2       , 15.6       , 31.46666667, 17.        , 35.66666667,
       23.53333333, 19.33333333, 17.46666667, 24.93333333, 19.8       ,
       13.26666667, 25.4       , 17.46666667, 24.46666667, 20.73333333,
       33.33333333, 20.26666667, 26.33333333, 16.06666667, 21.2       ,
       21.2       , 56.2       , 27.26666667, 26.8       , 21.66666667,
       22.13333333,  7.66666667, 22.6       ,  8.6       , 17.93333333,
       25.86666667, 30.53333333, 28.66666667, 24.93333333, 13.73333333,
        9.53333333, 21.2       , 17.93333333, 32.4       , 24.        ,
       24.46666667, 34.73333333, 31.46666667, 23.06666667, 17.93333333,
       30.53333333, 15.13333333, 22.13333333, 19.8       , 17.        ,
       25.4       , 18.86666667, 26.33333333, 19.33333333, 32.4       ,
        9.53333333, 21.66666667, 11.86666667, 31.46666667, 30.06666667,
       24.93333333, 24.46666667, 17.46666667, 21.66666667, 19.33333333,
       23.06666667, 23.53333333, 23.53333333, 33.33333333, 22.6       ,
       18.4       , 24.        , 23.53333333, 16.53333333, 19.8       ,
       17.        , 24.93333333, 10.93333333, 30.53333333, 13.73333333,
       16.06666667, 19.33333333, 17.93333333, 26.8       , 21.2       ,
       19.8       , 40.33333333, 18.86666667, 10.46666667, 23.53333333,
       16.53333333, 24.        , 21.2       , 19.33333333, 31.46666667,
       12.33333333, 10.93333333, 16.53333333, 25.4       , 33.33333333,
       33.33333333, 20.73333333, 12.8       , 20.26666667, 34.26666667,
       33.33333333, 29.13333333, 18.4       , 16.53333333, 20.73333333,
       22.13333333, 22.6       , 22.6       , 17.93333333, 10.        ,
       14.66666667, 14.66666667, 16.06666667, 16.53333333, 21.2       ,
       10.93333333, 17.46666667, 31.46666667, 14.2       , 18.86666667,
       24.        , 10.46666667, 30.06666667, 25.4       , 25.4       ,
       15.13333333, 24.93333333, 22.6       , 17.93333333, 12.8       ,
       20.73333333, 10.46666667, 12.8       , 32.86666667, 23.53333333,
       21.2       , 23.06666667, 17.        , 13.73333333, 16.53333333,
       24.46666667, 14.2       , 38.46666667, 26.33333333, 15.6       ,
       20.26666667, 31.93333333, 21.66666667, 18.86666667, 15.13333333,
       20.26666667, 31.46666667, 25.4       , 17.        , 26.8       ,
       24.46666667, 14.2       , 42.2       , 22.6       , 17.46666667,
       20.26666667, 35.66666667, 26.8       , 24.93333333, 31.46666667,
       16.53333333, 24.        , 23.06666667, 17.93333333, 31.46666667,
       30.06666667, 20.26666667, 11.2       , 23.06666667, 38.93333333,
       13.73333333, 17.93333333, 24.        , 25.4       , 25.86666667,
       24.        , 18.86666667, 22.13333333, 21.2       , 11.86666667,
       18.86666667, 14.2       , 22.6       , 32.4       , 18.4       ,
       24.93333333, 18.86666667, 24.93333333, 10.46666667, 24.93333333,
       23.06666667, 15.6       , 21.66666667, 23.53333333, 20.73333333,
       24.93333333, 25.86666667, 12.8       , 20.73333333, 25.4       ,
       19.8       , 34.73333333, 30.53333333, 33.8       , 17.        ,
       25.4       , 28.66666667, 18.4       , 30.06666667, 19.8       ,
       52.93333333, 19.33333333, 25.86666667, 19.33333333, 10.93333333,
       18.4       , 28.66666667,  7.66666667, 20.73333333, 43.13333333,
       22.6       , 24.46666667, 23.53333333, 25.4       , 12.33333333,
       26.8       , 22.6       , 22.13333333, 26.33333333, 31.        ,
       29.13333333, 18.86666667, 20.26666667, 22.13333333, 16.53333333,
       22.6       , 12.8       , 37.06666667, 23.06666667, 31.93333333,
       10.        , 16.53333333, 14.2       , 26.8       , 18.4       ,
       16.06666667, 22.6       , 13.26666667, 19.33333333, 21.66666667,
       15.6       , 16.06666667, 19.8       , 26.33333333, 21.66666667,
       25.86666667, 23.06666667, 16.53333333,  8.6       , 14.66666667,
       23.06666667, 15.13333333, 15.6       , 14.66666667, 29.6       ,
       13.73333333, 19.8       , 17.        , 19.8       , 39.86666667,
       31.        , 12.8       , 24.93333333, 31.        , 23.06666667,
       21.66666667, 21.66666667, 17.46666667, 20.73333333, 19.8       ,
       25.4       , 21.2       , 19.8       , 11.86666667, 31.        ,
       19.8       , 19.33333333, 13.26666667, 33.8       , 18.86666667,
       46.4       , 24.        , 16.53333333, 16.53333333, 31.46666667,
       23.53333333, 26.33333333, 25.86666667, 31.93333333, 21.2       ,
       21.66666667, 20.73333333, 29.6       , 24.        , 38.46666667,
        7.66666667, 17.        , 13.73333333,  6.26666667, 20.26666667,
       36.13333333, 21.66666667, 23.53333333, 25.86666667, 18.4       ,
       14.2       , 15.6       , 20.73333333, 34.73333333, 26.8       ,
       21.66666667, 24.93333333, 29.13333333, 31.93333333, 25.86666667,
       14.2       , 12.33333333, 17.46666667, 24.        , 17.        ,
       27.26666667, 17.46666667, 17.        , 34.26666667, 12.8       ,
       29.6       , 24.93333333, 15.13333333, 13.73333333, 34.26666667,
       18.86666667, 30.06666667, 19.8       , 37.53333333, 26.8       ,
       19.33333333, 17.93333333, 22.6       , 16.53333333, 12.33333333,
       20.73333333, 17.46666667, 32.4       , 10.46666667, 33.8       ,
       21.66666667, 18.86666667, 26.8       , 32.86666667, 41.26666667,
       27.73333333, 16.06666667, 24.46666667, 15.6       , 19.33333333,
       23.53333333, 19.33333333, 12.8       , 21.66666667, 12.8       ,
       24.46666667, 36.13333333, 17.        , 18.86666667, 16.06666667,
       17.93333333, 33.33333333, 10.93333333, 14.66666667, 11.4       ,
       18.4       , 31.46666667, 24.46666667, 20.73333333, 40.8       ,
       16.06666667, 20.73333333, 15.6       , 26.8       , 14.66666667,
       31.46666667, 20.26666667, 24.46666667, 11.86666667, 17.46666667,
       24.93333333,  6.73333333, 17.93333333, 24.46666667, 18.4       ,
       14.2       , 26.8       , 24.        , 40.33333333, 16.53333333,
       47.33333333, 23.53333333, 20.73333333, 14.2       , 23.53333333,
       18.86666667, 13.73333333, 31.        , 21.66666667, 22.6       ,
       11.4       , 24.46666667, 32.86666667, 43.13333333, 22.6       ,
       19.8       , 36.6       , 28.2       , 23.06666667, 29.13333333,
       15.13333333, 17.        , 28.2       , 34.26666667, 25.86666667,
       13.26666667, 32.4       ,  8.6       , 29.13333333, 26.33333333,
       28.66666667, 15.6       , 19.8       , 17.        , 12.8       ,
       23.06666667, 16.06666667, 30.06666667, 20.26666667, 19.8       ,
       22.6       , 21.66666667, 25.86666667, 36.6       , 15.6       ,
       24.46666667, 15.13333333, 19.33333333, 10.46666667, 26.33333333,
       18.86666667, 23.06666667, 24.46666667, 22.13333333, 29.6       ,
       17.46666667, 23.06666667, 16.        , 23.06666667, 28.66666667,
       20.26666667, 17.93333333, 17.        , 26.33333333, 21.66666667,
       13.73333333, 17.93333333, 16.06666667, 21.66666667, 26.8       ,
       18.86666667, 24.93333333, 21.66666667, 34.26666667, 23.06666667,
       23.06666667, 12.8       , 15.6       , 13.73333333, 35.2       ,
       26.8       , 25.4       , 19.33333333, 25.4       , 22.6       ,
       18.86666667, 23.06666667, 29.6       , 13.73333333, 13.26666667,
       30.06666667, 31.93333333, 12.8       , 33.8       , 11.4       ,
       15.13333333, 15.6       , 36.13333333, 16.06666667, 13.73333333,
       42.2       , 14.2       , 30.06666667, 24.46666667, 45.93333333,
       29.6       , 17.93333333, 29.13333333, 29.6       , 38.46666667,
       17.93333333, 19.33333333, 12.8       , 22.13333333, 17.93333333,
       19.33333333, 14.2       , 24.46666667, 14.66666667, 21.2       ,
       29.6       , 18.4       , 20.73333333, 20.26666667, 22.6       ,
       17.        , 33.33333333, 29.6       , 31.46666667])

Influence of Bounding Extent

import libpysal
from libpysal.cg import shapely_ext
import geopandas
import numpy as np
from pointpats import Window
va = libpysal.io.open(libpysal.examples.get_path("vautm17n.shp"))
polys = [shp for shp in va]
state = shapely_ext.cascaded_union(polys)
window = Window(state.parts)
np.random.seed(5)
samples = poissonpp(window, 200, 1, conditioning=False, asPP=False)
samples.realizations[0]
array([[ 414659.62831296, 4098843.62306827],
       [ 944877.32665295, 4064524.51881247],
       [ 624464.14012849, 4135522.50140932],
       [ 716322.44071544, 4158399.44765818],
       [ 831143.93926953, 4237580.13537067],
       [ 400509.95328193, 4096900.41002516],
       [ 810686.07255281, 4283013.52195582],
       [ 589382.5482221 , 4207114.63468694],
       [ 464834.86653763, 4099680.23066161],
       [ 729016.86342122, 4295967.88710233],
       [ 692558.78034025, 4104007.16031022],
       [ 707522.86530594, 4083889.38215422],
       [ 368068.7091872 , 4091422.04350303],
       [ 781939.22465452, 4097373.59625561],
       [ 690938.87384332, 4295945.27613892],
       [ 737215.08721821, 4168650.77104844],
       [ 610370.4068341 , 4121670.2849925 ],
       [ 557829.48604931, 4085935.58930553],
       [ 866763.76550655, 4111032.57384731],
       [ 834827.89687458, 4166256.04344958],
       [ 621872.14784085, 4116134.29168132],
       [ 731234.98798312, 4131270.48035054],
       [ 682313.43606906, 4223967.01711812],
       [ 733908.93745755, 4275494.4664081 ],
       [ 529604.21979282, 4140192.20505708],
       [ 667623.24339624, 4231046.01435233],
       [ 852876.94714772, 4144452.31221088],
       [ 736531.73670527, 4298186.25041488],
       [ 628191.02243473, 4102712.26100424],
       [ 695286.19214258, 4089811.49187367],
       [ 335791.24699685, 4071456.16300708],
       [ 976525.54090453, 4190170.9356442 ],
       [ 269907.7423385 , 4053819.55125621],
       [ 901005.55219154, 4135790.38279535],
       [ 343450.39084153, 4069350.54999132],
       [ 616962.39647439, 4060407.85366255],
       [ 856890.64094909, 4088171.88687401],
       [ 784246.96302866, 4158561.00287435],
       [ 506667.94859104, 4057344.67213736],
       [ 652994.7682644 , 4222285.369847  ],
       [ 752892.56374042, 4051208.83003112],
       [ 925790.59890195, 4067279.1569195 ],
       [ 979545.31057954, 4216138.61394589],
       [ 834141.62958798, 4078931.40609538],
       [ 826072.78416898, 4141338.71410948],
       [ 789540.24165872, 4134439.70394346],
       [ 832204.18186126, 4197953.71447929],
       [ 425655.57604877, 4089467.13633862],
       [ 864217.85281509, 4188693.47802216],
       [ 862328.44401525, 4076592.74052587],
       [ 867441.18732928, 4116652.71820767],
       [ 699661.2534777 , 4307801.55307259],
       [ 751829.65031639, 4319968.92400562],
       [ 693363.69978698, 4164890.1129782 ],
       [ 746506.9340749 , 4128752.07645258],
       [ 732083.67983923, 4248326.90577783],
       [ 653338.32075661, 4156598.67265619],
       [ 976606.02276802, 4197597.40929845],
       [ 848350.52448247, 4064866.02614159],
       [ 630634.31029898, 4214285.994748  ],
       [ 710542.99205501, 4142311.94640686],
       [ 818235.01391031, 4101202.27810456],
       [ 823663.1391133 , 4155899.63963564],
       [ 664444.47776103, 4236228.98363075],
       [ 360769.23263894, 4085354.33832084],
       [ 766550.94064044, 4292018.87615081],
       [ 708574.49400057, 4316489.95362822],
       [ 678464.74663622, 4157026.01869247],
       [ 669386.8193962 , 4168943.73102981],
       [ 739117.58013062, 4238904.58741115],
       [ 802008.45139164, 4058821.42801494],
       [ 719353.5399807 , 4310434.15086711],
       [ 699010.43647616, 4235885.22615002],
       [ 711302.81748723, 4189425.14715171],
       [ 711004.15283455, 4307013.13265628],
       [ 443641.20854179, 4112502.92242971],
       [ 549017.70273327, 4122109.33477415],
       [ 633880.98332554, 4180721.53522785],
       [ 761505.3618409 , 4337608.44051833],
       [ 628113.51741034, 4248999.17523924],
       [ 612085.42234894, 4193531.31241006],
       [ 773003.9526705 , 4191729.03945972],
       [ 869260.59923287, 4095817.64598321],
       [ 429695.78427436, 4083168.63837101],
       [ 411407.60297276, 4095291.64838574],
       [ 774204.90698235, 4114220.83806725],
       [ 592778.43642512, 4048923.19755613],
       [ 384376.48425827, 4088331.25454645],
       [ 947440.38984378, 4151004.37886308],
       [ 767483.69263252, 4178399.29510034],
       [ 690935.63144369, 4235036.15530389],
       [ 576980.01519074, 4070580.86201211],
       [ 712493.43738419, 4089094.16426798],
       [ 519079.03725716, 4062389.09073365],
       [ 594894.13977775, 4126627.3577412 ],
       [ 802962.14790058, 4255158.21686279],
       [ 641802.18696761, 4198983.48922481],
       [ 726954.63054571, 4344401.63706861],
       [ 821460.13878318, 4132750.73296464],
       [ 837220.98889414, 4222136.55345996],
       [ 563543.98825404, 4153613.9629347 ],
       [ 814038.74000601, 4093096.42208192],
       [ 697273.58284353, 4163467.0010079 ],
       [ 629072.6884094 , 4213295.94973238],
       [ 819817.84709794, 4102394.60580934],
       [ 526160.27728639, 4104037.23601285],
       [ 836763.95559331, 4129516.28447547],
       [ 736374.59850277, 4352644.80670343],
       [ 841168.99204432, 4138747.2216095 ],
       [ 751403.55221945, 4199922.61169574],
       [ 557689.70136621, 4058695.77127365],
       [ 878737.13093459, 4069134.69802166],
       [ 645585.19035458, 4215659.45070671],
       [ 848301.94947934, 4240704.72062012],
       [ 811185.54323789, 4246760.16720487],
       [ 802385.39914521, 4152499.83940512],
       [ 967083.70536616, 4187628.68689018],
       [ 566173.41485163, 4171863.51737594],
       [ 695607.47904146, 4288180.87390924],
       [ 677616.25847172, 4050830.91542181],
       [ 861869.68527996, 4210372.43779312],
       [ 654780.64776662, 4111656.98283347],
       [ 585233.58031201, 4074992.10261737],
       [ 781434.81304468, 4058413.05158421],
       [ 822682.84769376, 4213417.77129642],
       [ 768900.63289125, 4088226.44858065],
       [ 791899.72825801, 4245563.76587488],
       [ 831576.99118498, 4312162.65433444],
       [ 878388.72674408, 4062578.7934681 ],
       [ 823191.58958438, 4196021.45946538],
       [ 378156.77607172, 4068466.52709272],
       [ 862070.43546301, 4193252.59919724],
       [ 817515.77267352, 4251166.78629974],
       [ 690730.22340288, 4134039.36745731],
       [ 582428.37056692, 4142295.17500245],
       [ 701803.27707921, 4257761.15121192],
       [ 794450.97333767, 4082458.53574423],
       [ 683956.9175291 , 4263206.01583531],
       [ 810304.08829204, 4189035.31606198],
       [ 626831.94796862, 4183736.79813135],
       [ 447925.51227984, 4053619.31963374],
       [ 720176.25306412, 4080228.49511975],
       [ 671706.30020031, 4057219.61544144],
       [ 737091.10397116, 4186517.38432557],
       [ 621943.7767598 , 4230899.1177717 ],
       [ 781727.00521532, 4188771.47311895],
       [ 533270.55041969, 4101228.87129633],
       [ 422275.11061901, 4061412.90318906],
       [ 623274.94414034, 4137159.04830485],
       [ 613396.83788472, 4098031.58804004],
       [ 918485.80482483, 4066117.04895843],
       [ 835277.63162857, 4216987.74937433],
       [ 840905.06186863, 4119589.82676569],
       [ 776734.80434232, 4130379.65381659],
       [ 822926.36295514, 4212704.28173775],
       [ 577183.29908505, 4173970.73377266],
       [ 844670.36825708, 4169964.16925377],
       [ 726945.33864164, 4247843.41173161],
       [ 522700.79684074, 4133457.24989219],
       [ 781400.95255701, 4075836.20334854],
       [ 807851.53178381, 4155393.2584169 ],
       [ 528527.53915051, 4128711.54607999],
       [ 733715.04344501, 4324209.51951477],
       [ 761959.58353421, 4278337.10568371],
       [ 462416.02583551, 4055426.72279291],
       [ 783784.3588985 , 4219065.00137999],
       [ 656981.17357128, 4081038.36317257],
       [ 752519.5579292 , 4122287.87050136],
       [ 785998.97275375, 4180386.72063339],
       [ 515218.38308205, 4058619.31300199],
       [ 665460.74168329, 4220305.65172169],
       [ 635096.17082896, 4088479.03653079],
       [ 525676.09274115, 4094679.99674041],
       [ 652994.1969737 , 4196544.93157394],
       [ 684221.48776759, 4279076.50535586],
       [ 841203.32727666, 4091321.7151704 ],
       [ 658966.29762168, 4152432.0546778 ],
       [ 746057.91004075, 4159150.20681748],
       [ 713023.65348106, 4313845.48665105],
       [ 389367.77238998, 4131872.44048721],
       [ 964384.90536381, 4177830.49754845],
       [ 861956.74790132, 4163047.64826283],
       [ 652335.83396067, 4066521.74757694],
       [ 970927.39251655, 4191911.74445852],
       [ 723112.44351288, 4066723.92870727],
       [ 617299.64641549, 4215686.04441991],
       [ 733460.51785091, 4209610.5745427 ],
       [ 774366.73041553, 4083740.25237318],
       [ 773520.40251126, 4336421.17625672],
       [ 410898.72636993, 4079437.47953348],
       [ 673646.58115341, 4145523.97371361],
       [ 817877.62652755, 4319656.3622249 ],
       [ 592810.59894755, 4136791.36885295],
       [ 552979.14353052, 4059233.18378104],
       [ 812842.56890396, 4121182.53606018],
       [ 767558.36215433, 4227759.98274924],
       [ 844106.62271791, 4069246.81629651],
       [ 801080.39465864, 4189486.98256327],
       [ 781265.07995516, 4324246.416531  ],
       [ 628867.42811353, 4070282.90463193]])
from pointpats import PointPattern
pp_csr = PointPattern(samples.realizations[0])
pp_csr.plot()

pp_csr.plot(window=True, hull=True, title='Random Point Pattern')

csr_qr = qs.QStatistic(pp_csr, shape='rectangle', nx=3, ny=3)
csr_qr.plot()

csr_qr.chi2_pvalue
np.float64(7.295335713511762e-19)

The low p-value is an artifact of the shape of the window, since we will never see points in the top two cells in the first column. We did infact generate a CSR sample, so our test is incorrect here because of the shape of the window.

To correct for this, we can simulate other CSR samples in the same window and compare the value of our original statistic to this distribution to develop an alternative approach to inference.

np.random.seed(12345)
csr_samples = poissonpp(window, 200, 99, conditioning=False, asPP=True)
csr_samples.realizations[0]
<pointpats.pointpattern.PointPattern at 0x7c6cc3943220>
stats = []
for key in csr_samples.realizations:
    realization = csr_samples.realizations[key]
    test = qs.QStatistic(realization, shape='rectangle', nx=3, ny=3)
    stats.append(test.chi2)
stats = np.array(stats)
num = 1 + (stats >= csr_qr.chi2).sum()
den = 99 +1
p_value = num / den
p_value
np.float64(0.31)
import seaborn as sns
ax =sns.distplot(stats)
ax.axvline(x = csr_qr.chi2, ymin=0, ymax=.25, color='r')

csr_qr.chi2
np.float64(103.75000000000001)