normal_distribution 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/normal_distribution/erf_inv.c +91 -0
- data/ext/normal_distribution/erf_inv.h +12 -0
- data/ext/normal_distribution/extconf.rb +5 -0
- data/ext/normal_distribution/normal_distribution.c +108 -0
- data/ext/normal_distribution/normal_distribution.h +10 -0
- data/lib/normal_distribution.rb +7 -0
- data/lib/normal_distribution/model.rb +21 -0
- data/lib/normal_distribution/version.rb +5 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 662e907768609223b84adda82f20f9dbba538b2b
|
4
|
+
data.tar.gz: d8b1598c480018ae67999e987be9d18d65340e00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c99cc756d443cc770d661f9c841be2c33c5526625ac53c26c959bcb725b6908ddfc8ef522221810db9dda39641cf4653ff49ce9de0c9d59e0e66e2468de6b6d6
|
7
|
+
data.tar.gz: 258308f35c9436adda2e0023c2f0dcb540014b2e0ffd4f482e8f4e12adfc5a5855f9bb0b3d6216865757bcea36c060435593fa6b068ef44323a5f780fd63ffb9
|
@@ -0,0 +1,91 @@
|
|
1
|
+
#include "erf_inv.h"
|
2
|
+
|
3
|
+
long double t_erf_inv(long double x) {
|
4
|
+
if (x < -1 || x > 1) {
|
5
|
+
return NAN;
|
6
|
+
} else if (x == 1.0) {
|
7
|
+
return INFINITY;
|
8
|
+
} else if (x == -1.0) {
|
9
|
+
return -INFINITY;
|
10
|
+
}
|
11
|
+
|
12
|
+
const long double LN2 = 6.931471805599453094172321214581e-1L;
|
13
|
+
|
14
|
+
const long double A0 = 1.1975323115670912564578e0L;
|
15
|
+
const long double A1 = 4.7072688112383978012285e1L;
|
16
|
+
const long double A2 = 6.9706266534389598238465e2L;
|
17
|
+
const long double A3 = 4.8548868893843886794648e3L;
|
18
|
+
const long double A4 = 1.6235862515167575384252e4L;
|
19
|
+
const long double A5 = 2.3782041382114385731252e4L;
|
20
|
+
const long double A6 = 1.1819493347062294404278e4L;
|
21
|
+
const long double A7 = 8.8709406962545514830200e2L;
|
22
|
+
|
23
|
+
const long double B0 = 1.0000000000000000000e0L;
|
24
|
+
const long double B1 = 4.2313330701600911252e1L;
|
25
|
+
const long double B2 = 6.8718700749205790830e2L;
|
26
|
+
const long double B3 = 5.3941960214247511077e3L;
|
27
|
+
const long double B4 = 2.1213794301586595867e4L;
|
28
|
+
const long double B5 = 3.9307895800092710610e4L;
|
29
|
+
const long double B6 = 2.8729085735721942674e4L;
|
30
|
+
const long double B7 = 5.2264952788528545610e3L;
|
31
|
+
|
32
|
+
const long double C0 = 1.42343711074968357734e0L;
|
33
|
+
const long double C1 = 4.63033784615654529590e0L;
|
34
|
+
const long double C2 = 5.76949722146069140550e0L;
|
35
|
+
const long double C3 = 3.64784832476320460504e0L;
|
36
|
+
const long double C4 = 1.27045825245236838258e0L;
|
37
|
+
const long double C5 = 2.41780725177450611770e-1L;
|
38
|
+
const long double C6 = 2.27238449892691845833e-2L;
|
39
|
+
const long double C7 = 7.74545014278341407640e-4L;
|
40
|
+
|
41
|
+
const long double D0 = 1.4142135623730950488016887e0L;
|
42
|
+
const long double D1 = 2.9036514445419946173133295e0L;
|
43
|
+
const long double D2 = 2.3707661626024532365971225e0L;
|
44
|
+
const long double D3 = 9.7547832001787427186894837e-1L;
|
45
|
+
const long double D4 = 2.0945065210512749128288442e-1L;
|
46
|
+
const long double D5 = 2.1494160384252876777097297e-2L;
|
47
|
+
const long double D6 = 7.7441459065157709165577218e-4L;
|
48
|
+
const long double D7 = 1.4859850019840355905497876e-9L;
|
49
|
+
|
50
|
+
const long double E0 = 6.65790464350110377720e0L;
|
51
|
+
const long double E1 = 5.46378491116411436990e0L;
|
52
|
+
const long double E2 = 1.78482653991729133580e0L;
|
53
|
+
const long double E3 = 2.96560571828504891230e-1L;
|
54
|
+
const long double E4 = 2.65321895265761230930e-2L;
|
55
|
+
const long double E5 = 1.24266094738807843860e-3L;
|
56
|
+
const long double E6 = 2.71155556874348757815e-5L;
|
57
|
+
const long double E7 = 2.01033439929228813265e-7L;
|
58
|
+
|
59
|
+
const long double F0 = 1.414213562373095048801689e0L;
|
60
|
+
const long double F1 = 8.482908416595164588112026e-1L;
|
61
|
+
const long double F2 = 1.936480946950659106176712e-1L;
|
62
|
+
const long double F3 = 2.103693768272068968719679e-2L;
|
63
|
+
const long double F4 = 1.112800997078859844711555e-3L;
|
64
|
+
const long double F5 = 2.611088405080593625138020e-5L;
|
65
|
+
const long double F6 = 2.010321207683943062279931e-7L;
|
66
|
+
const long double F7 = 2.891024605872965461538222e-15L;
|
67
|
+
|
68
|
+
long double abs_x = fabsl(x);
|
69
|
+
|
70
|
+
if (abs_x <= 0.85L) {
|
71
|
+
long double r = 0.180625L - 0.25L * x * x;
|
72
|
+
long double num = (((((((A7 * r + A6) * r + A5) * r + A4) * r + A3) * r + A2) * r + A1) * r + A0);
|
73
|
+
long double den = (((((((B7 * r + B6) * r + B5) * r + B4) * r + B3) * r + B2) * r + B1) * r + B0);
|
74
|
+
return x * num / den;
|
75
|
+
}
|
76
|
+
|
77
|
+
long double r = sqrtl(LN2 - logl(1.0L - abs_x));
|
78
|
+
|
79
|
+
long double num, den;
|
80
|
+
if (r <= 5.0L) {
|
81
|
+
r = r - 1.6L;
|
82
|
+
num = (((((((C7 * r + C6) * r + C5) * r + C4) * r + C3) * r + C2) * r + C1) * r + C0);
|
83
|
+
den = (((((((D7 * r + D6) * r + D5) * r + D4) * r + D3) * r + D2) * r + D1) * r + D0);
|
84
|
+
} else {
|
85
|
+
r = r - 5.0L;
|
86
|
+
num = (((((((E7 * r + E6) * r + E5) * r + E4) * r + E3) * r + E2) * r + E1) * r + E0);
|
87
|
+
den = (((((((F7 * r + F6) * r + F5) * r + F4) * r + F3) * r + F2) * r + F1) * r + F0);
|
88
|
+
}
|
89
|
+
|
90
|
+
return copysignl(num / den, x);
|
91
|
+
}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
#include "normal_distribution.h"
|
2
|
+
|
3
|
+
static double t_parse_percentage(VALUE percentage) {
|
4
|
+
double perc = NUM2DBL( percentage );
|
5
|
+
|
6
|
+
if (perc > 100 || perc < 0) {
|
7
|
+
rb_raise(rb_eArgError, "percentage must be between 0 and 100");
|
8
|
+
}
|
9
|
+
|
10
|
+
return perc;
|
11
|
+
}
|
12
|
+
|
13
|
+
static double t_mean( double * data, long size ) {
|
14
|
+
double sum = .0;
|
15
|
+
|
16
|
+
for ( long i = 0 ; i < size ; ++ i ) {
|
17
|
+
sum += data[i];
|
18
|
+
}
|
19
|
+
|
20
|
+
return sum / size;
|
21
|
+
}
|
22
|
+
|
23
|
+
static double t_z_score( double percentage ) {
|
24
|
+
return sqrt( 2 ) * t_erf_inv( percentage / 100 );
|
25
|
+
}
|
26
|
+
|
27
|
+
static double t_variance( double * data, long size, double mean ) {
|
28
|
+
double * squared_diff = ALLOC_N( double, size );
|
29
|
+
|
30
|
+
for ( long i = 0 ; i < size ; ++ i ) {
|
31
|
+
squared_diff[i] = pow( mean - data[i], 2 );
|
32
|
+
}
|
33
|
+
|
34
|
+
double variance = t_mean( squared_diff, size );
|
35
|
+
free( squared_diff );
|
36
|
+
|
37
|
+
return variance;
|
38
|
+
}
|
39
|
+
|
40
|
+
static double t_stddev( double * data, long size, double mean ) {
|
41
|
+
return sqrt( t_variance( data, size, mean ) );
|
42
|
+
}
|
43
|
+
|
44
|
+
static double * t_parse_dbl_ary( VALUE ary, long * size ) {
|
45
|
+
Check_Type(ary, T_ARRAY);
|
46
|
+
long len = RARRAY_LEN( ary );
|
47
|
+
|
48
|
+
if (len == 0) {
|
49
|
+
rb_raise(rb_eArgError, "data must not be empty");
|
50
|
+
}
|
51
|
+
|
52
|
+
VALUE * values = RARRAY_PTR( ary );
|
53
|
+
double * d_data = ALLOC_N( double, len );
|
54
|
+
|
55
|
+
for ( int i = 0 ; i < len ; ++ i ) {
|
56
|
+
d_data[i] = NUM2DBL( values[i] );
|
57
|
+
}
|
58
|
+
|
59
|
+
*size = len;
|
60
|
+
|
61
|
+
return d_data;
|
62
|
+
}
|
63
|
+
|
64
|
+
static VALUE t_init( VALUE self, VALUE values ) {
|
65
|
+
long size;
|
66
|
+
double * data = t_parse_dbl_ary( values, &size );
|
67
|
+
double mean = t_mean( data, size );
|
68
|
+
double stddev = t_stddev( data, size, mean );
|
69
|
+
|
70
|
+
rb_iv_set( self, "@mean", rb_float_new( mean ) );
|
71
|
+
rb_iv_set( self, "@standard_deviation", rb_float_new( stddev ) );
|
72
|
+
free( data );
|
73
|
+
|
74
|
+
return self;
|
75
|
+
}
|
76
|
+
|
77
|
+
static VALUE t_confidence_interval( VALUE self, VALUE percentage ) {
|
78
|
+
double perc = t_parse_percentage( percentage );
|
79
|
+
double z = t_z_score( perc );
|
80
|
+
double stddev = NUM2DBL( rb_iv_get( self, "@standard_deviation" ) );
|
81
|
+
double mean = NUM2DBL( rb_iv_get( self, "@mean" ) );
|
82
|
+
double lower_bound = - z * stddev + mean;
|
83
|
+
double upper_bound = z * stddev + mean;
|
84
|
+
|
85
|
+
VALUE pair = rb_ary_new();
|
86
|
+
rb_ary_push( pair, rb_float_new( lower_bound ) );
|
87
|
+
rb_ary_push( pair, rb_float_new( upper_bound ) );
|
88
|
+
|
89
|
+
return pair;
|
90
|
+
}
|
91
|
+
|
92
|
+
static VALUE t_attr_mean( VALUE self ) {
|
93
|
+
return rb_iv_get( self, "@mean" );
|
94
|
+
}
|
95
|
+
|
96
|
+
static VALUE t_attr_stddev( VALUE self ) {
|
97
|
+
return rb_iv_get( self, "@standard_deviation" );
|
98
|
+
}
|
99
|
+
|
100
|
+
void Init_normal_distribution( void ) {
|
101
|
+
VALUE module = rb_define_module( "NormalDistribution" );
|
102
|
+
VALUE model = rb_define_class_under( module, "Model", rb_cObject );
|
103
|
+
|
104
|
+
rb_define_method( model, "initialize", t_init, 1 );
|
105
|
+
rb_define_method( model, "confidence_interval", t_confidence_interval, 1 );
|
106
|
+
rb_define_method( model, "mean", t_attr_mean, 0 );
|
107
|
+
rb_define_method( model, "standard_deviation", t_attr_stddev, 0 );
|
108
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NormalDistribution
|
4
|
+
class Model
|
5
|
+
# :nodoc:
|
6
|
+
# attr_reader :mean
|
7
|
+
|
8
|
+
# :nodoc:
|
9
|
+
# attr_reader :standard_deviation
|
10
|
+
|
11
|
+
# Initializes normal distribution from given data
|
12
|
+
# Params:
|
13
|
+
# +values+:: Non-empty array of numbers representing data for normal distribution construction
|
14
|
+
# def initialize(values); end
|
15
|
+
|
16
|
+
# Creates confidence interval for given percentage
|
17
|
+
# Params:
|
18
|
+
# +percentage+:: Number in interval <0, 100>
|
19
|
+
# def confidence_interval(percentage); end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normal_distribution
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dominik Čermák
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-16 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-benchmark
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- domcermak64@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/normal_distribution/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ext/normal_distribution/erf_inv.c
|
92
|
+
- ext/normal_distribution/erf_inv.h
|
93
|
+
- ext/normal_distribution/extconf.rb
|
94
|
+
- ext/normal_distribution/normal_distribution.c
|
95
|
+
- ext/normal_distribution/normal_distribution.h
|
96
|
+
- lib/normal_distribution.rb
|
97
|
+
- lib/normal_distribution/model.rb
|
98
|
+
- lib/normal_distribution/version.rb
|
99
|
+
homepage:
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
- ext
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.6.14
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: This gem provides library to detect anomalies in numerical data via normal
|
124
|
+
distribution
|
125
|
+
test_files: []
|