SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

exp(5) = [$]e^5[$]

October 7th, 2011, 8:02 pm

#include <cmath> with using namespace std probably does that.(I prefer not to use the using-directive (using namespace std) at all, and that's one of the reasons (namespace pollution leading to unpredictable/undersirable results); using-declarations (using std::cout) are somewhat better, but once you get used to it just std::cout is fine).What's more annoying, though, is that some compilers (MSVC comes to mind) import the names anyway, even if you use neither the using-directive nor the using-declaration. So, there's no difference whether you #include <cmath> or #include <math.h> on those compilers (even though the standard dictates that there should be).I also think that "size_t" works just about anywhere in MSVC, whether you use stddef.h, cstddef or neither (and no form of "using" at all).
Last edited by Polter on October 6th, 2011, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

October 8th, 2011, 6:44 am

QuoteOriginally posted by: Polter#include <cmath> with using namespace std probably does that.(I prefer not to use the using-directive (using namespace std) at all, and that's one of the reasons (namespace pollution leading to unpredictable/undersirable results); using-declarations (using std::cout) are somewhat better, but once you get used to it just std::cout is fine).What's more annoying, though, is that some compilers (MSVC comes to mind) import the names anyway, even if you use neither the using-directive nor the using-declaration. So, there's no difference whether you #include <cmath> or #include <math.h> on those compilers (even though the standard dictates that there should be).I also think that "size_t" works just about anywhere in MSVC, whether you use stddef.h, cstddef or neither (and no form of "using" at all).Tried all; no joy.. edit: my exp() code is wrong(foobar).
Last edited by Cuchulainn on October 7th, 2011, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
katastrofa
Posts: 9581
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

exp(5) = [$]e^5[$]

October 8th, 2011, 7:12 pm

Apropos compilers, I've got the following piece of code (below). It works fine with an MS compiler. With g++ compiler under linux it reads only argv[1], doesn't convert argv[2] into double and doesn't create the file(name), reporting no error or warning during the compilation. It took me a while to figure out that I need to add ss.clear() after each ss.str("") to make it work. Which compiler is buggy (or am I still doing sth wrong)? (Despite the way I've learnt it, g++'s behaviour seems more reasonable to me).a.
Last edited by katastrofa on October 7th, 2011, 10:00 pm, edited 1 time in total.
 
User avatar
Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

exp(5) = [$]e^5[$]

October 8th, 2011, 9:14 pm

 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

November 21st, 2011, 7:21 am

Most of the 10 solutions till now are missing one ingredient, i.e. we cannot *check* if we are getting closer to the real solution. Can we use a geometric (e.g. integral) argument to get 2-sided estimates, like Archimedes' calculation of pi by inscibing and circumscribing polygons of increasing degree around a circle? The missing link is that log is the inverse of exp and it has a computable integral:y = exp(5) => 5 = log(y) = Integral(1 to y of f(t) = 1/t) ... (1)The problem is now to find y that satisfies (1). We can use a numerical quadrature (e.g. midpoint) for RHS of (1) and an interval-reduction method to bound the true solution.For example, I manually tried some values and compared with LHS of (1). [100,200] -> [4.6, 5.2][120,160] -> [4.7, 5.06][140,160] -> [4.93,5.06] Good example of interval arithmetic.
Last edited by Cuchulainn on November 20th, 2011, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Costeanu
Posts: 189
Joined: December 29th, 2008, 5:33 pm

exp(5) = [$]e^5[$]

March 4th, 2012, 3:19 pm

Here's a cute way to calculate exp(5). Use Stirling's formula: 5! ~ 5^5 * exp(-5) * sqrt(2*pi * 5)We get exp(5) ~ 625*sqrt(10 pi) / 24Now, one can argue that: 1. this calculation is hard to do by hand, and 2. it's a rough approximation anyway. 1. It so happens that this calculation is easy for someone who does math finance. We all know the ATM call price approximation, which has a factor of 1/sqrt(2pi), and which we happily replace with 0.4. The square root of 5 can be done in a few seconds using the babylonian approximation: start with the initial guess of 2, and the next iteration is (2+5/2)/2=2.25 As for 625/24, we know that 625 = 25^2, so 625-1 = 24*26. We have the following 3 approximations: - sqrt(2*pi) ~ 2.5 (actual is 2.50663)- sqrt(5) ~ 2.25 (actual is 2.23607)- 625/24 ~ 26 (actual is 26.0417) Result is 146.25. And one could argue that this can be done with pen and paper, or even without. The actual value of 625*sqrt(10 pi) / 24 is 145.963. 2. How about the argument that Stirling's approximation may be too rough? We can take the next 1 or 2 terms in the Stirling series. They are 1/12*n and 1/288*n^2. In our case 1/60 and 1/7200. Here's how 5! is approximated: - no correction: 118.019- first correction: 119.986- second correction: 120.003Here's how exp(5) gets resolved (actual value 148.413):- no corrrection: 145.963- first correction: 148.396- second correction: 148.416Back to 1. Can we improve our result using only pen and paper, or only mental calculations? You bet. Here's how: - the next babylonian iteration for sqrt(5) is (2.25+5/2.25)/2. We need first 5/2.25 = 20/9 = 2.22222... The mid point between 2.22 and 2.25 is 2.235, which is quite decent (actual value is 2.23607)- next babylonian iteration for sqrt(2*pi) is (2.5 + 2*pi/2.5)/2. The fraction 2*pi/2.5 is the same as 8*pi/10, and if we use the humble approximation of pi~3.14 we get (2.5 + 2.512)/2 ~ 2.506 (actual is 2.40663). - we can use the improved 625/24 = 26 + 1/24 ~ 26 + 1/25 = 26.04 (actual value is 26.04167). - and finally we use only the first Stirling correctionWe get exp(5) ~ 148.27Best,V.
Last edited by Costeanu on March 4th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

March 9th, 2012, 9:18 am

Costeanu,That's a nice one The Babylonian method (aka fixed point theorem) is linear convergent and we get 2nd-order convergence using Aitken's delta process In fact, x(0) = 2.25 is not even needed because these methods converge for _any_ x(0).
Last edited by Cuchulainn on March 8th, 2012, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

October 14th, 2012, 2:47 pm

TI-89
Last edited by Cuchulainn on October 14th, 2012, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Traden4Alpha
Posts: 23951
Joined: September 20th, 2002, 8:30 pm

exp(5) = [$]e^5[$]

October 14th, 2012, 3:20 pm

5th scale in from the outside:
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

October 15th, 2012, 6:16 pm

QuoteOriginally posted by: Traden4Alpha5th scale in from the outside:What answer does it give?
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

October 16th, 2012, 5:27 am

Looks kind of bulky. Plan B; A book of logarithms and find fixed point of x = g(x) := x + 5 - log(x).
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62916
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

exp(5) = [$]e^5[$]

October 16th, 2012, 9:20 am

I bet T4A could make a slide rule that does fixed points.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Traden4Alpha
Posts: 23951
Joined: September 20th, 2002, 8:30 pm

exp(5) = [$]e^5[$]

October 16th, 2012, 12:24 pm

QuoteOriginally posted by: CuchulainnI bet T4A could make a slide rule that does fixed points.It's pretty easy, actually. Just plot the function and fold the piece of paper on the diagonal. What I've always liked are nomograms:Or this one that solves quadratic equations:
Last edited by Traden4Alpha on October 15th, 2012, 10:00 pm, edited 1 time in total.
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On