jpcolino / IPython_notebooks

Set of Jupyter (iPython) notebooks (and few pdf-presentations) about things that I am interested on, like Computer Science, Statistics and Machine-Learning, Artificial Intelligence (AI), Financial Engineering, Optimization, Stochastic Modelling, Time-Series forecasting, Science in general... and more.
GNU General Public License v3.0
97 stars 48 forks source link

@property memoization #1

Closed avishn closed 3 years ago

avishn commented 3 years ago

Using your implementation of LSM MC for derivatives pricing from https://github.com/jpcolino/IPython_notebooks/blob/master/Least%20Square%20Monte%20Carlo%20Implementation%20in%20a%20Python%20Class.ipynb - thanks! Noticed that the class is immutable and you're using @property decorator for all calculated properties. Unfortunately,@property doesn't cache (memoize) the results so all the "expensive" properties (e.g. MCprice_matrix) are recalculated many times over when referenced from other methods.

A quick fix (and a huge speedup) is possible via Boltons package from boltons.cacheutils import cachedproperty and then using @cachedproperty instead of @property everywhere.

I understand that the source code is pretty old, so just letting you know this in case you're still maintaining it (or any derivations of it). I found it useful, so possibly others find it useful too.

Thanks.

jpcolino commented 3 years ago

Thanks a lot, avish. You are right, that's something that I build 5 years ago and never thought about that. As soon as I have some spare time I will have a look. Your suggestion looks definitely interesting.