• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Python Workshop - May 2, 2009

Joined
10/29/03
Messages
4
Points
11
I. Attached is the raw un-annotated transcript of today's ipython session.


II. Also, here's a "Further Reading" list:
o http://python.org/doc/
o Python Essential Reference by David M Beazley -- Third Edition, 2006; Forth edition due out this May.
o Core Python Programming by Wesley Chun -- Second Edition, 2006.
o Dive Into Python by Mark Pilgrim -- 2004. Free Download available at http://diveintopython.org/index.html
o Numerical Methods in Engineering with Python by Jaan Kiusalaas, 2005. Excerpts available on books.google.com.
o "Why Python?", by Eric Raymond (http://www.linuxjournal.com/article/3882)
o Python Module of The Week Web-Site -- http://blog.doughellmann.com/search/label/PyMOTW



III. Finally, below are some ideas for exercises that should help reinforce what we covered in the workshop, Saturday:


1. Warm-ups (Mostly taken from Chun):

1.1. Use a list-comprehension to write your own implementation of str.strip() -- should be a one-liner.

1.2. Write a one-liner that converts a date-string in the format YYYY-MM-DD into a tuple of three integers (year, month, day).


2. Daily-close prices from the Yahoo Finance data:

2.1.1. Retrieve some daily-close prices from yahoo-finance for some heavily traded instruments. These will come down as CSV-files.

2.1.2. Write a function that loads the csv into Python -- I leave it up to you how you want to represent this data in Python (e.g. a list of dictionaries, a list of tuples, etc.)

2.1.3. Write a function that converts the sequence of closing-prices into a sequence of daily returns assuming return(t+1) = (price(t+1) - price(t)) / price(t).


3. Histogram Calculator:

3.1. Write a function that takes the sequence of numbers and returns histogram-buckets like so:
hist_buckets([9,22,39,14,25,11], n_buckets=3)
[[(9,19), 3], [(19,29), 2], [(29,39), 1]]

3.2. Hit Google and see if there are libraries that do this already. If you find any, try them out.
3.2.1. What did you find?
3.2.2. Were you able to use it?
3.2.3. If so, was it easier to use than the one you had written?
3.2.4. How would you judge the quality of the code?
3.2.4.1. Did it work?
3.2.4.2. Did the results it gave make sense? Were they correct?
3.2.4.3. Was it buggy? Buggier than your code?
3.2.4.4. Was the interface more useable than yours?
3.2.4.5. How did the code look? More readable than yours? Less?
3.2.4.6. Did the project appear to be active? Did you get the sense there was an active community behind it? Or did it look like some code somebody posted and no one ever touched it again?

3.3."Extra Credit:" Look up the unittest package in the Python Library online documentation.
3.3.1. Create some unit-tests for your function using the above example.
3.3.2. Try to run your unittests using nose or trial (part of twisted).


4. Put it all together:

4.1. Choose some histogram calculator (be it your own, or one of the ones you found out there)

4.2. Feed your daily-returns into the histogram calculator. Try various n_buckets settings.

4.3. Based on your advanced studies in the topic of price-processes, any comments on the results?
 

Attachments

  • all-20090502.txt
    8.7 KB · Views: 90
What I have for 1.1 works, but seems horrible:
C++:
def my_strip(inString):
    # There must be a better way.
    return inString[max([0]+[x for x in range(len(inString)) \
                             if inString[:x].isspace()]):\
                    min([x for x in range(len(inString)) \
                         if inString[x:].isspace()]+[len(inString)])]

Could somebody help a Python newb out and share a more elegant way to do this?
 
Bob, I'm working on it. I had a solution in two lines. I'll post my one liner as soon as it's done.

Here is my two-liner (still thinking about a clean one liner) but not very efficient:

C++:
def trim_str(the_str):
    str_elem = [x for x in range(0,len(the_str)) if not the_str[x].isspace()]
    return the_str[min(str_elem):max(str_elem)+1]
 
Bob, I'm working on it. I had a solution in two lines. I'll post my one liner as soon as it's done.

Here is my two-liner (still thinking about a clean one liner) but not very efficient:

C++:
def trim_str(the_str):
    str_elem = [x for x in range(0,len(the_str)) if not the_str[x].isspace()]
    return the_str[min(str_elem):max(str_elem)+1]
You might want to try:
trim_str(" ")

More efficient than mine, but still more vigorous than it absolutely has to be. Can't see a lazy way to do it in one line, though.
 
A couple hints

Try typing these at the python or ipython prompt:

>>> ''.join(['a','s','d'])

>>> [x for x in 'ABCD' if x not in 'AC']
 
Back
Top