Adding rows

Hi there! I am a bit new to julia so I have a few a very basic questions:

I want to sum 33 rows of an nxm matrix. How can I do so?

Hi @r.bhl, welcome to the community! Your question is really about array indexing. Have a look at the manual here (you can skip straight to the indexing section if you wish):

and let us know if it’s still unclear or if you need further assistance.

Hello @mthelm85! thank you for the reply!
The question I have to answer: sort the 30 assets according to the sum of returns over the last 33 days. Assets are given in columns and days are the rows. Data is given for 10 years, and I have to (I think) create a loop for returns.

My commands I have tried:
#R is the return which I indexed from a larger database.

m = 1
n = R[1:30]
for t = [1:33][1]
local n
m = m + t
n = t
R2 = sum(R[m])

Another command I tried:

Rsum = sum(R,dims=1)

How could I go about this?

You should always quote your code here with backticks ```, like this:

m = 1
n = R[1:30]
for t = [1:33][1]
local n
m = m + t
n = t
R2 = sum(R[m])

It makes it much easier to read :grinning: Have a look at this thread when you get a chance.

I’m not sure I’m following but if you have some returns data in a Matrix R and then the names of the assets in a vector assets, you can do this:

# Create fake data
R = rand(Float64, (50,30))
assets = ["company$i" for i in 1:30]

rets = [(company=i[2], ret=sum(i[1][1:33])) for i in zip(eachcol(R), assets)]

julia> sorted_assets = [sort(rets, by=last, rev=true)[i][1] for i in 1:30]
30-element Array{String,1}:

That being said, I would probably use DataFrames.jl to do this kind of work. The documentation for that package is good and it will probably allow you to get up and running with these kinds of operations faster than if you just use base Julia.


Thank you so much @mthelm85. Just a small doubt- the array we get of the 30 element string is a sorted function of their summed returns? If not, how could I rank these assets on the basis of their sum?

I can’t thank you enough. I am new to this language and amidst covid it’s become even tougher to seek guidance.

1 Like

Anytime! You’ll find that the community here is very helpful and on numerous occasions, I’ve been amazed at how much time & effort folks are willing to put in to help me out (so I do my best to pay it forward :wink:). If you can provide a minimum working example (MWE), you’ll nearly always receive help that will allow you to solve your problem :slightly_smiling_face:.

In the example above, if I just wanted an array of sorted returns, I would do something like this (this is much easier):

R = rand(Float64, (50,30))

julia> rets = sort([sum(col[1:33]) for col in eachcol(R)], rev=true)
30-element Array{Float64,1}:

To fully understand the above, check out the following sections of the manual:

Also, note that you can type ? in the REPL and then search for function names, types, keywords, etc. So, if you want to understand the sort, rand and eachcol functions, in your Julia REPL, just type ? and you’ll see it change from julia> to help?>. Then, you’ll see something like this:

help?> rand
search: rand randn transcode macroexpand @macroexpand @macroexpand1 CartesianIndex CartesianIndices range transpose LinRange UnitRange StepRange

  rand([rng=GLOBAL_RNG], [S], [dims...])

. # more info here