Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytools
Manage
Activity
Members
Labels
Plan
Issues
2
Issue boards
Milestones
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Klöckner
pytools
Commits
fbc81a9f
Commit
fbc81a9f
authored
16 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Fix VarianceAggregator.
parent
25e48eab
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/__init__.py
+36
-12
36 additions, 12 deletions
src/__init__.py
with
36 additions
and
12 deletions
src/__init__.py
+
36
−
12
View file @
fbc81a9f
...
...
@@ -485,22 +485,46 @@ def average(iterable):
class
VarianceAggregator
:
"""
Online variance calculator.
See http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
Adheres to pysqlite
'
s aggregate interface.
"""
def
__init__
(
self
,
entire_pop
):
self
.
n
=
0
self
.
mean
=
0
self
.
m2
=
0
self
.
entire_pop
=
entire_pop
def
step
(
self
,
x
):
self
.
n
+=
1
delta
=
x
-
self
.
mean
self
.
mean
+=
delta
/
self
.
n
self
.
m2
+=
delta
*
(
x
-
self
.
mean
)
def
finalize
(
self
):
if
self
.
entire_pop
:
if
self
.
n
==
0
:
return
None
else
:
return
self
.
m2
/
self
.
n
else
:
if
self
.
n
<=
1
:
return
None
else
:
return
self
.
m2
/
(
self
.
n
-
1
)
def
variance
(
iterable
,
entire_pop
):
# http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
n
=
0
mean
=
0
m2
=
0
v_comp
=
VarianceAggregator
(
entire_pop
)
for
x
in
iterable
:
n
+=
1
delta
=
x
-
mean
mean
+=
delta
/
n
m2
+=
delta
*
(
x
-
mean
)
v_comp
.
step
(
x
)
if
entire_pop
:
return
m2
/
n
else
:
return
m2
/
(
n
-
1
)
return
v_comp
.
finalize
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment