normal_distribution 0.1.2 → 0.2.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 +4 -4
- data/ext/normal_distribution/confidence_interval.c +41 -0
- data/ext/normal_distribution/confidence_interval.h +8 -0
- data/ext/normal_distribution/erf_inv.h +1 -1
- data/ext/normal_distribution/model.c +111 -0
- data/ext/normal_distribution/model.h +10 -0
- data/ext/normal_distribution/normal_distribution.c +3 -103
- data/ext/normal_distribution/normal_distribution.h +4 -7
- data/lib/normal_distribution/confidence_interval.rb +32 -0
- data/lib/normal_distribution/model.rb +5 -3
- data/lib/normal_distribution/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e57048774793d77b8c736134cfe7a6f966c25e31
|
4
|
+
data.tar.gz: 69366dc447c98ecf257c02e5e4ee93dc9fa67540
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2731bf134df4c0c4e86ebfd8ece0d5877005523a133aac6e21c6ca94fb071d8be8b4a7443b5c2c84974fcc195cfdb656228892adc4dd8213f7fcbed1bc5acb45
|
7
|
+
data.tar.gz: 33acb859a3e228c8c041e40e4244a7ee052e9d81e836197f43c35af72560a05d759205c8b7195ba1ecb6352b64b083b7923b6c711565e3d93824c89f6bed8410
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#include "confidence_interval.h"
|
2
|
+
|
3
|
+
static VALUE t_init( VALUE self, VALUE lower_bound, VALUE upper_bound ) {
|
4
|
+
double lower = NUM2DBL( lower_bound );
|
5
|
+
double upper = NUM2DBL( upper_bound );
|
6
|
+
|
7
|
+
if ( lower > upper ) {
|
8
|
+
rb_raise( rb_eArgError, "lower bound must not be greater then upper bound" );
|
9
|
+
}
|
10
|
+
|
11
|
+
rb_iv_set( self, "@lower_bound", lower_bound );
|
12
|
+
rb_iv_set( self, "@upper_bound", upper_bound );
|
13
|
+
|
14
|
+
return self;
|
15
|
+
}
|
16
|
+
|
17
|
+
static VALUE t_attr_get_upper_bound( VALUE self ) {
|
18
|
+
return rb_iv_get( self, "@upper_bound" );
|
19
|
+
}
|
20
|
+
|
21
|
+
static VALUE t_attr_get_lower_bound( VALUE self ) {
|
22
|
+
return rb_iv_get( self, "@lower_bound" );
|
23
|
+
}
|
24
|
+
|
25
|
+
static VALUE t_include( VALUE self, VALUE value ) {
|
26
|
+
double lower = NUM2DBL( rb_iv_get( self, "@lower_bound" ) );
|
27
|
+
double upper = NUM2DBL( rb_iv_get( self, "@upper_bound" ) );
|
28
|
+
double v = NUM2DBL( value );
|
29
|
+
|
30
|
+
return v < lower || v > upper ? Qfalse : Qtrue;
|
31
|
+
}
|
32
|
+
|
33
|
+
void Init_confidence_interval( void ) {
|
34
|
+
VALUE rb_mNormalDistribution = rb_path2class( "NormalDistribution" );
|
35
|
+
VALUE rb_cConfidenceInterval = rb_define_class_under( rb_mNormalDistribution, "ConfidenceInterval", rb_cObject );
|
36
|
+
|
37
|
+
rb_define_method( rb_cConfidenceInterval, "initialize", t_init, 2 );
|
38
|
+
rb_define_method( rb_cConfidenceInterval, "lower_bound", t_attr_get_lower_bound, 0 );
|
39
|
+
rb_define_method( rb_cConfidenceInterval, "upper_bound", t_attr_get_upper_bound, 0 );
|
40
|
+
rb_define_method( rb_cConfidenceInterval, "include?", t_include, 1 );
|
41
|
+
}
|
@@ -0,0 +1,111 @@
|
|
1
|
+
#include "model.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
|
+
xfree( 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 rb_cConfidenceInterval = rb_path2class( "NormalDistribution::ConfidenceInterval" );
|
86
|
+
VALUE interval = rb_funcall(
|
87
|
+
rb_cConfidenceInterval, rb_intern( "new" ), 2,
|
88
|
+
rb_float_new( lower_bound ),
|
89
|
+
rb_float_new( upper_bound )
|
90
|
+
);
|
91
|
+
|
92
|
+
return interval;
|
93
|
+
}
|
94
|
+
|
95
|
+
static VALUE t_attr_mean( VALUE self ) {
|
96
|
+
return rb_iv_get( self, "@mean" );
|
97
|
+
}
|
98
|
+
|
99
|
+
static VALUE t_attr_stddev( VALUE self ) {
|
100
|
+
return rb_iv_get( self, "@standard_deviation" );
|
101
|
+
}
|
102
|
+
|
103
|
+
void Init_model( void ) {
|
104
|
+
VALUE rb_mNormalDistribution = rb_path2class( "NormalDistribution" );
|
105
|
+
VALUE rb_cModel = rb_define_class_under( rb_mNormalDistribution, "Model", rb_cObject );
|
106
|
+
|
107
|
+
rb_define_method( rb_cModel, "initialize", t_init, 1 );
|
108
|
+
rb_define_method( rb_cModel, "confidence_interval", t_confidence_interval, 1 );
|
109
|
+
rb_define_method( rb_cModel, "mean", t_attr_mean, 0 );
|
110
|
+
rb_define_method( rb_cModel, "standard_deviation", t_attr_stddev, 0 );
|
111
|
+
}
|
@@ -1,108 +1,8 @@
|
|
1
1
|
#include "normal_distribution.h"
|
2
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
3
|
void Init_normal_distribution( void ) {
|
101
|
-
|
102
|
-
VALUE model = rb_define_class_under( module, "Model", rb_cObject );
|
4
|
+
rb_define_module( "NormalDistribution" );
|
103
5
|
|
104
|
-
|
105
|
-
|
106
|
-
rb_define_method( model, "mean", t_attr_mean, 0 );
|
107
|
-
rb_define_method( model, "standard_deviation", t_attr_stddev, 0 );
|
6
|
+
Init_confidence_interval();
|
7
|
+
Init_model();
|
108
8
|
}
|
@@ -1,10 +1,7 @@
|
|
1
1
|
#ifndef NORMAL_DISTRIBUTION_H
|
2
|
-
#define NORMAL_DISTRIBUTION_H
|
2
|
+
#define NORMAL_DISTRIBUTION_H
|
3
3
|
|
4
|
-
#include
|
5
|
-
#include
|
6
|
-
#include <math.h>
|
7
|
-
#include "ruby.h"
|
8
|
-
#include "erf_inv.h"
|
4
|
+
#include "model.h"
|
5
|
+
#include "confidence_interval.h"
|
9
6
|
|
10
|
-
#endif
|
7
|
+
#endif //NORMAL_DISTRIBUTION_H
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NormalDistribution
|
4
|
+
# Confidence interval of normal distribution
|
5
|
+
#
|
6
|
+
# @since 0.2.0
|
7
|
+
class ConfidenceInterval
|
8
|
+
# @!parse [ruby]
|
9
|
+
#
|
10
|
+
# # @return [Float] lower bound of confidence interval
|
11
|
+
# attr_reader :lower_bound
|
12
|
+
#
|
13
|
+
# # @return [Float] upper bound of confidence interval
|
14
|
+
# attr_reader :upper_bound
|
15
|
+
#
|
16
|
+
# # Initializes confidence interval
|
17
|
+
# #
|
18
|
+
# # @param lower_bound [Numeric] lower bound of confidence interval
|
19
|
+
# # @param upper_bound [Numeric] upper bound of confidence interval
|
20
|
+
# def initialize(lower_bound, upper_bound)
|
21
|
+
# # This is stub used for indexing
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # Decides, whether value is from the interval
|
25
|
+
# #
|
26
|
+
# # @param value [Numeric] value to be compared with interval bounds
|
27
|
+
# # @return [Boolean] true if value is from the interval. Otherwise returns false.
|
28
|
+
# def include?(value)
|
29
|
+
# # This is stub used for indexing
|
30
|
+
# end
|
31
|
+
end
|
32
|
+
end
|
@@ -20,10 +20,12 @@ module NormalDistribution
|
|
20
20
|
# # This is stub used for indexing
|
21
21
|
# end
|
22
22
|
#
|
23
|
-
# # Calculates confidence interval for given percentage
|
23
|
+
# # Calculates confidence interval for given probability in percentage
|
24
24
|
# #
|
25
|
-
# # @param percentage [Numeric] a number in interval <0, 100> representing probability
|
26
|
-
# # @return [
|
25
|
+
# # @param percentage [Numeric] a number in interval <0, 100> representing probability in percentage
|
26
|
+
# # @return [ConfidenceInterval] an instance of ConfidenceInterval class
|
27
|
+
# #
|
28
|
+
# # @since 0.2.0
|
27
29
|
# def confidence_interval(percentage)
|
28
30
|
# # This is stub used for indexing
|
29
31
|
# end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: normal_distribution
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dominik Čermák
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -104,12 +104,17 @@ extensions:
|
|
104
104
|
- ext/normal_distribution/extconf.rb
|
105
105
|
extra_rdoc_files: []
|
106
106
|
files:
|
107
|
+
- ext/normal_distribution/confidence_interval.c
|
108
|
+
- ext/normal_distribution/confidence_interval.h
|
107
109
|
- ext/normal_distribution/erf_inv.c
|
108
110
|
- ext/normal_distribution/erf_inv.h
|
109
111
|
- ext/normal_distribution/extconf.rb
|
112
|
+
- ext/normal_distribution/model.c
|
113
|
+
- ext/normal_distribution/model.h
|
110
114
|
- ext/normal_distribution/normal_distribution.c
|
111
115
|
- ext/normal_distribution/normal_distribution.h
|
112
116
|
- lib/normal_distribution.rb
|
117
|
+
- lib/normal_distribution/confidence_interval.rb
|
113
118
|
- lib/normal_distribution/model.rb
|
114
119
|
- lib/normal_distribution/version.rb
|
115
120
|
homepage: https://github.com/domcermak/normal_distribution
|