Forum Discussion
Lambda Example: Generate Fibonacci series
Hi Lori
I remember MMULT with the upper triangular matrix as being remarkably effective even for quite large problems. I just chickened out at having to explain the method to a non-mathematically inclined financial modeller as the answer to their corkscrew prayers!
The formulae in my spreadsheet are worse than the REDUCE/VSTACK row-based solutions you mention. I have taken a vertical slice through the data array and accumulated all four rows simultaneously. That requires 1000 applications of HSTACK. The advantage of such an approach is that it works even when the accumulated rows are not independent. In a time series financial model, it is possible that some accumulated arrays may depend upon more than one input row and need to be treated simultaneously.
An approach I finished with there was to write a Lambda function that advances the model one period (essentially providing the derivative with respect to time of the entire model). It should then be possible to place the Lambda function within SCAN to generate the entire timeseries model as an array of arrays.
Hi Peter, I must admit I had replied from my phone to the previous message without examining your file - looking more closely I see your formulas are running horizontally across the table. As Sergei implies, I think you'd need to transpose the Fibonacci setup and augment the MAP parameters to include column indices. I didn't check closely but perhaps a setup similar to below would fair better?
=LET(
n, ROWS(data),
m, COLUMNS(data),
thunks, SCAN(
LAMBDA(SEQUENCE(n, , 0, 0)),
SEQUENCE(, m),
LAMBDA(thunk,i, LET(acc, CHOOSECOLS(data, i) + thunk(), LAMBDA(acc)))
),
MAP(
IF(SEQUENCE(n), thunks),
IF(SEQUENCE(, m), SEQUENCE(n)),
LAMBDA(thunk,i, INDEX(thunk(), i))
)
)
SergeiBaklan we all learn off each other - I am just about keeping up with formula developments but not other areas the breadth of knowledge in your posts far exceeds mine (I have very little knowledge of OfficeScript, DAX, M, web, etc.) . If my post is hard to follow that is poor explanation on my part - it is the description of the concept and not any related language construct that is important. In the present situation 'list' works much the same as a thunk, the difference is only that one can include a position index if one chooses rather than leaving the argument empty.