Skip to content

Commit afa5dd0

Browse files
authored
Merge pull request #83 from SpatLyu/dev
remove leading lagged NA/NaN values for surd ts defination
2 parents b166ed4 + e9789c4 commit afa5dd0

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# infoxtr 0.3
22

3+
### breaking changes
4+
5+
* Remove leading lag-induced NA values in `surd` time-series implementation (#83).
6+
37
# infoxtr 0.2
48

59
### enhancements

inst/include/infoxtr/lagg.hpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************
1+
/*****************************************************************************************
22
* File: lagg.hpp
33
*
44
* Lagged aggregation utilities for matrices.
@@ -44,9 +44,21 @@
4444
* lagg(mat, lag, byrow)
4545
* Temporal lag for time series
4646
*
47+
* Parameter Constraints
48+
*
49+
* Lattice graphs
50+
* large lag values beyond the graph diameter have no additional effect.
51+
* The algorithm automatically stops when no further neighbors are reachable.
52+
*
53+
* Regular grids
54+
* lag must not exceed the maximum grid dimension
55+
*
56+
* Time series
57+
* lag must be smaller than the number of observations
58+
*
4759
* Author: Wenbo Lyu (Github: @SpatLyu)
4860
* License: GPL-3
49-
*******************************************************************/
61+
*****************************************************************************************/
5062

5163
#ifndef INFOXTR_LAGG_HPP
5264
#define INFOXTR_LAGG_HPP
@@ -55,7 +67,6 @@
5567
#include <cmath>
5668
#include <limits>
5769
#include <algorithm>
58-
#include <unordered_set>
5970
#include <stdexcept>
6071
#include <cstddef>
6172

@@ -89,6 +100,9 @@ namespace lagg
89100
bool byrow = true
90101
)
91102
{
103+
if (mat.empty())
104+
throw std::invalid_argument("Input matrix is empty.");
105+
92106
const size_t n = nb.size();
93107
const size_t p = mat.front().size();
94108

@@ -182,7 +196,10 @@ namespace lagg
182196
size_t lag = 1,
183197
bool byrow = true
184198
)
185-
{
199+
{
200+
if (mat.empty())
201+
throw std::invalid_argument("Input matrix is empty.");
202+
186203
const size_t N = mat.size();
187204
const size_t p = mat.front().size();
188205

@@ -191,6 +208,9 @@ namespace lagg
191208

192209
const size_t ncols = N / nrows;
193210

211+
if (lag > std::max(nrows, ncols))
212+
throw std::invalid_argument("lag too large for grid dimensions.");
213+
194214
Matrix out;
195215
if (byrow)
196216
out.assign(N, Vector(p, NaN));
@@ -275,9 +295,15 @@ namespace lagg
275295
size_t lag = 1,
276296
bool byrow = true
277297
)
278-
{
298+
{
299+
if (mat.empty())
300+
throw std::invalid_argument("Input matrix is empty.");
301+
279302
const size_t n = mat.size();
280303
const size_t p = mat.front().size();
304+
305+
if (lag >= n)
306+
throw std::invalid_argument("lag must be smaller than number of observations.");
281307

282308
Matrix out;
283309
if (byrow)

inst/include/infoxtr/transferentropy.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace transferentropy
156156
size_t ag_lag = lag_single ? ag.size() : ag.size() * lag_q;
157157
size_t N = n_obs - t0;
158158
DiscMat pm(tg.size() + ag_lag + tg_lag,
159-
std::vector<uint64_t>(N,0));
159+
std::vector<uint64_t>(N, 0));
160160

161161
// Y_present
162162
for (size_t i = 0; i < tg.size(); ++i)

src/SURD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ Rcpp::List RcppSURD(const Rcpp::NumericMatrix& mat,
189189
);
190190
}
191191

192+
// Remove leading lagged NA (time series only)
193+
if (nb.isNull() && nrows.isNull())
194+
{
195+
size_t lag_abs = static_cast<size_t>(std::abs(lag));
196+
197+
if (lag_abs > 0 && lag_abs < n_obs)
198+
{
199+
for (auto& vec : pm)
200+
{
201+
vec.erase(vec.begin(), vec.begin() + lag_abs);
202+
}
203+
}
204+
}
205+
192206
infoxtr::surd::SURDRes res = infoxtr::surd::surd(
193207
pm,
194208
static_cast<size_t>(std::abs(max_order)),

0 commit comments

Comments
 (0)