[Python-ideas] Reduce/fold and scan with generator expressions and comprehensions

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Mon Oct 24 22:26:43 EDT 2016


Danilo J. S. Bellini writes:

 > >>> [prev * k for k in [5, 2, 4, 3] from prev = 1]
 > [1, 5, 10, 40, 120]

 > Among the examples I wrote on PyScanPrev, there are use cases on:
 > - maths
 > - physics
 > - economics

As a practicing economist, I wonder what use cases you're referring
to.  I can't think of any use cases where if one previous value is
useful, having all previous values available (ie, an arbitrary
temporal structure, at the modeler's option) isn't vastly more useful.

This means that in modern econometrics, for example, simple procedures
like Cochrane-Orcutt (which handles one previous value of the
dependent variable in a single-equation regression) are subsumed in
ARIMA and VAR estimation, which generalize the number of equations
and/or the number of lags to greater than one.  BTW, numerical
accuracy considerations often mean you don't want to use the compact
"for ... in ... if ..." expression syntax anyway, as accuracy can
often be greatly improved with appropriate reordering of values in the
series.

Even "online" regression algorithms, where you might think to write

    ( updated_model(datum, prev)
      for datum in sensor_data()
      from prev = something )

'prev' need to refer not to the previous value of 'datum', but to the
previous value of 'updated_model()' (since you need a sufficient
statistic for all previous data).  And 'prev' as currently conceived
is just plain useless for any long period moving average, etc.

So in the end, even if there are plausible use cases for quick and
dirty code, an experienced implementer wouldn't use them anyway as
more powerful tools are likely to be immediately to hand.


More information about the Python-ideas mailing list