# How do I sum a tensor by progressing index on specific axis?

## Issue

I am working with time series returns and I want to find the cumulative return by time index. Assume the last index of a given time horizon should be itself, and the 1st index of a horizon should be the sum of all timesteps.

In the following example, I have a boolean mask indicating what timesteps should be added together for a given index. I just don’t know where to go from here.

``````import tensorflow as tf

# Shape: (samples, horizon, feature)
source = tf.constant([[, , , ], [, , , ]])
# Shape: (horizon, horizon)
mask = tf.linalg.band_part(tf.ones((source.shape, source.shape)), 0, -1)

# Result should be tf.constant([[, , , ], [, , , ]])
``````

## Solution

You can expand `mask` in the samples dimension, then use `tf.matmul` to get desired result

``````source = tf.constant([[, , , ], [, , , ]])
mask = tf.linalg.band_part(tf.ones((source.shape, source.shape)), 0, -1)
``````

Output

``````<tf.Tensor: shape=(2, 4, 1), dtype=int32, numpy=
array([[[ 6],
[ 6],
[ 5],
[ 3]],

[,
,
,
[ 7]]])>
``````

To do it without a mask, you can flip `source` on the horizon axis, perform a `tf.cumsum`, then flip the resultant tensor on the horizon axis again

``````ans = tf.reverse(tf.cumsum(tf.reverse(source, axis=), axis=1), axis=)
``````

Output

``````<tf.Tensor: shape=(2, 4, 1), dtype=int32, numpy=
array([[[ 6],
[ 6],
[ 5],
[ 3]],

[,
,
,
[ 7]]])>
`````` 