fditraglia / econometrics.blog-comments

repo for utterances comments in econometrics.blog
0 stars 0 forks source link

post/street-fighting-numerical-analysis-part-1/ #5

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Street Fighting Numerical Analysis - Part 1 | econometrics.blog

Computing is a crucial part of modern applied and theoretical econometrics but most economists, myself included, have little if any formal training numerical analysis and computer science. This means that we often learn things the hard way: by making boneheaded mistakes and spending hours browsing stackoverflow to try to figure out what went wrong.

https://www.econometrics.blog/post/street-fighting-numerical-analysis-part-1/

dacbarbosa commented 1 year ago

Dear Frank, truly interesting post! I am wondering, if one is not concerned with precision but rather in having fast in-memory calculations (suppose you have a highly non-linear optimization problem), can R deal with approximations of let's say, 6 digits only?

lasse-meixner commented 1 year ago

Great post!

For anyone reading who is curious how the binary representation actually works (I was...): The 64-bit “double-precision” representation the computer stores actually encodes a scientific notation of the floating point number, for 0.2 this would be 0.6 2^{-3} (the factor in front, here 0.6, is sometimes called the "mantissa" or "significand"*) The 64 bits essentially contain the information required to compute this quantity, and are split as follows: 1 bit is for the sign, 11 bits are for the exponent, and the remaining 52 bits are for the mantissa. For example, for 0.2 these bits correspond to: ['0', '01111111100', '1001100110011001100110011001100110011001100110011010']

How to work backwards from these binary numbers to get to these decimals is very well explained in this post. Thought this might be worth sharing!

fditraglia commented 1 year ago

@dacbarbosa Glad you enjoyed! I'm not aware a limited precision type in R, although it's possible that someone has created a package that defines an object like this. My advice would be that if you're in a setting where performance is so important that you're considering something like this, you'd be better off implementing this part of your code in a lower-level language, like C++. The Rcpp package makes it incredibly easy to connect R with C++.

@lasse-meixner Thanks for sharing!