Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9274951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:28:27+00:00 2026-06-18T16:28:27+00:00

I have the following code which works perfect: from __future__ import division from math

  • 0

I have the following code which works perfect:

    from __future__ import division
    from math import log, sqrt
    from scipy.stats import norm
    from datetime import datetime

    #Default values used for testing
    s1 = 10; s2 = 20
    sigma1 = 1.25; sigma2 = 1.45
    t = 0.5; rho = 0.89
    rate=0.05; y=0; k0 = 0.000001

    sigma = lambda sig1=sigma1, sig2=sigma2, corr=rho: sqrt(sig1**2+sig2**2 -2*corr*sig1*sig2)
    m_d1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: (log(stock1/stock2)+1/2*sigma()**2*time)/(sigma()*sqrt(time))
    m_d2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: m_d1() -sigma()*sqrt(time)
    m_delta1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.cdf(m_d1())
    m_delta2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -norm.cdf(m_d2())
    m_gamma11 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.pdf(m_d1())/(stock1*sigma()*sqrt(time))
    m_gamma22 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: norm.pdf(m_d2())/(stock2*sigma()*sqrt(time))
    m_gamma12 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho:-norm.pdf(m_d1())/(stock2*sigma()*sqrt(time))
    m_theta = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -stock1*sigma()*norm.pdf(m_d1())/(2*sqrt(time))
    m_vega1 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*sqrt(t)*norm.pdf(m_d1())*((sig1-(corr*sig2))/sigma())
    m_vega2 = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*sqrt(t)*norm.pdf(m_d1())*((sig2-(corr*sig1))/sigma()) 
    m_correlation = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: -stock1*sqrt(t)*norm.pdf(m_d1())*((sig1*sig2)/sigma()) 
    m_margrabe = lambda stock1=s1, stock2=s2, sig1=sigma1, sig2=sigma2, time=t, corr=rho: stock1*norm.cdf(m_d1())-stock2*norm.cdf(m_d2())

    print "Margrabe = "+str(m_margrabe()) + "\n"
    print "THE GREEKS \n"
    print "Delta Asset 1 = "+str(m_delta1())
    print "Delta Stock 2 = "+str(m_delta2()) +"\n"
    print "Gamma Asset 11 = "+str(m_gamma11())
    print "Gamma Stock 12 = "+str(m_gamma12())
    print "Gamma Stock 22 = "+str(m_gamma22()) + "\n"
    print "Theta = "+str(m_theta()) +"\n"
    print "Vega sigma 1 = "+str(m_vega1())
    print "Vega sigma 2 = "+str(m_vega2()) + "\n"
    print "Correlation = "+str(m_correlation()) + "\n"

It works perfect, but now I want to see how long my computer takes to run this program. So after googling the problem I found 2 alternatives:

1) from timeit import Timer . and in the part where I print the results, I added:

    print "Margrabe = "+str(m_margrabe()) +"\n"
    t1 = Timer(lambda: m_margrabe()).timeit(number=1)
    print "THE GREEKS \n"
    print "Delta Asset 1 = "+str(m_delta1())
    t2 = Timer(lambda: m_delta1()).timeit(number=1)
    print "Delta Stock 2 = "+str(m_delta2()) +"\n"
    t3 = Timer(lambda: m_delta2()).timeit(number=1)
    print "Gamma Asset 11 = "+str(m_gamma11())
    t4 = Timer(lambda: m_gamma11()).timeit(number=1)
    print "Gamma Stock 12 = "+str(m_gamma12())
    t5 = Timer(lambda: m_gamma12()).timeit(number=1)
    print "Gamma Stock 22 = "+str(m_gamma22()) +"\n"
    t6 = Timer(lambda: m_gamma22()).timeit(number=1)
    print "Theta = "+str(m_theta()) + "\n"
    t7 = Timer(lambda: m_theta()).timeit(number=1)
    print "Vega sigma 1 = "+str(m_vega1())
    t8 = Timer(lambda: m_vega1()).timeit(number=1)
    print "Vega sigma 2 = "+str(m_vega2()) +"\n"
    t9 = Timer(lambda: m_vega2()).timeit(number=1)
    print "Correlation = "+str(m_correlation()) + "\n"
    t10 = Timer(lambda: m_correlation()).timeit(number=1)
    print "Running Time = " + str(t1+t2+t3+t4+t5+t6+t7+t8+t9+t10)

this works fine: I get Running Time = 0.00682769470029

2)from datetime import datetime and:

    startTime = datetime.now()
    # Same code as before"
    print(datetime.now()-startTime)

This also works fine and i get: 0:00:00.010000

My question is: Why are both methods different? Why do I get a different result? Is there a way to do this “calculating the running time” in one line?

Thank you

UPDATE:

    def printit():  
        print "Margrabe = "+str(m_margrabe()) + "\n"
        print "THE GREEKS \n"
        print "Delta Asset 1 = "+str(m_delta1())
        print "Delta Stock 2 = "+str(m_delta2()) +"\n"
        print "Gamma Asset 11 = "+str(m_gamma11())
        print "Gamma Stock 12 = "+str(m_gamma12())
        print "Gamma Stock 22 = "+str(m_gamma22()) + "\n"
        print "Theta = "+str(m_theta()) +"\n"
        print "Vega sigma 1 = "+str(m_vega1())
        print "Vega sigma 2 = "+str(m_vega2()) + "\n"
        print "Correlation = "+str(m_correlation()) + "\n"


    if __name__=='__main__':
        printit()

So on i can type (my file is saved as time1.py)

    python -m timeit -s "import time1; time1.main()"

but i get an error: “Attribute Error: ‘module’ object has no attribute ‘main’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:28:28+00:00Added an answer on June 18, 2026 at 4:28 pm

    You don’t need to instrument or change anything in your program to measure it. From the command line,

    python -m timeit -s "import mymodule; mymodule.main()"
    

    will tell you the running time of your module’s main(). Modify the statement to measure exactly what you need.

    The difference between the result from timeit and datetime is presumably because of low precision supported by datetime, but it is supposed to have microsecond precision so I cannot say for sure. At any rate, timeit is the right tool for the job at hand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code which works out the possible combinations from a
I have the following block of code which works fine; var boughtItemsToday = (from
I have the following code which works perfect in IE for a textarea element.
i have the following code which works perfect. Objective: Given a number n, find
I have the following code which works: $date1 = mysql_query(SELECT date1 FROM Users WHERE
I have the following code which works perfect in Chrome, IE8, and FF. However,
I have the following code: $(document).ready(function(){ $(button.continue).click(function(){ $(this).html(Hello!); }); }); Which works perfect but
I have following code which works for radio buttons but need to be changed
I have the following code which works perfectly fine, however I would like to
I have the following code which works perfectly, but I want to allow access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.