# Find maximum theoretical join count (spatial statistics)

In addition to calculating the joint count statistic, I would like to compare the actual count of joins to the theoretical maximum count of joins. I know how to compute the statistic and find the actual count of joins for a given group (under conditions of two group), but how can I find the theoretical maximum given the known count of members in each group? I would preferably solve this using Python or PostGIS. I am currently using the Pysal module to calculate the joint count statistics. The input data are census tract polygon shapefiles with arbitrary boundaries, making it impossible to simply agglomerate from east to west or north to south, etc.

EDIT: I am asking about the maximum possible count of within-group joins.

the theoretical maximum of a join is given by the Cartesian product of the sets. That's literally what `CROSS JOIN` does it gives you the maximum-join which you reduce later.

``````SELECT *
FROM generate_series(1,3) AS gs1(x)
CROSS JOIN generate_series(17,23) AS gs2(y);
x | y
---+----
1 | 17
1 | 18
1 | 19
1 | 20
1 | 21
1 | 22
1 | 23
2 | 17
2 | 18
2 | 19
2 | 20
2 | 21
2 | 22
2 | 23
3 | 17
3 | 18
3 | 19
3 | 20
3 | 21
3 | 22
3 | 23
(21 rows)
``````
• Thank you. I am asking about joins based on spatial relationships. Each item can only be joined to spatial neighbors, not to each element in the set. Elements with value '1' in col 'x' in your example would be restricted to joining elements based on spatial adjacency. Perhaps I misunderstand the application of your example to solving this problem. – eric s Sep 12 '17 at 18:31
• So you reduce the set by `WHERE ST_Touches(x,y)` or the like. The theoretical maximum is every element touches every other element. If the real world/spacetime or whatever prohibits that you just have to reduce from that set. The cross join is the theoretical maximum. The second things have real world constraints you're outside of that. – Evan Carroll Sep 12 '17 at 18:34
• I don't think this will get the result I am after. The theoretical maximum is indeed limited by real-world spatial relationships. If I have a county with n ZIP codes and k of them are in group j, I want to know what the maximum possible number of joins possible would if k number of ZIP codes were reassigned to group j so as to maximize the join count. This likely requires an iterative solving algorithm, not a simple table query. – eric s Sep 13 '17 at 19:45
• What do you think the word "theoretical" means vs "real world." I have no idea how you're assigning your variables there `k` is being used in two contexts. – Evan Carroll Sep 13 '17 at 21:31
• "Hypothetical maximum" would perhaps be more accurate. I used "theoretical" to contrast with "actual." I asked in the context of applying a particular spatial statistic where I thought the meaning of "theoretical maximum" would be reasonably clear. – eric s Sep 13 '17 at 22:00