stan-dev / stan-dev/stan

generate matrix for NNGP

Open
#2,222 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
2.8k
Forks
388
Avg merge
2d 17h
Merged PRs (30d)
15

Description

Summary:

A function for generating sparse distance matrix and the index of its' nonzero entities to be used in the calculation of the log likelihood of Nearest-Neighbor Gaussian Process.

`#ifndef GET_NN_DATA_HPP
#define GET_NN_DATA_HPP

#include
#include
#include
#include <Eigen/Dense>
#include "get_dist_v.hpp"
#include "getNNIndx.hpp"
#include <stan/math/prim/mat.hpp>

using std::vector;
using Eigen::Matrix;
using Eigen::Dynamic;
using Eigen::RowVectorXd;
template <typename T, int neighborNum>
void
get_NN_data(const Matrix<T, Dynamic, Dynamic>& sorted_coords,
vector& NN_indM, vector& NN_distv,
vector& NN_dist_M){

/**
 *  Generate sparse distance matrix and the index of
 *  its' nonzero entities to be used in the calculation of
 *  the log likelihood of Nearest-Neighbor Gaussian Process.
 *  
 *  The workspace is allocated base on the number of Nearest 
 *  Neighbors and the number of locations
 *
 *  neighborNum:    int     number of nearest neighbors
 *  sorted_coords:  matrix  coordinates with sorted first
 *  NN_indM:        vector  index of nearest neighbors
 *  NN_distv:       vector  distances among obs and 
 *  its' neighbors
 *  NN_dist_M:      vector  distance matrix of neighbors
 *
 *
 **/

int l1 = static_cast<int>
(static_cast<double>(neighborNum) / 2 * (neighborNum - 1)+
 (sorted_coords.rows() - neighborNum) * neighborNum);

NN_indM.resize(l1);
NN_distv.resize(l1);

int l2 = static_cast<int>
(static_cast<double>(neighborNum - 2) * (neighborNum - 1) *
 (neighborNum) / 6 + (sorted_coords.rows() - neighborNum) *
 static_cast<double>(neighborNum ) / 2 * (neighborNum - 1));

NN_dist_M.resize(l2);

int iNN, iNNIndx, iNNMIndx;

for (int i = 0; i < sorted_coords.rows(); i++){
    
    vector<double> dist_v(i);
    
    dist_v = get_dist_v<double>(sorted_coords, i);
    
    vector<int> sortind = stan::math::sort_indices_asc(dist_v);
    getNNIndx(i, neighborNum, iNNIndx, iNN, iNNMIndx);
    
    for (int j = 0; j < iNN; j++){
        NN_indM[j + iNNIndx] = sortind[j] - 1;
        NN_distv[j + iNNIndx] = dist_v[sortind[j] - 1];
    }
    
    int k = 0;
    
    for (int j = 0; j < (iNN - 1); j++){
        for (int h = j + 1; h < iNN; h++){
            RowVectorXd loc1 = sorted_coords.row(NN_indM[j + iNNIndx]);
            RowVectorXd loc2 = sorted_coords.row(NN_indM[h + iNNIndx]);
            NN_dist_M[iNNMIndx + k] = stan::math::squared_distance(loc1, loc2);
            k++;
        }
    }
}
return;

}
#endif
`

steps:
  1. Allocate the workspace for NN_indM, NN_distv and NN_dist_M
  2. In the for-loop, get_dist_v saves the distance vector between ith obs and all obs with order lower than i, getNNIndx find the index of the starting point for saving data.
Here is the test code

vector<int> NN_indM(0); vector<double> NN_distv(0); vector<double> NN_dist_M(0); get_NN_data<double, 8>(sorted_coords, NN_indM, NN_distv, NN_dist_M);

Results:

Can get results in C++.
No sure how to test it with stan.
Haven't done the test unit.

Current Version:

v2.14.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing get_dist_v and getNNIndx implementations and the Stan-facing entry point for this proposed function. Reproduce the provided C++ example, then add a unit test covering NN_indM, NN_distv, and NN_dist_M and confirm the function can be tested through Stan.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.