Help Needed on Financial Simulation

extracted from craigslist “gigs” / SFO

Urgent: Help needed with financial simulation.

2008-07-05

PLZ someone hlp me! My (part-time) boss at Lehman Brothers had me write this prgm and he used it ‘cause he didn’t trust his own programmers (I’m his cousin.) Every year I give him an updated version, when I’m taking a break from surfing in Hawaii or hiking in the Alaskan wilderness. These software gigs pay so well I can take about nine months off every year.

Here’s the problem. A few months back in the spring of 2008 I found a couple of bugs. The program will either crash with a stack overflow or produce impossible numbers! Turns out if we change the HousePricesRise constant into a variable and toggle it to false every few iterations the crazy numbers go away. I told my boss I would change it but he freaked out on me and started yelling and said that was impossible, and I should join the real world and get my head out of my ass, etc. He sure can yell.

Then there’s the problem of creating derivatives on the same loans and doing it over and over (see the ‘create_commissions method on Firm,). It’s just a technicality of Ruby that you can’t call a function on itself forever but somehow I think it could indicate we’re approaching the whole thing wrong. I’m not sure, but it just doesn’t feel right.

Anyway, I called him about that problem last spring but as soon as I started to explain he broke down crying, and it was sort of awkward. He never did say how I should handle the problem. Every time I tried to ask the question he’d just break into more sobbing. My Satelite phone was dying at that point anyhow so I let him go. I figured he had some personal issues to work through and I’d give him time and we would talk business when he had his shit together.

I’m back from my five months of alone time in Alaska and my problem is I can’t resolve the crashing problem. Anyone got any good ideas? According to the simulation you just can’t run a bank this way but that can’t be right because my boss told me the program is a perfect description of how his department works. Thing is, I can’t raise him now, no matter who I call.
The bank doesn’t have him in their directory and I’m getting worried. It’s weird too, ‘cause their website seems to be off-line. Whatever.

Before I email him the new version of the program I need to get it working. Help!

PLZ, im desparate at this point. I have to have this thing sent out in five days. If you help me to fix it i will cut you in for 40% of whatever he pays me. I’m posting the code so you can get a jump on the problem.

THX

p.s. Pay special attention to the Firm#create_commissions() and the Economy#simulate() methods, that’s where all the magic happens.


class Firm
  SalesCharge = 0.02
  
  attr_accessor :bonuses

  def create_commissions_from(shitty_loans)
    commissions = 0.0
    
    cdo = format_as_colateralized_debt_obligation shitty_loans
    aaa_debt = RatingsAgency.format_as :AAA, cdo
    
    commissions += sell aaa_debt, :bulls
    
    cds = create_credit_default_swap cdo
    
    commissions += sell cds.short_side, :bears
   
    pay_bonuses commissions
    
    create_commissions cds.long_side    
  end
  
  
  private
  
  def sell(security, customer)
    screw customer    
    security = Security.new(security) 
    return security.value * SalesCharge
  end
  
  def screw(anyone)
    true
  end
   
  def format_as_colateralized_debt_obligation(debt)
    return ColateralizedDebtObligation.new(debt)
  end
  
  def create_credit_default_swap(security)
    return CreditDefaultSwap.new(security)
  end
  
 
  def pay_bonuses(commissions)
    self.bonuses = commissions
    puts "Made $" + self.bonuses.round.to_s
  end
  end
  
  
class Security
    attr_accessor :based_on, :risk
    
    def initialize(value)
      self.based_on = value
      if self.based_on.respond_to?(:grade) then
        self.risk = 0.001 if value.grade == :AAA
        self.risk = 0.1 if value.grade == :BB
      else
        self.risk = 0.1 
      end
    end
    
    # Unwrap security, but don't adjust final risk calculation 
    def value
      unless self.based_on.respond_to?(:value)
        return self.based_on
      else
        return self.based_on.value
      end
    end
  end
   
  class ColateralizedDebtObligation < Security;end
    
  class CreditDefaultSwap
    RiskDiscount = 0.001    
    attr_accessor :security 
    
    def initialize(security)
      self.security = security
    end
    
    def long_side
      return Security.new(security.value)
    end
           
    def short_side
      return Security.new(security.value * security.risk  * RiskDiscount)
    end   
  end  
  
  
class RatingsAgency  
  def self.format_as(rating,  debt)
    RatedDebt.new(rating , debt)
  end
  end
   
 
  class RatedDebt < Security
    attr_accessor :grade
    
    def initialize(rating, debt)
      self.grade = rating
      super(debt)      
    end
  end
  
class MortgageOriginator
  LoanBundle = 10000000.0 

  def self.originate    
        return MortgageOriginator.inflated_price(MortgageOriginator::LoanBundle) 
  end
  
  private  
   
   def self.inflated_price(price)
      return price + price * $percent_increase/100.0  + 1.0 
    end  
end

class CountrywideFinancial < MortgageOriginator; end

class Economy
  HomePricesRise = true
  RealEstateMonthlyIncrease = 1.0  # Very conservative
  
    def initialize
      self.real_estate_value_increase = 0.0 # % price change since start of simulation
    end

    def increase_real_estate_values #  one month goes by ...
      self.real_estate_value_increase += RealEstateMonthlyIncrease
    end
    
    def simulate
      months = 0
      investment_bank = Firm.new  
      shitty_loans = 0.0
    
      while HomePricesRise 
          increase_real_estate_values

          shitty_loans += CountrywideFinancial.originate 
          puts "Loan values " + shitty_loans.to_s
    
          investment_bank.create_commissions_from shitty_loans 
          months += 1
          puts "Month #{months}"
      end
    end
end

new_economy = Economy.new
new_economy.simulate