Numbers That Do Not Lie
The last thing that separates quant Python from ordinary Python is a suspicion of your own arithmetic. Floating point is not real arithmetic, estimators are not the quantities they estimate, and both facts show up in code that runs cleanly and returns something wrong.
The variance formula everyone learns is dangerous
The identity that turns two passes into one,
is exact in real arithmetic and treacherous in floating point. When the mean is large relative to the spread, which is exactly what a price series looks like, the two terms are nearly equal and subtracting them destroys most of the significant digits. With prices near 10000 and a standard deviation near 1, the two terms agree to eight digits and a double holds about sixteen, so half the precision is gone in one subtraction. It can and does return a negative variance.
The rest of this lesson is for subscribers
Unlock every lesson in Programming for Quantitative Developers, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Programming for Quantitative Developers
25 lessons in this course, and every other premium course, on one subscription.
- Every lesson in every course, with the worked examples and interactive simulators
- Graded questions on every lesson, with explanations for the wrong answers as well as the right one
- The trainers, timed assessments and brainteaser library that go with them