Skip to content

Instantly share code, notes, and snippets.

@daltonrenaldo
Created October 1, 2015 22:11
Show Gist options
  • Save daltonrenaldo/c418061c865dd6c2d580 to your computer and use it in GitHub Desktop.
Save daltonrenaldo/c418061c865dd6c2d580 to your computer and use it in GitHub Desktop.
/* Generated by Opal 0.8.0.beta1 */
(function(Opal) {
Opal.dynamic_require_severity = "error";
function $rb_plus(lhs, rhs) {
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs + rhs : lhs['$+'](rhs);
}
function $rb_times(lhs, rhs) {
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs * rhs : lhs['$*'](rhs);
}
function $rb_minus(lhs, rhs) {
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs - rhs : lhs['$-'](rhs);
}
function $rb_divide(lhs, rhs) {
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs / rhs : lhs['$/'](rhs);
}
function $rb_lt(lhs, rhs) {
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs < rhs : lhs['$<'](rhs);
}
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $range = Opal.range, $hash2 = Opal.hash2;
Opal.add_stubs(['$length', '$inject', '$**', '$parse', '$to_json', '$each_with_index', '$<<', '$[]', '$to_f', '$==', '$greatest_non_nil_range', '$&', '$to_a', '$any?', '$min', '$max', '$size', '$ruby_pearson', '$first', '$last', '$run_all_correlations_helper', '$each', '$shift', '$nil?', '$[]=', '$times', '$corr', '$!', '$zero?', '$imaginary', '$blank?', '$to_sym', '$split', '$to_s', '$send', '$to_i', '$keys', '$dup', '$pop', '$lead_data', '$lag_data', '$select', '$new', '$<=>', '$count', '$run_all_correlations']);
Opal.Object.$$proto.$ruby_pearson = function(x, y) {
var $a, $b, TMP_1, $c, TMP_2, $d, TMP_3, $e, TMP_4, $f, TMP_5, $g, TMP_6, self = this, n = nil, sumx = nil, sumy = nil, sumxSq = nil, sumySq = nil, prods = nil, pSum = nil, num = nil, den = nil;
n = x.$length();
sumx = ($a = ($b = x).$inject, $a.$$p = (TMP_1 = function(r, i){var self = TMP_1.$$s || this;
if (r == null) r = nil;if (i == null) i = nil;
return $rb_plus(r, i)}, TMP_1.$$s = self, TMP_1), $a).call($b, 0);
sumy = ($a = ($c = y).$inject, $a.$$p = (TMP_2 = function(r, i){var self = TMP_2.$$s || this;
if (r == null) r = nil;if (i == null) i = nil;
return $rb_plus(r, i)}, TMP_2.$$s = self, TMP_2), $a).call($c, 0);
sumxSq = ($a = ($d = x).$inject, $a.$$p = (TMP_3 = function(r, i){var self = TMP_3.$$s || this;
if (r == null) r = nil;if (i == null) i = nil;
return $rb_plus(r, i['$**'](2))}, TMP_3.$$s = self, TMP_3), $a).call($d, 0);
sumySq = ($a = ($e = y).$inject, $a.$$p = (TMP_4 = function(r, i){var self = TMP_4.$$s || this;
if (r == null) r = nil;if (i == null) i = nil;
return $rb_plus(r, i['$**'](2))}, TMP_4.$$s = self, TMP_4), $a).call($e, 0);
prods = [];
($a = ($f = x).$each_with_index, $a.$$p = (TMP_5 = function(this_x, i){var self = TMP_5.$$s || this;
if (this_x == null) this_x = nil;if (i == null) i = nil;
return prods['$<<']($rb_times(this_x, y['$[]'](i)))}, TMP_5.$$s = self, TMP_5), $a).call($f);
pSum = ($a = ($g = prods).$inject, $a.$$p = (TMP_6 = function(r, i){var self = TMP_6.$$s || this;
if (r == null) r = nil;if (i == null) i = nil;
return $rb_plus(r, i)}, TMP_6.$$s = self, TMP_6), $a).call($g, 0);
num = $rb_minus(pSum, ($rb_divide($rb_times(sumx, sumy), (n.$to_f()))));
den = ($rb_times(($rb_minus(sumxSq, $rb_divide((sumx['$**'](2)), (n.$to_f())))), ($rb_minus(sumySq, $rb_divide((sumy['$**'](2)), (n.$to_f()))))))['$**'](0.5);
if (den['$=='](0)) {
return 0};
r = $rb_divide(num, den);
return r;
};
Opal.Object.$$proto.$corr = function(series1, series2) {
var $a, self = this, range1 = nil, range2 = nil, inter = nil, range = nil, actual_s1 = nil, actual_s2 = nil, percent_match = nil;
range1 = self.$greatest_non_nil_range(series1);
range2 = self.$greatest_non_nil_range(series2);
inter = range1.$to_a()['$&'](range2.$to_a());
if ((($a = inter['$any?']()) !== nil && (!$a.$$is_boolean || $a == true))) {
range = $range(inter.$min(), inter.$max(), false);
actual_s1 = series1['$[]'](range);
actual_s2 = series2['$[]'](range);
percent_match = $rb_divide(range.$size(), [series1.$size(), series2.$size()].$max().$to_f());
return [self.$ruby_pearson(actual_s1, actual_s2), percent_match];
} else {
return ["Invalid Period", "no matched data in range"]
};
};
Opal.Object.$$proto.$run_all_correlations = function(datasets) {
var self = this, set1 = nil, set2 = nil, tmp1 = nil, tmp2 = nil;
if (self.filter == null) self.filter = [];
if (self.correlations == null) self.correlations = $hash2(["straight", "lead_1", "lead_2", "lag_1", "lag_2"], {"straight": $hash2([], {}), "lead_1": $hash2([], {}), "lead_2": $hash2([], {}), "lag_1": $hash2([], {}), "lag_2": $hash2([], {})});
datasets = $scope.get('JSON').$parse(datasets)
set1 = datasets.$first().$to_a();
set2 = datasets.$last().$to_a();
if (self.filter.$first()['$=='](self.filter.$last())) {
self.$run_all_correlations_helper(set1, set2, true)
} else {
self.$run_all_correlations_helper(set1, set2)
};
return JSON.parse(self.correlations.$to_json());
};
Opal.Object.$$proto.$run_all_correlations_helper = function(set1, set2, set1_set2_equal) {
var $a, $b, TMP_1, self = this;
if (set1_set2_equal == null) {
set1_set2_equal = false
}
return ($a = ($b = set1).$each, $a.$$p = (TMP_1 = function(set1_series){var self = TMP_1.$$s || this, $a, $b, TMP_2, data1 = nil;
if (self.correlations == null) self.correlations = nil;
if (set1_series == null) set1_series = nil;
if (set1_set2_equal !== false && set1_set2_equal !== nil) {
set2.$shift()};
if ((($a = self.correlations['$[]']("straight")['$[]']("" + (set1_series['$[]'](0)))['$nil?']()) !== nil && (!$a.$$is_boolean || $a == true))) {
self.correlations['$[]']("straight")['$[]=']("" + (set1_series['$[]'](0)), $hash2([], {}))};
data1 = set1_series['$[]'](1);
return ($a = ($b = set2.$size()).$times, $a.$$p = (TMP_2 = function(i){var self = TMP_2.$$s || this, $a, correlation = nil, r = nil;
if (self.correlations == null) self.correlations = nil;
if (i == null) i = nil;
correlation = self.$corr(data1, set2['$[]'](i)['$[]'](1));
r = correlation.$first();
if ((($a = r['$==']("Invalid Period")['$!']()) !== nil && (!$a.$$is_boolean || $a == true))) {
return self.correlations['$[]']("straight")['$[]']("" + (set1_series['$[]'](0)))['$[]=']("" + (set2['$[]'](i)['$[]'](0)), correlation)
} else {
return nil
};}, TMP_2.$$s = self, TMP_2), $a).call($b);}, TMP_1.$$s = self, TMP_1), $a).call($b);
};
Opal.Object.$$proto.$correlate_all_to = function(series_name, dataset, data1) {
var $a, $b, TMP_9, self = this, temp1 = nil, last_index = nil;
if (self.correlations == null) self.correlations = nil;
if ((($a = data1['$blank?']()) !== nil && (!$a.$$is_boolean || $a == true))) {
} else {
temp1 = data1;
last_index = -1;
($a = ($b = self.correlations.$keys()).$each_with_index, $a.$$p = (TMP_9 = function(key, idx){var self = TMP_9.$$s || this, $a, $b, TMP_10;
if (self.correlations == null) self.correlations = nil;
if (key == null) key = nil;if (idx == null) idx = nil;
self.correlations['$[]'](key)['$[]='](series_name, $hash2([], {}));
return ($a = ($b = dataset.$to_a()).$each, $a.$$p = (TMP_10 = function(one_series){var self = TMP_10.$$s || this, $a, $b, $c, data = nil, type = nil, correlation = nil, r = nil;
if (self.correlations == null) self.correlations = nil;
if (one_series == null) one_series = nil;
if ((($a = one_series.$first()['$=='](series_name)['$!']()) !== nil && (!$a.$$is_boolean || $a == true))) {
data = [temp1, one_series['$[]'](1)];
if ((($a = key.$to_sym()['$==']("straight")['$!']()) !== nil && (!$a.$$is_boolean || $a == true))) {
type = key.$to_s().$split("_");
data = self.$send("" + (type['$[]'](0)) + "_shift", temp1, one_series['$[]'](1), type['$[]'](1).$to_i());};
correlation = ($a = self).$corr.apply($a, [].concat(data));
r = correlation.$first();
if ((($b = ($c = r['$==']("Invalid Period")['$!'](), $c !== false && $c !== nil ?r.$imaginary()['$zero?']() : $c)) !== nil && (!$b.$$is_boolean || $b == true))) {
self.correlations['$[]'](key)['$[]'](series_name)['$[]=']("" + (one_series['$[]'](0)), correlation)};
return last_index = idx;
} else {
return nil
}}, TMP_10.$$s = self, TMP_10), $a).call($b);}, TMP_9.$$s = self, TMP_9), $a).call($b);
};
return self.correlations;
};
Opal.Object.$$proto.$lead_data = function(data, offset) {
var $a, $b, TMP_11, self = this, data_dup = nil;
data_dup = data.$dup();
($a = ($b = offset).$times, $a.$$p = (TMP_11 = function(){var self = TMP_11.$$s || this;
return data_dup.$shift()}, TMP_11.$$s = self, TMP_11), $a).call($b);
return data_dup;
};
Opal.Object.$$proto.$lag_data = function(data, offset) {
var $a, $b, TMP_12, self = this, data_dup = nil;
data_dup = data.$dup();
($a = ($b = offset).$times, $a.$$p = (TMP_12 = function(){var self = TMP_12.$$s || this;
return data_dup.$pop()}, TMP_12.$$s = self, TMP_12), $a).call($b);
return data_dup;
};
Opal.Object.$$proto.$lead_shift = function(data1, data2, offset) {
var self = this;
return [self.$lead_data(data1, offset), self.$lag_data(data2, offset)];
};
Opal.Object.$$proto.$lag_shift = function(data1, data2, offset) {
var self = this;
return [self.$lag_data(data1, offset), self.$lead_data(data2, offset)];
};
Opal.Object.$$proto.$greatest_non_nil_range = function(array) {
var $a, $b, TMP_13, $c, TMP_14, $d, TMP_15, self = this, result = nil, nil_indices = nil, nils_count = nil, ranges = nil;
array = ((($a = array) !== false && $a !== nil) ? $a : []);
result = ($range(0, array.$length(), true));
nil_indices = ($a = ($b = array.$size().$times()).$select, $a.$$p = (TMP_13 = function(i){var self = TMP_13.$$s || this;
if (i == null) i = nil;
return array['$[]'](i)['$=='](nil)}, TMP_13.$$s = self, TMP_13), $a).call($b);
nils_count = nil_indices.$length();
ranges = [];
if ((($a = nil_indices['$any?']()) !== nil && (!$a.$$is_boolean || $a == true))) {
($a = ($c = nil_indices).$each_with_index, $a.$$p = (TMP_14 = function(v, i){var self = TMP_14.$$s || this, range = nil;
if (v == null) v = nil;if (i == null) i = nil;
if ($rb_lt($rb_plus(i, 1), nils_count)) {
range = $scope.get('Range').$new($rb_plus(v, 1), nil_indices['$[]']($rb_plus(i, 1)), true);
return ranges['$<<'](range);
} else {
return nil
}}, TMP_14.$$s = self, TMP_14), $a).call($c);
ranges['$<<'](($range(0, nil_indices['$[]'](0), true)));
ranges['$<<'](($range(($rb_plus(nil_indices.$last(), 1)), $rb_minus(nils_count, 1), true)));
ranges['$<<'](($range(($rb_plus(nil_indices.$last(), 1)), array.$length(), true)));
result = ($a = ($d = ranges).$max, $a.$$p = (TMP_15 = function(a, b){var self = TMP_15.$$s || this;
if (a == null) a = nil;if (b == null) b = nil;
return a.$count()['$<=>'](b.$count())}, TMP_15.$$s = self, TMP_15), $a).call($d);};
return result;
};
})(Opal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment