segmentby

Syntax

segmentby(func, funcArgs, segment)

Arguments

func is a function.

funcArgs are the parameters of func. It is a tuple if there are more than 1 parameter of func.

segment is the grouping variable.

segment and each of the function argument in funcArgs are vectors of the same size.

Details

segmentby is very similar to contextby except for how groups are determined. With contextby, a group includes all elements with the same value. With segmentby, only a block of equal value elements counts as a group. 2 blocks of equal value elements separated by different values are treated as 2 groups.

Examples

$ ret = 0.01 -0.02 0.03 -0.04 0.03 -0.02 0.05 -0.01 0.03 -0.04 0.05 -0.04
$ position = 1 1 1 1 -1 -1 -1 -1 1 1 1 1
$ t = table(ret, position);
$ t;

ret

position

0.01

-0.02

1

0.03

1

-0.04

1

0.03

-1

-0.02

-1

0.05

-1

-0.01

-1

0.03

1

-0.04

1

0.05

1

-0.04

1

$ update t set cumret=contextby(cumsum, ret, position);
$ t;

ret

position

cumret

0.01

1

0.01

-0.02

1

-0.01

0.03

1

0.02

-0.04

1

-0.02

0.03

-1

0.03

-0.02

-1

0.01

0.05

-1

0.06

-0.01

-1

0.05

0.03

1

0.01

-0.04

1

-0.03

0.05

1

0.02

-0.04

1

-0.02

$ update t set cumret=segmentby(cumsum, ret, position);
$ t;

ret

position

cumret

0.01

1

0.01

-0.02

1

-0.01

0.03

1

0.02

-0.04

1

-0.02

0.03

-1

0.03

-0.02

-1

0.01

0.05

-1

0.06

-0.01

-1

0.05

0.03

1

0.03

-0.04

1

-0.01

0.05

1

0.04

-0.04

1

0