<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Algorithms</title><link>http://timlabonne.com:80/algorithms</link><description>Algorithms</description><item><title>Parsing a Time String with JavaScript</title><link>http://timlabonne.com:80/2013/07/parsing-a-time-string-with-javascript/</link><description>&lt;p&gt;Let’s say you are building a UI where you’d like a user to enter a time into a text box.&amp;nbsp; This time value they might enter needs to handle a lot of different formats, but we’d like it to end up as the same value either way:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;1pm  &lt;li&gt;1:00pm  &lt;li&gt;1:00p  &lt;li&gt;13:00&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Here’s a little JavaScript function to do this work.&amp;nbsp; Most of the interesting bits are the regular expression which helps handle of these various scenarios.&amp;nbsp; It takes a string representation of a time and attempts to parse it, setting the time on the specified date object sent to the function as the second parameter.&amp;nbsp; In the case where you do not provide a second parameter, the current date will be used.&lt;/p&gt;&lt;pre class="brush: js;"&gt;function parseTime(timeStr, dt) {
    if (!dt) {
        dt = new Date();
    }

    var time = timeStr.match(/(\d+)(?::(\d\d))?\s*(p?)/i);
    if (!time) {
        return NaN;
    }
    var hours = parseInt(time[1], 10);
    if (hours == 12 &amp;amp;&amp;amp; !time[3]) {
        hours = 0;
    }
    else {
        hours += (hours &amp;lt; 12 &amp;amp;&amp;amp; time[3]) ? 12 : 0;
    }

    dt.setHours(hours);
    dt.setMinutes(parseInt(time[2], 10) || 0);
    dt.setSeconds(0, 0);
    return dt;
}&lt;/pre&gt;
&lt;p&gt;This function will return NaN if it can’t parse the input at all.&amp;nbsp; The logic immediately following the match() call is to handle the noon/midnight case correctly.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Here’s a jsFiddle of this in action:&lt;/p&gt;

