# Time masking for a Gaussian Random Walk

**URL:** <https://discourse.pymc.io/t/time-masking-for-a-gaussian-random-walk/11574>\
**Category:** v5\
**Tags:** modeling\
**Created:** [March 9, 2023, 10:58pm UTC](https://discourse.pymc.io/t/time-masking-for-a-gaussian-random-walk/11574 "2023-03-09T22:58:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lionelchg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lionelchg/32/6224_2.png) [@lionelchg](https://discourse.pymc.io/u/lionelchg)\
**Post date:** [March 9, 2023, 10:58pm UTC](https://discourse.pymc.io/t/time-masking-for-a-gaussian-random-walk/11574/1 "2023-03-09T22:58:40Z")

</div>

Hello !

I am having trouble masking a gaussian random walk. Let’s say I have four different items that vary over time according to a random walk. We assume that the time is discretized such that T=[0, 1, 2, …, 99]. If I have data available for all the items I create a gaussian random walk with a matrix of gaussians on which I apply a cumulative sum:

```auto
err = pm.Normal(
    "err",
    mu=0,
    sigma=sigma,
    dims=("time", "items"),
)

err = err.reshape([100, 4])

# ----------------------
# Baseline implementation
# ----------------------
cumulated_err = at.cumsum(err, axis=0)
cumulated_err = cumulated_err.reshape([100, 4])

```

Now let’s assume that for item 1 I do not have data until time 10 such that I need to mask the first 10 instants of this item. I tried the following but I get divergences (block with Masking option):

```auto
err = pm.Normal(
    "err",
    mu=0,
    sigma=sigma,
    dims=("time", "items"),
)

err = err.reshape([100, 4])

# -------------------
# Masking option
# -------------------
# Manual definition of cumulated err to take into account
# missing temporal data of some items
items_range = {
    0: [10, 100],
    1: [0, 100],
    2: [0, 100],
    3: [0, 100],
}
list_cum_err = list()
for i in range(4):
    start, end = items_range[i]
    tmp_cum_err = pm.math.concatenate(
        [
            at.zeros(start),
            at.cumsum(err[start:end, 0, i], axis=0),
            at.zeros(100 - end),
        ], axis=0)
    list_cum_err.append(tmp_cum_err)
cumulated_err = pm.math.concatenate(list_cum_err, axis=0)
cumulated_err = cumulated_err.reshape([100, 4])

```

And setting the items range to the full range does not give me the same behaviour as the reference implementation as I also have divergences:

```auto
items_range = {
    0: [0, 100],
    1: [0, 100],
    2: [0, 100],
    3: [0, 100],
}

```

Any ideas as to why my “manual” concatenation does not behave like the cumsum function :)?  
Any help would be appreciated!
