Module providing spatial methods for processes utilizing spatial information.
More...
#include <spatial_module.hpp>
|
| | SpatialModule (const Data &data_, const Eigen::MatrixXi W_, double spatial_coeff, const Eigen::VectorXi *old_alloc_provider=nullptr, const std::unordered_map< int, std::vector< int > > *old_cluster_members_provider_=nullptr) |
| | Constructs a SpatialModule with parameter and data references.
|
| Eigen::VectorXd | compute_similarity_obs (int obs_idx) const override |
| | Counts neighbors of an observation grouped by cluster membership.
|
| double | compute_similarity_cls (int cls_idx, bool old_allo=false) const override |
| | Counts internal edges within a cluster.
|
| double | compute_similarity_obs (int obs_idx, int cls_idx) const override |
| | Counts neighbors of an observation within a specific cluster.
|
| | Module (const Eigen::VectorXi *old_allocations_provider_=nullptr, const std::unordered_map< int, std::vector< int > > *old_cluster_members_provider_=nullptr) |
| void | set_old_allocations_provider (const Eigen::VectorXi *provider) |
| void | set_old_cluster_members_provider (const std::unordered_map< int, std::vector< int > > *provider) |
| virtual | ~Module ()=default |
|
| std::vector< std::vector< int > > | neighbor_cache |
| | Cache storing neighbor indices for each observation.
|
| const Data & | data_module |
| | Reference to data object with cluster assignments.
|
| const Eigen::MatrixXi | W |
| | Reference to adjacency matrix W from covariates.
|
| const double | spatial_weight = 1.0 |
| | Weighting factor for spatial similarity.
|
| const Eigen::VectorXi * | old_allocations_provider |
| | Provider function for accessing old allocation state.
|
| const std::unordered_map< int, std::vector< int > > * | old_cluster_members_provider |
| | Provider function for accessing old cluster members map.
|
Module providing spatial methods for processes utilizing spatial information.
This class offers utility functions to compute neighbor counts based on an adjacency matrix W, facilitating the incorporation of spatial dependencies in clustering algorithms. The neighbor relationships are cached at construction time for efficient repeated access.
◆ SpatialModule()
| SpatialModule::SpatialModule |
( |
const Data & | data_, |
|
|
const Eigen::MatrixXi | W_, |
|
|
double | spatial_coeff, |
|
|
const Eigen::VectorXi * | old_alloc_provider = nullptr, |
|
|
const std::unordered_map< int, std::vector< int > > * | old_cluster_members_provider_ = nullptr ) |
|
inline |
Constructs a SpatialModule with parameter and data references.
Initializes the neighbor cache by calling neighbor_cache_compute().
- Parameters
-
| data_ | Reference to the Data object with cluster assignments. |
| W_ | Reference to the adjacency matrix W. matrix. |
| spatial_coeff | Weighting factor for spatial similarity. |
| old_alloc_provider | function to access old allocations for split-merge. |
| old_cluster_members_provider_ | function to access old cluster members for split-merge. |
◆ compute_similarity_cls()
| double SpatialModule::compute_similarity_cls |
( |
int | cls_idx, |
|
|
bool | old_allo = false ) const |
|
overridevirtual |
Counts internal edges within a cluster.
Computes the number of edges where both endpoints belong to the specified cluster. This is calculated as: (1/2) * sum_{i,j in cluster} W(i,j). The division by 2 accounts for the symmetry of W (each edge counted twice).
- Parameters
-
| cls_idx | The index of the cluster (0 to K-1). |
| old_allo | If true, uses old allocations from old_allocations_provider; if false, uses current allocations from data_module (default: false). |
- Returns
- The total number of internal edges in the cluster.
Implements Module.
◆ compute_similarity_obs() [1/2]
| Eigen::VectorXd SpatialModule::compute_similarity_obs |
( |
int | obs_idx | ) |
const |
|
overridevirtual |
Counts neighbors of an observation grouped by cluster membership.
Returns a vector where element k contains the number of neighbors of obs_idx that belong to cluster k.
- Parameters
-
| obs_idx | The index of the observation (0 to N-1). |
- Returns
- A K-dimensional vector of neighbor counts per cluster.
Implements Module.
◆ compute_similarity_obs() [2/2]
| double SpatialModule::compute_similarity_obs |
( |
int | obs_idx, |
|
|
int | cls_idx ) const |
|
overridevirtual |
Counts neighbors of an observation within a specific cluster.
Uses the cached neighbor list to efficiently count how many neighbors of observation obs_idx belong to cluster cls_idx.
- Parameters
-
| obs_idx | The index of the observation (0 to N-1). |
| cls_idx | The index of the cluster to consider for neighbor counting. |
- Returns
- The number of neighbors for the observation in the specified cluster.
Implements Module.
◆ neighbor_cache_compute()
| void SpatialModule::neighbor_cache_compute |
( |
| ) |
|
|
protected |
Precomputes and stores neighbor indices for all observations.
This method initializes neighbor_cache by extracting non-zero entries from each row of the adjacency matrix W. Called once during construction to enable O(neighbors) instead of O(N) lookup time.
◆ data_module
| const Data& SpatialModule::data_module |
|
protected |
Reference to data object with cluster assignments.
◆ neighbor_cache
| std::vector<std::vector<int> > SpatialModule::neighbor_cache |
|
protected |
Cache storing neighbor indices for each observation.
neighbor_cache[i] contains a vector of indices j where W(i,j) == 1. This avoids repeatedly scanning the full adjacency matrix during MCMC sampling.
◆ spatial_weight
| const double SpatialModule::spatial_weight = 1.0 |
|
protected |
Weighting factor for spatial similarity.
| const Eigen::MatrixXi SpatialModule::W |
|
protected |
Reference to adjacency matrix W from covariates.
The documentation for this class was generated from the following files: