-
Notifications
You must be signed in to change notification settings - Fork 0
/
remedian.html
217 lines (210 loc) · 12.2 KB
/
remedian.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>remedian.py</title>
<link rel="stylesheet" href="pycco.css">
<meta name="thumbnail" content="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200" />
<link rel="shortcut icon" href="favicon.png" type="image/png">
</head>
<body>
<div id='container'>
<a href="https://github.com/ttv1/src/blob/master/remedian.py"><img style="z-index: 10; position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/567c3a48d796e2fc06ea80409cc9dd82bf714434/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png"></a>
<div id="background"></div>
<div class='section'>
<div class='docs'><h1> remedian.py</h1><p> This is ttv1 code (Timm tools, version 1).</p></div>
</div>
<div class='clearall'>
<div class='section' id='section-0'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-0'>#</a>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55634425-2', 'auto');
ga('send', 'pageview');
</script>
<p><img
src="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200"
align=left
width=120>
<br> <br>
<a href="http://ttv1.github.io">home</a> | <a href="#discussion">discuss</a> | <a href="https://github.com/ttv1/src/issues">report bug</a>
<br>
<script>
(function() {
var cx = '009630129455493240085:aja5uvdnjeo';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
<div style="width:0px;overflow:hidden;height:0px;"> <!-- if you use display:none here, it doesn't work-->
<gcse:search></gcse:search>
</div>
<form style="margin-left: 20px;" id="searchbox_009630129455493240085:aja5uvdnjeo" action="">
<input value="009630129455493240085:aja5uvdnjeo" name="cx" type="hidden"/>
<input value="FORID:11" name="cof" type="hidden"/>
<input id="q" style="" name="q" size="25" type="text"/>
<button class="btn">Search</button>
</form></p>
<p><br clear=all></p>
<hr />
<p>Watch over a stream of numbers, incrementally learning their median.</p>
<p>Implemented via nested lists. New numbers are added to <code>lst[i]</code> and
when it fills up, it posts its median to <code>lst[i+1]</code>. Wen <code>lst[i+1]</code>
fills up, it posts the medians of its medians to <code>lst[i+2]</code>. Etc.
When a remedian is queried for the current median, it returns the
median of the last list with any numbers.</p>
<p>This approach is quite space efficient . E.g. four nested lists,
each with 64 items, require memory for 4*64 items yet can hold the
median of the median of the median of the median of over 17 million
numbers.</p>
<p>Example usage:</p>
<pre><code> z=remedian()
for i in range(1000):
z + i
if not i % 100:
print(i, z.median())
</code></pre>
<p>Based on <a href="http://web.ipac.caltech.edu/staff/fmasci/home/astro_refs/Remedian.pdf">The Remedian:A Robust Averaging Method for Large Data
Sets</a>.
by Peter J. Rousseeuw and Gilbert W. Bassett Jr. Journal of the
American Statistical Association March 1990, Vol. 85, No. 409,
Theory and Methods</p>
<p>The code <a href="remedianeg.py">remedianeg.py</a> compares this rig to just
using Python's built-in sort then reporing the middle number.
Assuming lists of length 64 and use of pypy3:</p>
<ul>
<li>Remedian is getting nearly as fast (within 20%) as raw sort after 500 items;</li>
<li>While at the same time, avoids having to store all the numbers in RAM;</li>
<li>Further, remedian's computed median is within 1% (or less) of the medians found via Python's sort.</li>
</ul>
<hr />
<h2>Programmer's Guide</h2>
</div>
<div class='code'>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">median</span> <span class="kn">import</span> <span class="n">median</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-1'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-1'>#</a>
</div>
</div>
<div class='code'>
<div class="highlight"><pre><span class="k">class</span> <span class="nc">remedian</span><span class="p">:</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-2'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-2'>#</a>
</div>
<p>Initialization</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">inits</span><span class="o">=</span><span class="p">[],</span> <span class="n">k</span><span class="o">=</span><span class="mi">64</span><span class="p">):</span> <span class="c"># after some experimentation, 64 works ok</span>
<span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">,</span><span class="n">i</span><span class="o">.</span><span class="n">k</span> <span class="o">=</span> <span class="p">[],</span><span class="n">k</span>
<span class="n">i</span><span class="o">.</span><span class="n">more</span><span class="p">,</span><span class="n">i</span><span class="o">.</span><span class="n">_median</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="bp">None</span>
<span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inits</span><span class="p">]</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-3'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-3'>#</a>
</div>
<p>When full, push the median of current values to next list, then reset.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">x</span><span class="p">):</span>
<span class="n">i</span><span class="o">.</span><span class="n">_median</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">)</span> <span class="o">==</span> <span class="n">i</span><span class="o">.</span><span class="n">k</span><span class="p">:</span>
<span class="n">i</span><span class="o">.</span><span class="n">more</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">more</span> <span class="ow">or</span> <span class="n">remedian</span><span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="n">i</span><span class="o">.</span><span class="n">k</span><span class="p">)</span>
<span class="n">i</span><span class="o">.</span><span class="n">more</span> <span class="o">+</span> <span class="n">i</span><span class="o">.</span><span class="n">_medianPrim</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">)</span>
<span class="n">i</span><span class="o">.</span><span class="n">all</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># reset</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-4'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-4'>#</a>
</div>
<p>If there is a next list, ask its median. Else, work it out locally.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">def</span> <span class="nf">median</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">return</span> <span class="n">i</span><span class="o">.</span><span class="n">more</span><span class="o">.</span><span class="n">median</span><span class="p">()</span> <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">more</span> <span class="k">else</span> <span class="n">i</span><span class="o">.</span><span class="n">_medianPrim</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">all</span><span class="p">)</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-5'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-5'>#</a>
</div>
<hr />
<p><img align=right
src="https://raw.githubusercontent.com/timm/timm.github.io/master/timm.png"
width=170></p>
<h2>Copyleft</h2>
<p>Copyright © 2016,2017 Tim Menzies <a href="mailto:tim@menzies.us">tim@menzies.us</a>, MIT license v2.</p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<p>Share and enjoy.</p>
<hr>
<a name="discussion"><h2>Discussion</h2></a>
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = 'https://ttv1.github.ip/remedian.html'
this.page.identifier = 'remedian';
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//ttv1.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
</div>
<script id="dsq-count-scr" src="//ttv1.disqus.com/count.js" async></script>
</body>
</html>