Serving the Quantitative Finance Community

 
User avatar
player
Topic Author
Posts: 0
Joined: August 5th, 2002, 10:00 am

simple algorithm

August 7th, 2006, 7:51 am

Trivial questionI'm trying to write an algorithm which solves the followingx*P1+y*P2wnere x and y are integers P1 and P2 are prices. I want to find x and y such that the equation above is as close to zero as possible..Using excel solver it is easy but I'm writing the algorithm in matlab first hand. Any ideas??
 
User avatar
player
Topic Author
Posts: 0
Joined: August 5th, 2002, 10:00 am

simple algorithm

August 7th, 2006, 7:58 am

ok got it
 
User avatar
player
Topic Author
Posts: 0
Joined: August 5th, 2002, 10:00 am

simple algorithm

August 7th, 2006, 9:09 am

actually I dont have it so the question still stands any ides???
Last edited by player on August 6th, 2006, 10:00 pm, edited 1 time in total.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

simple algorithm

August 7th, 2006, 9:58 am

 
User avatar
mohamedb
Posts: 2
Joined: December 16th, 2004, 4:07 pm

simple algorithm

August 7th, 2006, 10:05 am

Perhaps solve it without integer programming.x/y = -P2/P1Find a rational approximation for P2/P1. The problem is that you can let x and y have arbitrary numbers of digits when P2/P1 is irrational. When do you stop? You will need to specify a precision.In Mathematica, you could use Rationalize[P2/P1, 1E-10] Eg P1 = 1, P2 = -pi => x = 312689, y = 99532BM
 
User avatar
janickg
Posts: 0
Joined: August 3rd, 2004, 1:13 pm

simple algorithm

August 7th, 2006, 12:40 pm

What about fsolve? Treat [P1 P2] as a vector multiplied against another vector of ints. Feed fsolve with a non-zero starting point.
 
User avatar
AVt
Posts: 90
Joined: December 29th, 2001, 8:23 pm

simple algorithm

August 7th, 2006, 1:50 pm

prices in cents are already integers, so x=P2, y= - P1 will do; for a 'minimal' solution cancel out common divisors (prime factors)