stat_sugar 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/stat_sugar.rb +7 -0
- data/lib/stat_sugar/array.rb +63 -0
- data/lib/stat_sugar/numbers.rb +22 -0
- data/lib/stat_sugar/version.rb +3 -0
- data/stat_sugar.gemspec +23 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 749c3e4be45d3a022941d3a87d48521025a9478e
|
4
|
+
data.tar.gz: ce1b058fe6a64ab9808aa836e411dec74527ebfd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91805c6525ad855fc9c204f704ae7438a1c09ce39dd825b4cab6ac98de69ce0da2b21fa5b9b1ca212f2ed125946ac2306225880c5c47a7d8f8030c70bf2cede9
|
7
|
+
data.tar.gz: 3639b1dd1d76d702ed1f99885c71714e34b7327de6cd5da45375509a4b8eb22621af5dbc215765688119234e3150c49b7f0be294ed4713881491820b1404ec8b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# StatSugar
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stat_sugar`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'stat_sugar'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install stat_sugar
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/stat_sugar/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "stat_sugar"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/stat_sugar.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def sum
|
4
|
+
self.inject(:+).to_f
|
5
|
+
end
|
6
|
+
|
7
|
+
def mean
|
8
|
+
(self.sum.to_f / self.size).to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
def average
|
12
|
+
mean
|
13
|
+
end
|
14
|
+
|
15
|
+
def moving(index, distance = 10)
|
16
|
+
moving = [self[index]] ; down = index - 1 ; up = index + 1
|
17
|
+
distance.times { moving << self[down] if self[down] ; down -= 1 }
|
18
|
+
distance.times { moving << self[up] if self[up] ; up += 1 }
|
19
|
+
moving
|
20
|
+
end
|
21
|
+
|
22
|
+
def median
|
23
|
+
ary = self.sort ; len = ary.length
|
24
|
+
if ary.length % 2 == 0
|
25
|
+
l = (len / 2) - 1 ; h = len / 2
|
26
|
+
[ary[l], ary[h]].mean
|
27
|
+
else
|
28
|
+
i = ((len + 1) / 2) - 1
|
29
|
+
ary[i]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def percentile(input)
|
34
|
+
val = self.sort
|
35
|
+
k = (input * (val.size - 1) + 1).floor - 1
|
36
|
+
f = (input * (val.size - 1) + 1).modulo(1)
|
37
|
+
val[k] + (f * (val[k + 1] - val[k]))
|
38
|
+
end
|
39
|
+
|
40
|
+
def mode
|
41
|
+
self.frequencies.max_by{ |k,v| v }.to_f
|
42
|
+
end
|
43
|
+
|
44
|
+
def frequencies
|
45
|
+
self.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
|
46
|
+
end
|
47
|
+
|
48
|
+
def stnd_dev
|
49
|
+
avg = self.average
|
50
|
+
dev = self.map { |a| (a - avg) ** 2 }.sum / self.size
|
51
|
+
Math.sqrt(dev).to_f
|
52
|
+
end
|
53
|
+
|
54
|
+
def volatility
|
55
|
+
self.stnd_dev
|
56
|
+
end
|
57
|
+
|
58
|
+
def variance
|
59
|
+
mean = self.mean
|
60
|
+
self.map { |sample| (mean - sample) ** 2 }.reduce(:+) / self.size
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Fixnum
|
2
|
+
|
3
|
+
def pad(number,round = 20)
|
4
|
+
num = self.round(round)
|
5
|
+
"#{" " * (number - num.to_s.length)}#{num}"
|
6
|
+
end
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
class Float
|
11
|
+
|
12
|
+
def pad(number,round = 20)
|
13
|
+
num = self.round(round)
|
14
|
+
"#{" " * (number - num.to_s.length)}#{num}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def approx_equal?(float, options = {})
|
18
|
+
tolerance = options[:with_tolerance] || 0.1
|
19
|
+
self == float || (float >= self - (self * tolerance) && float <= self + (self * tolerance))
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/stat_sugar.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stat_sugar/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "stat_sugar"
|
8
|
+
spec.version = StatSugar::VERSION
|
9
|
+
spec.authors = ["Colin Walker"]
|
10
|
+
spec.email = ["cjwalker@sfu.ca"]
|
11
|
+
|
12
|
+
spec.summary = "Adds a little statistic sugar to Ruby."
|
13
|
+
spec.description = "Get methods like sum, average, standard deviation and more applied to arrays."
|
14
|
+
spec.homepage = "http://www.colinw.info"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stat_sugar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Walker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Get methods like sum, average, standard deviation and more applied to
|
42
|
+
arrays.
|
43
|
+
email:
|
44
|
+
- cjwalker@sfu.ca
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/stat_sugar.rb
|
58
|
+
- lib/stat_sugar/array.rb
|
59
|
+
- lib/stat_sugar/numbers.rb
|
60
|
+
- lib/stat_sugar/version.rb
|
61
|
+
- stat_sugar.gemspec
|
62
|
+
homepage: http://www.colinw.info
|
63
|
+
licenses: []
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.4.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Adds a little statistic sugar to Ruby.
|
85
|
+
test_files: []
|