&lt;iframe width="100%" height="300" src="http://jsfiddle.net/xTn4Z/2/embedded/" allowfullscreen="allowfullscreen" frameborder="0"&gt;&lt;/iframe&gt;</description><pubDate>Wed, 31 Jul 2013 14:56:00 GMT</pubDate><guid isPermaLink="true">http://timlabonne.com:80/2013/07/parsing-a-time-string-with-javascript/</guid></item><item><title>Fun with Statistics Calculations</title><link>http://timlabonne.com:80/2013/07/fun-with-statistics-calculations/</link><description>&lt;p&gt;A while back I was working on a system where we were going to score work items to measure risk of auditing.&amp;nbsp; Higher numbers would most likely result in an audit while lower numbers would pass.&amp;nbsp; The exact mechanism of measuring the risk is immaterial for this post, so we’ll treat it as a black box number.&amp;nbsp; Furthermore, we calculate the risk on all work items but only update our statistics (as described below) on work items that actually &lt;em&gt;did&lt;/em&gt; get audited.&lt;/p&gt; &lt;p&gt;I wanted to know whether the audit score for a particular work item was very far away from the mean or very under the mean.&amp;nbsp; If it was low, the audit risk should be low and vice versa.&amp;nbsp; What we are looking for here is a “sigma” level – or a number that indicates how far away from the mean something is.&amp;nbsp; If something has a sigma level of zero, it means it is equal to the mean.&amp;nbsp; If it has a sigma level of 1, it means it is 1 standard deviation above the mean.&amp;nbsp; -1 means that it is one standard deviation below the mean.&amp;nbsp; Lower levels of sigma are generally better than higher ones in this system.&amp;nbsp; In normalized data, we’d expect over two-thirds of the work items to score within +/- 1 sigma.&amp;nbsp; A sigma number of 6 or higher means that the score would be a very large outlier.&lt;/p&gt; &lt;p&gt;To calculate this sigma value, we need two primary pieces of data – the mean and the standard deviation of the population or sample (i.e. the audit risk scores).&amp;nbsp; I did not want to calculate these values over the entire set of data each time I wanted to compute the sigma level – I just wanted to add it to the previous mean and standard deviation to make calculations really fast.&lt;/p&gt; &lt;p&gt;Let’s start with the mean.&amp;nbsp; If we save the number of data points used (n) and the previous mean calculated (ca), we can derive the new mean given a new data point (x) with the following formula:&lt;/p&gt; &lt;p&gt;new_mean = (x + n * ca) / (n + 1)&lt;/p&gt; &lt;p&gt;Or in C#:&lt;/p&gt;&lt;pre class="brush: js;"&gt;public static double CalculateNewCumulativeAverage(int n, int x, double ca)
{
    return (x + n * ca) / (n + 1);
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The standard deviation calculation is a little harder.&amp;nbsp; The Wikipedia article at &lt;a title="http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods" href="http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods"&gt;http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods&lt;/a&gt; describes a method for rapid calculation that requires you to only provide the following variables to compute a new standard deviation given a new data point (x): n – the number of previous data points used, s1 – sum of all previous x’s, and s2 – sum of all previous x^2 (squared).&amp;nbsp; Here’s the formula in C#:&lt;/p&gt;&lt;pre class="brush: js;"&gt;public static double CalculateNewStandardDeviation(int n, int x, int s1, long s2)
{
    if (n == 0)
        return double.NaN;
    s1 += x;
    s2 += x * x;
    double num = (n + 1) * s2 - (s1 * s1);
    double denom = (n + 1) * n;
    return Math.Sqrt(num / denom);
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This will be a very fast way of calculating standard deviation because you simply don’t have to go over all data points (which also means not reading values out of a database).&lt;/p&gt;
&lt;p&gt;The sigma value I talked about earlier can then be calculated given the data point (x), cumulative mean (ca) and standard deviation (s):&lt;/p&gt;&lt;pre class="brush: js;"&gt;public static double CalculateSigma(int x, double ca, double s)
{
    return (x - ca) / s;
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So all you will need to store in your database is the following scalar values to calculate these stats:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Total number of data points (n).&lt;/li&gt;
&lt;li&gt;Sum of all data points (s1).&lt;/li&gt;
&lt;li&gt;Sum of the squares of all data points (s2).&lt;/li&gt;
&lt;li&gt;Cumulative average or mean (ca).&lt;/li&gt;
&lt;li&gt;Current standard deviation (s).&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;To add a new data point (x) and update all variables to new values:&lt;/p&gt;&lt;pre class="brush: js;"&gt;public static void AddDataPoint(int x, ref int n, ref int s1, ref int s2, ref double ca, ref double s)
{
    ca = CalculateNewCumulativeAverage(n, x, ca);
    s = CalculateNewStandardDeviation(n, x, s1, s2);
    n += 1;
    s1 += x;
    s2 += x * x;
}
&lt;/pre&gt;</description><pubDate>Fri, 26 Jul 2013 17:47:00 GMT</pubDate><guid isPermaLink="true">http://timlabonne.com:80/2013/07/fun-with-statistics-calculations/</guid></item><item><title>Distributing Monetary Amounts in C#</title><link>http://timlabonne.com:80/2013/07/distributing-monetary-amounts-in-c/</link><description>&lt;p&gt;Many times in financial applications, you will be tasked with distributing or allocating monetary amounts across a set of ratios (say, a 50/50 split or maybe a 30/70 split).&amp;nbsp; This can be tricky getting the rounding right so that no pennies are lost in the allocation.&lt;/p&gt; &lt;p&gt;Here’s an example:&amp;nbsp; split $0.05 in a 30/70 ratio.&amp;nbsp; The 30% amount becomes $0.015 and the 70% will be $0.035.&amp;nbsp; Now in this case, they both add up to the original amount (which is an absolute requirement) but they must have a half penny each to accomplish this.&amp;nbsp; We can’t have half pennies in this situation, so something has to be done with the extra penny.&lt;/p&gt; &lt;p&gt;Now the specifics on where you allocate the extra penny are up to your business requirements, so the solution I present below may not be exactly what you need.&amp;nbsp; It should, however, give you an idea on how to do this split without losing the extra penny.&amp;nbsp; Here’s a static method that will take a single monetary amount (as a C# decimal) and an array of ratios.&amp;nbsp; These ratios are your allocation percentages.&amp;nbsp; They could be the set [0.30, 0.70] or even [30, 70].&amp;nbsp; They don’t even need to sum to 1 or 100, it doesn’t matter:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;public static decimal[] DistributeAmount(decimal amount, decimal[] ratios)
{
    decimal total = 0;
    decimal[] results = new decimal[ratios.Length];

    // find the total of the ratios
    for (int index = 0; index &amp;lt; ratios.Length; index++)
        total += ratios[index];

    // convert amount to a fixed point value (no mantissa portion)
    amount = amount * 100;
    decimal remainder = amount;
    for (int index = 0; index &amp;lt; results.Length; index++)
    {
        results[index] = Math.Floor(amount * ratios[index] / total);
        remainder -= results[index];
    }

    // allocate remainder across all amounts
    for (int index = 0; index &amp;lt; remainder; index++)
        results[index]++;

    // convert back to decimal portion
    for (int index = 0; index &amp;lt; results.Length; index++)
        results[index] = results[index] / 100;

    return results;
}

&lt;/pre&gt;
&lt;p&gt;(Another thing here is that I am assuming pennies and dollars, or at least a currency system that divides their unit of currency into hundredths – notice the multiply by 100.&amp;nbsp; You can change that for universal currency systems to support any currency).&lt;/p&gt;
&lt;p&gt;This code works by converting amounts to a fixed point value with no mantissa.&amp;nbsp; So in our example, $0.05 turns into 5.&amp;nbsp; We then iterate over each ratio and compute the amount to distribute using a simple division.&amp;nbsp; The trick here is the Math.Floor().&amp;nbsp; We round all half pennies down.&amp;nbsp; The half-penny will stay in the remainder variable.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;At the end of the distribution, we then distribute all of the fractional pennies that have built up evenly across the distributed amounts.&amp;nbsp; If there are no remaining pennies to distribute, it simply ends.&amp;nbsp; So in this implementation, the first ratios in the set tend to get the extra pennies and the last one loses out.&amp;nbsp; You can change this behavior to be almost anything you like (such as random, even-odd, or something else).&lt;/p&gt;
&lt;p&gt;At the very end, we convert back to a decimal portion by dividing each amount by 100.&lt;/p&gt;
&lt;p&gt;The final results for $0.05 would be { 0.02, 0.03 }, which adds up to 0.05.&lt;/p&gt;</description><pubDate>Tue, 16 Jul 2013 17:42:00 GMT</pubDate><guid isPermaLink="true">http://timlabonne.com:80/2013/07/distributing-monetary-amounts-in-c/</guid></item></channel></rss>