Archive for the ‘programming’ Category

Python Monte Carlo graphs

Wednesday, November 16th, 2011

I wanted to plot the paths of my Monte Carlo simulation of Black Scholes in my previous post. Using iPython started with the --pylab flag, it's pretty easy. [sourcecode language=Python] from math import exp from random import gauss St0=41.75 rate=.0535 vol=.34 trials=100 for trials in range(trials): prices=[St0] for day in range(time): ...

Black Scholes Monte Carlo in Python

Wednesday, November 16th, 2011

The Black Scholes formula is a partial differential equation that can be used to price the present value of an option under certain assumptions. As with everything, you can read all about it in Wikipedia. It turns that that figuring out the present value of an option is also a ...

Python Monte Carlo

Sunday, November 13th, 2011

Monte Carlo is a simulation method that can be useful in solving problems that are difficult to solve analytically. Here's an interesting application of the technique to estimate the value of pi. Consider a circular dartboard placed against a square backing, with the sides of the square perfectly touching the circle tangentially ...

Prime numbers and Numpy – Python

Friday, August 27th, 2010

I've been looking at the element-wise operations in Numpy arrays. The code below generates 100K primes in about 1.3 secs on my laptop. [sourcecode language="python"] import numpy import math def prime(upto=100): return filter(lambda num: (num % numpy.arange(2,1+int(math.sqrt(num)))).all(), range(2,upto+1)) [/sourcecode] >>> prime() [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ...

Birthday Simulations – Python

Thursday, August 26th, 2010

I've wanted for a while to write simple python simulations of the birthday puzzles I'd written about previously. Then, recently, I was wondering what the most efficient way to tell if a list has duplicate elements. It suddenly dawned on me that these two topics were related: The ...

Shortest Path – Python

Tuesday, August 24th, 2010

Problem: Write a python function to calculate the shortest path given a start node (or vertex), an end node and a graph. Use Dijkstra's algorithm. Return the distance from the start node to the end node, as well as the path taken to get there. The implementation ...

Cyclic Linked List – Python function to find the node at the start of the loop

Sunday, July 18th, 2010

I'd blogged previously about writing a python function to find out whether a linked list is acyclic or not - it's worth a read before tackling this one. Problem: Given the head node of a cyclic (or circular) linked list, write a function to return the node that is at ...

Anagrams – Python

Friday, June 4th, 2010

Write a Python function to find all the anagrams for each of a supplied list of words. For the word dictionary to find the anagrams you could use the one found on Unix or OS X boxes at /usr/share/dict/words. So for example: >>> for group in anagrams(['dog','cat','horse']): ... ...

Mth to last – a recursive approach

Thursday, June 12th, 2008

A collegue of mine was pondering the mthToLast problem for a LinkedList I'd blogged about previously and sent me the following IM message: just a guess - but can you use recursion to solve your link list m problem? a method to track through the list which calls itself, then ...

Reverse a string using recursion

Thursday, June 12th, 2008

Very basic - take a string and reverse it using simple recursion. I'm sure there's a bunch of ways to do it, but like the fibonacci you should aim to be able to code something like the below in about 20 seconds... >>> def reverse(str): if len(str)==1: ...