Skip to content

Commit

Permalink
Corrected Moran library model and function input consistency test
Browse files Browse the repository at this point in the history
There was a confusion in the library model between the weight matrix and
the gaml grid.
The error in the consistency test was due to being tested on the library
model.
Also added a neighbours spatial weight case. Fairly common with Moran
index and easy to test.
  • Loading branch information
ScriBanana committed Aug 18, 2023
1 parent 9b2d5ac commit a4f3aad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion msi.gama.core/src/msi/gaml/operators/Spatial.java
Expand Up @@ -7261,7 +7261,7 @@ public static double moranIndex(final IScope scope, final IList<Double> vals, fi
if (weightMatrix == null || weightMatrix.numCols != weightMatrix.numRows) throw GamaRuntimeException
.error("A squared weight matrix should be given for the moran index computation", scope);
final int N = vals.size();
if (N != weightMatrix.numRows * weightMatrix.numCols) throw GamaRuntimeException
if (N != weightMatrix.numRows) throw GamaRuntimeException
.error("The lengths of the value list and of the weight matrix do not match", scope);
double I = 0.0;
double sumWeights = 0.0;
Expand Down
Expand Up @@ -9,7 +9,7 @@ model moranIndex

global {
string grid_spatial_init <- "random" among: ["random", "checkerboard", "blocks"];
string weight_type <- "overlapping" among: ["overlapping", "distance"];
string weight_type <- "neighbors" among: ["overlapping", "neighbors", "distance"];
int grid_size <- 20;

list<float> vals;
Expand All @@ -27,30 +27,34 @@ global {
}
vals <- cell collect (each.color = #white ? 0.0 : 1.0);
weights <- 0.0 as_matrix {grid_size, grid_size};
loop i from: 0 to: grid_size -1 {
loop j from: 0 to:grid_size -1 {
if (i = j) {weights[i,j] <- 0.0;}
else {
switch weight_type {
match "overlapping" {
weights[i,j] <- (cell[i] overlaps cell[j]) ? 1.0 : 0.0;
ask cell {
switch weight_type {
match "neighbors" {
ask self.neighbors {
weights[int(self), int(myself)] <- 1.0;
}
}
match "overlapping" {
ask cell overlapping self {
if self != myself {
weights[int(self), int(myself)] <- 1.0;
}
match "distance" {
using topology(cell) {
weights[i,j] <- 1/(cell[i] distance_to cell[j]);
}
}
}
match "distance" {
ask cell {
if self != myself {
weights[int(self), int(myself)] <- 1/(self distance_to myself);
}

}

}
}
}
}
I <- moran(vals, weights);
write "moran I: " + I;
}
}
grid cell width: grid_size height: grid_size neighbors: 8;
grid cell width: grid_size height: grid_size neighbors: 4;

experiment "Moran" type: gui {
parameter "grid size: " var: grid_size min: 2 max: 100;
Expand Down

0 comments on commit a4f3aad

Please sign in to comment.