title: Algorithmic Stock Trading - how to efficiently process an event-stream for updates of [ Mean, StDev, Median ] values?
body: <p>I have implemented a simplified algorithm that maintains stock statistics from a stream of incoming data. The goal is to use efficient algorithms to compute simple statistics, <strong><code>mean</code>, <code>variance</code> and <code>median</code></strong>. The program will only maintain one value for each company, hence new values for a previously rated company should replace the previous value. Companies may also be removed from the current list, with a <code>delete</code> command. The input must be something like this: </p>
  
  <pre><code>A 111.12 Apple_Inc
  A 745.29 Alphabet_Inc
  A 17.10 Twitter_Inc
  S
  A 101.33 Apple_Inc
  S
  A 112.55 Facebook_Inc
  S
  D Apple_Inc
  S
  </code></pre>
  
  <p>"<strong><code>A</code></strong>" means to input data,<br> "<strong><code>D</code></strong>" means to <code>delete</code> data and when asked for<br>"<strong><code>S</code></strong>", the program should give us this output: <sub>( <code>avg</code> is average, <code>s</code> is a standard deviation, <code>m</code> is a median )</sub></p>
  
  <pre><code>avg 291.17
  s 156878.67
  m 111.12 Apple_Inc
  avg 287.91
  s 158673.31
  m 101.33 Apple_Inc
  avg 244.07
  s 113469.70
  m 112.55 Facebook_Inc
  avg 291.65
  s 156621.88
  m 112.55 Facebook_Inc
  </code></pre>
  
  <p>Just as a note, when we have to compute the median, if the number of companies is odd, it should return the bigger number in the middle with the respective name of  company.</p>
  
  <p>The statistic values should always be updated instead making new ones, as long as we insert, delete or add new values.</p>