<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hackinghat.com &#187; finance</title>
	<atom:link href="http://www.hackinghat.com/index.php/category/finance/feed" rel="self" type="application/rss+xml" />
	<link>http://www.hackinghat.com</link>
	<description></description>
	<lastBuildDate>Wed, 19 May 2010 07:48:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Calculating peak-to-trough drawdown</title>
		<link>http://www.hackinghat.com/index.php/python/calculating-peak-to-trough-drawdown</link>
		<comments>http://www.hackinghat.com/index.php/python/calculating-peak-to-trough-drawdown#comments</comments>
		<pubDate>Fri, 28 Sep 2007 07:39:59 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/index.php/python/calculating-peak-to-trough-drawdown</guid>
		<description><![CDATA[Ok, so this is a little bit technical but it&#8217;s an intriguing puzzle that got me thinking quite hard. So here&#8217;s the problem. Sometimes investors want to be able to judge what the absolute worst case scenario would have been if they&#8217;d invested in something. Look at the following random graph of pretend asset prices: [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so this is a little bit technical but it&#8217;s an intriguing puzzle that got me thinking quite hard.    So here&#8217;s the problem.   Sometimes investors want to be able to judge what the absolute worst case scenario would have been if they&#8217;d invested in something.    Look at the following random graph of pretend asset prices:</p>
<p><img id="image61" src="http://www.hackinghat.com/wp-content/uploads/2007/09/peak-to-trough.png" alt="Peak-To-Trough" /></p>
<p>You&#8217;ll see that there are two points on the graph (marked in red) where if you had invested at the first point and pulled out on the second point you would have the worst-case loss.  This is the point of this analysis and is a way for investors in the asset to see how bad, &#8216;bad&#8217; has really been in the past.   Clearly past prices are not an indicator of future losses.  <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The upper one is the &#8216;peak&#8217; and the lower one is the &#8216;trough&#8217;.   Well, finding these two babys by eye is trivial.   To do it reliably (and quickly) on a computer, is not that straight forward.   Part of the problem is coming up with a consistent natural language of what you want your peak and trough to be.   This took me some time.   I believe what I really want is: the largest positive difference of high minus low where the low occurs after the high in time-order.   This was the best I could do.   This led to the first solution (in Python):</p>
<pre>
<code>
def drawdown(prices):
	maxi = 0
	mini = 0
	for i in range(len(prices))[1:]:
	   maxj = 0
	   max = 0
	   for j in range(i+1, len(prices)):
		if prices[i] - prices[j] > max:
		    maxj = j
		    max = prices[i] - prices[j]
	   if max > prices[maxi] - prices[mini]:
	   	maxi = i
		mini = maxj
	return (prices[maxi], navs[mini])
</code>
</pre>
<p>Now this solution is easy to explain.   It&#8217;s what I have come to know as a &#8216;between&#8217; analysis.   I don&#8217;t know if that&#8217;s the proper term but it harks back to the days when I used to be a number-cruncher for some <a href="http://linkinghub.elsevier.com/retrieve/pii/S0379073800002711">statisticians</a>.   The deal is relatively straight-forward: compare the fist item against every item after it in the list and store the largest positive difference.     If this difference is also the largest seen in the data-set so far then make it the largest positive difference of all points.  At the end you just return the two points you found.   This is a natural way to solve the problem because it looks at all possible start points and assesses what the worst outcome would be.   </p>
<p>The problem with this solution is that it has quadratic complexity.   That is for any data-series of size N the best and worst case will result in N * N-1 iterations, in shorthand this is <a href="http://www.nist.gov/dads/HTML/bigOnotation.html">O(N^2)</a>.   For small n this doesn&#8217;t really matter, but for any decently sized data-series this baby will be slow-as-molasses.  The challenge then is to find an O(N) solution to the problem and to save-those-much-needed-cycles for something really <a href="http://setiathome.berkeley.edu/">important</a>:</p>
<pre>
<code>
def drawdown(prices):
  prevmaxi = 0
  prevmini = 0
  maxi = 0

  for i in range(len(prices))[1:]:
    if prices[i] >= prices[maxi]:
      maxi = i
    else:
      # You can only determine the largest drawdown on a downward price!
      if (prices[maxi] - prices[i]) > (prices[prevmaxi] - prices[prevmini]):
	prevmaxi = maxi
	prevmini = i
      return (prices[prevmaxi], prices[prevmini])
</code>
</pre>
<p>This solution is a bit harder to explain.   We move through the prices and the first part of the &#8216;if&#8217; will find the highest part of the peak so far.    However, the second part of the &#8216;if&#8217; is where the magic happens.   If the next value is less than the maximum  then we see if this difference is larger than any previously encountered difference, if it is then this is our new peak-to-trough.</p>
<p>The purist in me likes that fact that the O(N) solution looks like easier code to understand than the O(N^2) solution.    Although the O(N^2) solution is, I think, an easier concept to grapple with, when it&#8217;s translated into code it just doesn&#8217;t grok.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/python/calculating-peak-to-trough-drawdown/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>refused by my own credit scoring system!</title>
		<link>http://www.hackinghat.com/index.php/finance/refused-by-my-own-credit-scoring-system</link>
		<comments>http://www.hackinghat.com/index.php/finance/refused-by-my-own-credit-scoring-system#comments</comments>
		<pubDate>Thu, 23 Nov 2006 11:21:46 +0000</pubDate>
		<dc:creator>Steve Knight</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[finance]]></category>

		<guid isPermaLink="false">http://www.hackinghat.com/?p=7</guid>
		<description><![CDATA[My wife and I recently purchased a new car. After much looking around on the market and test driving and the like we decided to purchase a vehicle through a company that I had previously worked for. I agreed to purchasing the car on 0% finance and was promptly taken to a private room to [...]]]></description>
			<content:encoded><![CDATA[<p>My wife and I recently purchased a new car.   After much looking around on the market and test driving and the like we decided to purchase a vehicle through a company that I had previously worked for.</p>
<p>I agreed to purchasing the car on 0% finance and was promptly taken to a private room to go through their credit searching system.   No one was as surprised as me to discover that the credit searching system I had worked on 10 years previously, at the finance arm of the company, was still functioning and was about to credit check me!</p>
<p>There was a brief moment when I had to cast my mind back to make sure that I hadn&#8217;t left any &#8216;testing&#8217; back doors in the application that I might trigger if I was to apply but then I remembered that I had not because I had a feeling that one day it might come back to haunt me.   Relieved about this I settled down to the long drawn out question and answer process that is credit scoring in the UK.   But then something strange happened (<em>room spins</em>)<span id="more-7"></span> and a whole lot of unpleasant details about car finance and the credit scoring system &#8220;The UNDERWRITER&#8221; came back to me.</p>
<p>It seems that the rules for credit scoring are actually quite complex.   I would expect that every credit organisation would have its own set of rules and in the UK there are a couple such agencies I think.   The credit providers undertake to update the credit scoring system and hence any credit you ever had is known to them and they can use that information to assess your application.   </p>
<p>Therefore it makes sense that if you&#8217;re applying for credit the following needs to be taken into consideration:</p>
<ul>
<li><strong>Have you ever had credit?</strong> Some people are surprised that if they&#8217;ve never had credit then they must be credit &#8216;worthy&#8217; because they&#8217;ve demonstrated some sort of fiscal prudence.   However this is not really the case.   The truth is that if you&#8217;ve had credit you&#8217;ve got a track record and it&#8217;s the track record that matters.   That isn&#8217;t to say that no track record is bad it just doesn&#8217;t give you as many points as having one, which might ultimately affect how much you can borrow.   Now if that track record shows missed mortgage payments or late credit card payments then this doesn&#8217;t help your score either.   </li>
<li><strong>Where do you live?</strong>  You may have noticed you have to provide previous addresses and bank details for the last 3 years.   If you&#8217;re anything like me this can be tedious.    But there&#8217;s also a catch.   Credit scores can be based by <strong>address</strong> and not name.   I think in the US where everything is controlled by social security number this doesn&#8217;t apply, but in the UK that form of ID is not required to get credit.   Now I agree you are not &#8216;where you live&#8217; but if there are people who lived at your address who had bad credit that&#8217;s going to reflect on you.  To all intents and purposes you&#8217;re in the same socio-economic group as the previous tenant of your previous addresses.   Needless to say if you have any <a href="http://en.wikipedia.org/wiki/County_court_judgment">CCJs</a> against you at an address you may as well move because almost no-one is going to want to lend to you at a good rate.  But other than that your address says a lot more about you than you might at first think and the credit scoring people care about it.   </li>
<li><strong>Do you vote?</strong>This is the one that annoys me the most.   It annoys me because I move around a lot and in the UK you don&#8217;t have to be on the electoral register and you don&#8217;t have to vote.   But the electoral register has important information about you that can&#8217;t be faked and a lot of credit agencies will flat-out refuse anyone who isn&#8217;t on the electoral register.   This can often be a way for people with bad credit to help themselves out, if your parents still live in the family home you could &#8216;move&#8217; back in on paper by redirecting electoral roll/credit card/bank statements back to that address whilst you stay in the bad address.   Effectively using their good standing (if they have one!) to improve your own.   After a while you could use this as your primary address for credit and take it from there.   However, since this is obtaining goods or services by deception it probably constitutes fraud and is not recommended since fraud carries a hefty penalty in the UK.</li>
<li><strong>Are you looking for a lot of credit?</strong>Of course you are, but the more you look the lower your score gets.   This makes sense on some level because if you start looking for a lot of money all of a sudden things can&#8217;t be good with you.    This was demonstrated to me when we finally put the &#8220;UNDERWRITER&#8221; live and ran a couple of searches on my friend&#8217;s father to make sure that it was all connected up and working.    We&#8217;d used him as a test case in our non-updating test system before we put the system live.   Someone wanted to verify that all was good and over zealously issued 5 or so credit requests in a minute and my friends father&#8217;s credit rating went from hero to zero.   Suffice to say credit searching without the intent of acquiring credit is illegal and we had some explaining to do.</li>
</ul>
<p>Back to the story (<em>room stops spinning</em>).    I met the critieria: I&#8217;d had a stable bank and address for 3 years, I paid my mortgage and credit cards on time, I was on the electoral register and I smelt of roses.   Nothing could go wrong.</p>
<p>My own credit scoring system referred me!   Now that&#8217;s not the same as being declined, it means that the computer wasn&#8217;t authorised to complete the scoring on its own and a human needed to look at it.   There is a reason for this too.   Some finance deals (0% ones being an example) are of limited value to the underwriter.    They want to take extra care that they&#8217;re not issuing &#8216;risky&#8217; finance on a 0% deal because the <a href="http://en.wikipedia.org/wiki/Time_Value_of_Money">time-value of money</a> means that they won&#8217;t be compensated for it.</p>
<p>Suffice to say a few days later someone looked at my &#8216;deal&#8217; and issued it and I got the car.   Sweet.   It was especially sweet that the system that I helped write 10 years ago was still functioning.   Which shows I can&#8217;t be all that bad at this software malarkey.  YMMV <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackinghat.com/index.php/finance/refused-by-my-own-credit-scoring-system/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

