title: C# LINQ - Center a distribution around zero
body: <p>Apologies, due to my poor understanding of the question, please find below a redrafted question.</p>
  
  <p>Using C# LINQ, is it possible to return the distribution of dataset <strong>and center the distribution around zero</strong>? </p>
  
  <p>Please find below code which returns the cumulative density function (cdf) based on an input dataSet.  In the resulting cdf, the percentile represents the middle of the "bucket" (as opposed to the top or bottom border of the "bucket"). This means when dataSet contains:</p>
  
  <ul>
  <li>an odd number of elements {1,2,3,4,5}, the percentile 0.5 will occur at the
  center value.<a href="https://i.stack.imgur.com/0IaRE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0IaRE.png" alt="odd"></a></li>
  <li>an even number of elements {1,2,3,4}, the percentile 0.5 can be
  interpolated between the two center values (upper and lower).</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/sFd7J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sFd7J.png" alt="even"></a></p>
  
  <p>Code:</p>
  
  <pre><code>void Main()
  {
      var dataSet = new List&lt;double&gt; {-5,-4,-3,-2,-1,0,1,2,3};
      var dist = new Distribution(dataSet);
      dist.Cdf.Dump();
  }
  
  public class Distribution
  {
     public int Count { get; set; }
     public double Max { get; set; }
     public double Min { get; set; }
     public IOrderedEnumerable&lt;VP&gt; Cdf { get; set; }
  
     public Distribution(IEnumerable&lt;double&gt; series)
     {
         Count = series.Count();
         Max = series.Max();
         Min = series.Min();
         Cdf = Populate(series);
     }
  
     private IOrderedEnumerable&lt;VP&gt; Populate(IEnumerable&lt;double&gt; source)
     {
         return source.AsParallel().ToLookup(k =&gt; k, k =&gt; source.Where(n =&gt; n &lt; k))
             .Select(k =&gt; new VP
             {
                 Value = k.Key,
                 Percentile = Percentile(source, k)
             })
             .OrderBy(k =&gt; k.Value);
     }
  
     private double Percentile(IEnumerable&lt;double&gt; source, IGrouping&lt;double, IEnumerable&lt;double&gt;&gt; k)
     {
         return (k.First().Count() + 0.5 * k.Count()) / source.Count();
     }
  }
  
  public struct VP
  {
      public double Value;
      public double Percentile;
  }
  </code></pre>
  
  <p>The above code returns the distribution:</p>
  
  <p><a href="https://i.stack.imgur.com/Fl6dc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fl6dc.png" alt="Actual"></a></p>
  
  <p>With an odd number of elements, it shows the 0.5 percentile at the center, which is correct.   However, in this case I am trying to center the distribution around zero, so: </p>
  
  <ul>
  <li>negative values are less than 0.5,  </li>
  <li>zero occurs at 0.5, and </li>
  <li>positive are greater than 0.5.</li>
  </ul>
  
  <p>Is it possible?  Please assist. </p>