A Tree? Multi-dimensional Array? hmm

Oh, I think there is a unlucky coincidence in the example that the i th transaction happens in the i th column of the matrices…

With the approach of the example, in each step the transaction matrix transactions[1] can represent any kind of transfer from any currency to any other currency (all at once). If I write Ai = transactions[i] then the entry Ai[n,m] represents the amount of transfer (in percentage) from currency m to currency n.
(But there could be another transfer in the other direction at the same time, which might balance out.)

These Ai matrices could be seen as adjacency matrices of the graph representing the flow of money in the i th step.

You can get the transactions between the initial and the final state via

total_transaction = prod( transactions )   # EDIT: is should be prod(reverse(transactions))

here, the indices total_transaction[i,j] again indicate the transfer from currency j to currency i.

In the given example

prod(transactions)[1:3,1:3]                 # EDIT: it should be reverse(transactions)...
3×3 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
   0.0  0.0     ⋅ 
 146.0  0.5     ⋅ 
    ⋅   0.025  1.0

and if you want to know how an initial amount of money propagates, you can use total_transactions * init_x and similar, the j th column gives you where the j th currency went too.

(I know, it’s a bit the opposite way of indices compared to what one would expect. Otherwise you have to transpose the matrix or use row-vectors from the left side, instead of the matrix-vector produce.)


EDIT: by the way, you can also use normal matrices instead of sparse ones. I think sparse onces are only needed for very large problems.
Or, if you know that in each step only one currency is converted, you can just just (idx = 2, targets = [0.3,0.2,0.5]). Many possibilities.

1 Like