Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative sum on a TSFrame #205

Open
chiraganand opened this issue Aug 13, 2024 · 3 comments
Open

Cumulative sum on a TSFrame #205

chiraganand opened this issue Aug 13, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@chiraganand
Copy link
Member

cumsum(ts) does not work.

julia> cumsum(ts)
ERROR: MethodError: no method matching +(::TSFrame, ::TSFrame)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at ~/.julia/juliaup/julia-1.7.3+0.x64.linux.gnu/share/julia/base/operators.jl:655
  +(::ChainRulesCore.AbstractThunk, ::Any) at ~/.julia/packages/ChainRulesCore/RbX5a/src/tangent_arithmetic.jl:122
  +(::ChainRulesCore.Tangent{P}, ::P) where P at ~/.julia/packages/ChainRulesCore/RbX5a/src/tangent_arithmetic.jl:146
  ...
Stacktrace:
  [1] add_sum(x::TSFrame, y::TSFrame)
    @ Base ./reduce.jl:24
  [2] iterate
    @ ./iterators.jl:515 [inlined]
  [3] collect_to!
    @ ./array.jl:782 [inlined]
  [4] collect_to_with_first!
    @ ./array.jl:760 [inlined]
  [5] _collect(c::UnitRange{Int64}, itr::Base.Iterators.Accumulate{typeof(Base.add_sum), TSFrame, Base._InitialValue}, #unused#::Base.EltypeUnknown, isz::Base.HasLength)
    @ Base ./array.jl:754
  [6] collect
    @ ./array.jl:649 [inlined]
  [7] #accumulate#793
    @ ./accumulate.jl:294 [inlined]
  [8] accumulate
    @ ./accumulate.jl:292 [inlined]
  [9] cumsum(itr::TSFrame)
    @ Base ./accumulate.jl:151
 [10] top-level scope
    @ REPL[38]:1
@chiraganand chiraganand added the bug Something isn't working label Aug 13, 2024
@Prajna1999
Copy link

hi @chiraganand cumsum seems not be implemented in the package (?) . I can start implementing it. Can you please assign me this task?

@chiraganand
Copy link
Member Author

@Prajna1999 You can assign the task to yourself and submit a PR. I can review it.

@Prajna1999
Copy link

Prajna1999 commented Nov 20, 2024

@chiraganand Base.cumsum for vectors only works along one dimension. So we cannot directly use ts as an Array-like argument for TSFrame object. For instance for a random TSFrame object
`

julia> ts_rand
10×3 TSFrame with Int64 Index
 Index  x1        x2        cumulative_sum_row
 Int64  Float64   Float64   Float64
───────────────────────────────────────────────
     1  0.374538  0.374538            0.374538
     2  0.873534  0.873534            1.24807
     3  0.290125  0.290125            1.5382
     4  0.336841  0.336841            1.87504
     5  0.662271  0.662271            2.53731
     6  0.776336  0.776336            3.31364
     7  0.503406  0.503406            3.81705
     8  0.750177  0.750177            4.56723
     9  0.910337  0.910337            5.47756
    10  0.464067  0.464067    

`

To cumulatively sum row wise for x2 column, you need to call

`

julia> cumsum(ts_rand.x2)
10-element Vector{Float64}:
 0.37453777969575874
 1.2480721438971558
 1.5381970305596713
 1.8750378636195264
 2.537308550691223
 3.313644735994737
 3.8170502654496943
 4.567227632902407
 5.477564954520991
 5.941632119720697
`
 To add a new column to the existing TSFrame object, you can modify the underlying dataframe directly by running
`
julia> ts_rand.coredata[!,:cumulative_sum_row_2]=cumsum(ts_rand.x2)
10-element Vector{Float64}:
 0.37453777969575874
 1.2480721438971558
 1.5381970305596713
 1.8750378636195264
 2.537308550691223
 3.313644735994737
 3.8170502654496943
 4.567227632902407
 5.477564954520991
 5.941632119720697

julia> ts_rand
10×4 TSFrame with Int64 Index
 Index  x1        x2        cumulative_sum_row  cumulative_sum_row_2
 Int64  Float64   Float64   Float64             Float64
─────────────────────────────────────────────────────────────────────
     1  0.374538  0.374538            0.374538              0.374538
     2  0.873534  0.873534            1.24807               1.24807
     3  0.290125  0.290125            1.5382                1.5382
     4  0.336841  0.336841            1.87504               1.87504
     5  0.662271  0.662271            2.53731               2.53731
     6  0.776336  0.776336            3.31364               3.31364
     7  0.503406  0.503406            3.81705               3.81705
     8  0.750177  0.750177            4.56723               4.56723
     9  0.910337  0.910337            5.47756               5.47756
    10  0.464067  0.464067            5.94163               5.94163
`

do you want a custom cumsum for TSFrame that would sum across columns? IMO that would make things difficult than a simple for loop accumulator over the underlying dataframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants