sooth 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d52d3347ee6806fdb75d4ede666c0c6571fb8f32393f459765b45660adc79e13
4
- data.tar.gz: 2571b62383a6751b6709814b262779d73e3259853644856f3ff20d34f44f7698
3
+ metadata.gz: b9781b968536658207c18020248b92095f274aeb01b66f8a2b161b63fe355792
4
+ data.tar.gz: 17c1f18642aade6f2ab2906799f5c5a85cb4b0e1f9c674ae8ed5a071a047640a
5
5
  SHA512:
6
- metadata.gz: 3069f1da2d2bbdb6a856704503be13b92f1998d1c8fda7cdfc75fe0ca8c700d67c47de97c0590425dbd83a78b895ab0fa45dd576851e492ec3320c2454b03de1
7
- data.tar.gz: dac4c0bad6f63069b59e9665e33a0cae791271d649c507da0e19811e2841d42fac4c361bdc6915b0320897ea9d50d4b37046a90991b00e8882f1463c2b2b7c56
6
+ metadata.gz: f9e0fcf793b1d09119952574a1731551c55b9b3aa7b1e1b7a4edb53ea11af319fa643c52dfbf89d23bfc8cfd1a92841200fcc3a7940b945dc8a7a2744f9aa415
7
+ data.tar.gz: 0f59f8ee384cd5ddedf6450c6b3b85f123428737dbd411a6ce982331e949eefc4b868d8da084cab6245ac32d0cea53ae4d88a778ccec90bc2a8531ff5d16ec4f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.2
1
+ 2.2.0
@@ -43,7 +43,10 @@ void method_sooth_native_deallocate(void * predictor);
43
43
  * def uncertainty(context)
44
44
  * # (native code)
45
45
  * end
46
- * def surprise(context, evemt)
46
+ * def surprise(context, event)
47
+ * # (native code)
48
+ * end
49
+ * def frequency(context, event)
47
50
  * # (native code)
48
51
  * end
49
52
  * end
@@ -159,6 +162,16 @@ VALUE method_sooth_native_uncertainty(VALUE self, VALUE context);
159
162
  */
160
163
  VALUE method_sooth_native_surprise(VALUE self, VALUE bigram, VALUE limit);
161
164
 
165
+ /*
166
+ * Return a number indicating the frequency that the event has been observed
167
+ * within the given context.
168
+ *
169
+ * @param [Fixnum] context A number that provides a context for observations.
170
+ * @param [Fixnum] event A number representing the observed event.
171
+ * @return [Float] The frequency, as a number between 0.0 and 1.0
172
+ */
173
+ VALUE method_sooth_native_frequency(VALUE self, VALUE bigram, VALUE limit);
174
+
162
175
  //------------------------------------------------------------------------------
163
176
 
164
177
  void Init_sooth_native()
@@ -181,6 +194,7 @@ void Init_sooth_native()
181
194
  rb_define_method(SoothNative, "distribution", method_sooth_native_distribution, 1);
182
195
  rb_define_method(SoothNative, "uncertainty", method_sooth_native_uncertainty, 1);
183
196
  rb_define_method(SoothNative, "surprise", method_sooth_native_surprise, 2);
197
+ rb_define_method(SoothNative, "frequency", method_sooth_native_frequency, 2);
184
198
  }
185
199
 
186
200
  //------------------------------------------------------------------------------
@@ -368,4 +382,16 @@ method_sooth_native_surprise(VALUE self, VALUE context, VALUE event)
368
382
  return DBL2NUM(surprise);
369
383
  }
370
384
 
385
+ //------------------------------------------------------------------------------
386
+
387
+ VALUE
388
+ method_sooth_native_frequency(VALUE self, VALUE context, VALUE event)
389
+ {
390
+ SoothPredictor * predictor = NULL;
391
+ Check_Type(context, T_FIXNUM);
392
+ Check_Type(event, T_FIXNUM);
393
+ Data_Get_Struct(self, SoothPredictor, predictor);
394
+ return DBL2NUM(sooth_predictor_frequency(predictor, NUM2UINT(context), NUM2UINT(event)));
395
+ }
396
+
371
397
  //==============================================================================
@@ -406,4 +406,26 @@ sooth_predictor_surprise(SoothPredictor * predictor, uint32_t id, uint32_t event
406
406
  return -log2(frequency);
407
407
  }
408
408
 
409
+ //------------------------------------------------------------------------------
410
+
411
+ double
412
+ sooth_predictor_frequency(SoothPredictor * predictor, uint32_t id, uint32_t event)
413
+ {
414
+ SoothContext * context = sooth_predictor_find_context(predictor, id);
415
+
416
+ if (context == NULL || context->count == 0)
417
+ {
418
+ return 0.0;
419
+ }
420
+
421
+ SoothStatistic * statistic = sooth_predictor_find_statistic(context, event);
422
+
423
+ if (statistic == NULL || statistic->count == 0)
424
+ {
425
+ return 0.0;
426
+ }
427
+
428
+ return (double)statistic->count / (double)context->count;
429
+ }
430
+
409
431
  //==============================================================================
@@ -30,6 +30,7 @@ uint32_t sooth_predictor_select(SoothPredictor * predictor, uint32_t id, uint32_
30
30
  SoothStatistic * sooth_predictor_distribution(SoothPredictor * predictor, uint32_t id);
31
31
  double sooth_predictor_uncertainty(SoothPredictor * predictor, uint32_t id);
32
32
  double sooth_predictor_surprise(SoothPredictor * predictor, uint32_t id, uint32_t symbol);
33
+ double sooth_predictor_frequency(SoothPredictor * predictor, uint32_t id, uint32_t symbol);
33
34
 
34
35
  //==============================================================================
35
36
 
@@ -2,17 +2,17 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: sooth 2.1.2 ruby lib
5
+ # stub: sooth 2.2.0 ruby lib
6
6
  # stub: ext/sooth_native/extconf.rb
7
7
 
8
8
  Gem::Specification.new do |s|
9
9
  s.name = "sooth".freeze
10
- s.version = "2.1.2"
10
+ s.version = "2.2.0"
11
11
 
12
12
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
13
13
  s.require_paths = ["lib".freeze]
14
14
  s.authors = ["Jason Hutchens".freeze]
15
- s.date = "2018-03-15"
15
+ s.date = "2018-03-19"
16
16
  s.description = "Sooth is a simple stochastic predictive model.".freeze
17
17
  s.email = "jasonhutchens@gmail.com".freeze
18
18
  s.extensions = ["ext/sooth_native/extconf.rb".freeze]
@@ -213,4 +213,25 @@ describe Sooth::Predictor do
213
213
  expect(predictor.surprise(1, 3)).to eq(8)
214
214
  end
215
215
  end
216
+
217
+ describe "#frequency" do
218
+ it "returns zero for a new context" do
219
+ expect(predictor.frequency(1, 3)).to eq(0)
220
+ end
221
+
222
+ it "returns zero for a new event" do
223
+ predictor.observe(1, 3)
224
+ expect(predictor.frequency(1, 4)).to eq(0)
225
+ end
226
+
227
+ it "is one for a lone event" do
228
+ predictor.observe(1, 3)
229
+ expect(predictor.frequency(1, 3)).to eq(1)
230
+ end
231
+
232
+ it "is uniform for a uniform distribution" do
233
+ (1..100).each { |i| predictor.observe(1, i) }
234
+ expect(predictor.frequency(1, 3)).to eq(0.01)
235
+ end
236
+ end
216
237
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sooth
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Hutchens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-15 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec