Popular recipes by Alia Khouri http://code.activestate.com/recipes/users/4169084/2013-09-06T23:04:55-07:00ActiveState Code RecipesNPV / IRR / Payback Analysis (Python) 2013-09-06T23:04:55-07:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576686-npv-irr-payback-analysis/ <p style="color: grey"> Python recipe 576686 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/finance/">finance</a>). Revision 18. </p> <p>A few financial functions for quick analysis of an investment opportunity and a series of associated cashflows.</p> <p>As a module, it currently provides straightforward and easy to understand implementations of the Net Present Value (NPV), Internal Rate of Return (IRR), and Payback Period functions.</p> <p>As a script, it provides a simple command line interface which integrates the above functions into a concise analysis of the investment opportunity.</p> <pre class="prettyprint"><code> usage: invest discount_rate [cashflow0, cashflow1, ..., cashflowN] where discount_rate is the rate used to discount future cashflows to their present values cashflow0 is the investment (always a negative value) cashflow1 .. cashflowN values can be positive (net inflows) or negative (net outflows) </code></pre> <p>Here is an example of actual usage and output:</p> <pre class="prettyprint"><code>$ ./invest 0.05 -10000 6000 6000 6000 ---------------------------------------------------------------------- year 0 1 2 3 cashflow -10,000 6,000 6,000 6,000 Discount Rate: 5.0% Payback: 1.67 years IRR: 36.31% NPV: 6339.49 ==&gt; Approve Investment of 10,000 ---------------------------------------------------------------------- </code></pre> <p><em>Note</em>: A check of the output of the Microsoft Excel NPV function against that of the function implemented here reveals a curious discrepancy/bug in the way Excel calculates its NPV. For further details see: <a href="http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/" rel="nofollow">http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/</a></p> <p>Furthermore, the method used to calculate the IRR is rough to say the least and fails at fewer than 3 entries. Please use the secant method along the lines of the following haskell code from (<a href="http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio" rel="nofollow">http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio</a>) for greater accuracy.</p> <pre class="prettyprint"><code>secant :: (Double -&gt; Double) -&gt; Double -&gt; Double secant f delta = fst $ until err update (0,1) where update (x,y) = (x - (x - y) * f x / (f x - f y), x) err (x,y) = abs (x - y) &lt; delta npv :: Double -&gt; [Double] -&gt; Double npv i = sum . zipWith (\t c -&gt; c / (1 + i)**t) [0..] irr :: [Double] -&gt; Double irr cashflows = secant (`npv` cashflows) (0.1**4) </code></pre> Update Source Directories (Python) 2009-07-21T23:40:53-07:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576853-update-source-directories/ <p style="color: grey"> Python recipe 576853 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/bzr/">bzr</a>, <a href="/recipes/tags/git/">git</a>, <a href="/recipes/tags/hg/">hg</a>, <a href="/recipes/tags/svn/">svn</a>, <a href="/recipes/tags/vcs/">vcs</a>). </p> <p>A convenience script to update a pre-specified folder containing subversion, mercurial, bazaar, and/or git source folders</p> <p>To use it: </p> <ul> <li><p>change the 'src' variable below to point to your source folder</p></li> <li><p>name this script to something appropriate (I call it 'update')</p></li> <li><p>put it into a directory on your PATH</p></li> </ul> Recursive file/folder cleaner (Python) 2011-02-12T20:30:59-08:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576643-recursive-filefolder-cleaner/ <p style="color: grey"> Python recipe 576643 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/cleaner/">cleaner</a>, <a href="/recipes/tags/cleaning/">cleaning</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/folder/">folder</a>, <a href="/recipes/tags/pyc/">pyc</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/svn/">svn</a>). Revision 24. </p> <p>This script recursively scans a given path and applies a cleaning 'action' to matching files and folders. By default files and folders matching the specified (.endswith) patterns are deleted. Alternatively, _quoted_ glob patterns can used with the '-g' or '--glob' option.</p> <p>By design, the script lists targets and asks permission before applying cleaning actions. It should be easy to extend this script with further actions and also more intelligent pattern matching functions.</p> <p>The getch (single key confirmation) functionality comes courtesy of <a href="http://code.activestate.com/recipes/134892/" rel="nofollow">http://code.activestate.com/recipes/134892/</a></p> <p>To use it, place the script in your path and call it something like 'clean':</p> <pre class="prettyprint"><code>Usage: clean [options] patterns deletes files/folder patterns: clean .svn .pyc clean -p /tmp/folder .svn .csv .bzr .pyc clean -g "*.pyc" clean -ng "*.py" converts line endings from windows to unix: clean -e .py clean -e -p /tmp/folder .py Options: -h, --help show this help message and exit -p PATH, --path=PATH set path -n, --negated clean everything except specified patterns -e, --endings clean line endings -g, --glob clean with glob patterns -v, --verbose </code></pre>