From 725bba5ec9c22014ddf75d0bb11731d3ed90547c Mon Sep 17 00:00:00 2001 From: Mike Bryant Date: Mon, 20 Apr 2020 23:57:01 +0100 Subject: [PATCH] refactor: Move Predictor into class --- js/predictions.js | 1220 +++++++++++++++++++++++---------------------- js/scripts.js | 3 +- 2 files changed, 617 insertions(+), 606 deletions(-) diff --git a/js/predictions.js b/js/predictions.js index a6d00d2..80621e9 100644 --- a/js/predictions.js +++ b/js/predictions.js @@ -38,43 +38,14 @@ const PROBABILITY_MATRIX = { const RATE_MULTIPLIER = 10000; -function intceil(val) { - return Math.trunc(val + 0.99999); -} - -function minimum_rate_from_given_and_base(given_price, buy_price) { - return RATE_MULTIPLIER * (given_price - 0.99999) / buy_price; -} - -function maximum_rate_from_given_and_base(given_price, buy_price) { - return RATE_MULTIPLIER * (given_price + 0.00001) / buy_price; -} - -function rate_range_from_given_and_base(given_price, buy_price) { - return [ - minimum_rate_from_given_and_base(given_price, buy_price), - maximum_rate_from_given_and_base(given_price, buy_price) - ]; -} - -function get_price(rate, basePrice) { - return intceil(rate * basePrice / RATE_MULTIPLIER); -} - -function* multiply_generator_probability(generator, probability) { - for (const it of generator) { - yield {...it, probability: it.probability * probability}; - } +function range_length(range) { + return range[1] - range[0]; } function clamp(x, min, max) { return Math.min(Math.max(x, min), max); } -function range_length(range) { - return range[1] - range[0]; -} - function range_intersect(range1, range2) { if (range1[0] > range2[1] || range1[1] < range2[0]) { return null; @@ -89,54 +60,6 @@ function range_intersect_length(range1, range2) { return range_length(range_intersect(range1, range2)); } - -/* - * This corresponds to the code: - * for (int i = start; i < start + length; i++) - * { - * sellPrices[work++] = - * intceil(randfloat(rate_min / RATE_MULTIPLIER, rate_max / RATE_MULTIPLIER) * basePrice); - * } - * - * Would return the conditional probability given the given_prices, and modify - * the predicted_prices array. - * If the given_prices won't match, returns 0. - */ -function generate_individual_random_price( - given_prices, predicted_prices, start, length, rate_min, rate_max) { - rate_min *= RATE_MULTIPLIER; - rate_max *= RATE_MULTIPLIER; - - const buy_price = given_prices[0]; - const rate_range = [rate_min, rate_max]; - let prob = 1; - - for (let i = start; i < start + length; i++) { - let min_pred = get_price(rate_min, buy_price); - let max_pred = get_price(rate_max, buy_price); - if (!isNaN(given_prices[i])) { - if (given_prices[i] < min_pred - FUDGE_FACTOR || given_prices[i] > max_pred + FUDGE_FACTOR) { - // Given price is out of predicted range, so this is the wrong pattern - return 0; - } - // TODO: How to deal with probability when there's fudge factor? - // Clamp the value to be in range now so the probability won't be totally biased to fudged values. - const real_rate_range = - rate_range_from_given_and_base(clamp(given_prices[i], min_pred, max_pred), buy_price); - prob *= range_intersect_length(rate_range, real_rate_range) / - range_length(rate_range); - min_pred = given_prices[i]; - max_pred = given_prices[i]; - } - - predicted_prices.push({ - min: min_pred, - max: max_pred, - }); - } - return prob; -} - /* * Probability Density Function of rates. * Since the PDF is continuous*, we approximate it by a discrete probability function: @@ -274,618 +197,705 @@ class PDF { } } -/* - * This corresponds to the code: - * rate = randfloat(start_rate_min, start_rate_max); - * for (int i = start; i < start + length; i++) - * { - * sellPrices[work++] = intceil(rate * basePrice); - * rate -= randfloat(rate_decay_min, rate_decay_max); - * } - * - * Would return the conditional probability given the given_prices, and modify - * the predicted_prices array. - * If the given_prices won't match, returns 0. - */ -function generate_decreasing_random_price( - given_prices, predicted_prices, start, length, start_rate_min, - start_rate_max, rate_decay_min, rate_decay_max) { - start_rate_min *= RATE_MULTIPLIER; - start_rate_max *= RATE_MULTIPLIER; - rate_decay_min *= RATE_MULTIPLIER; - rate_decay_max *= RATE_MULTIPLIER; - - const buy_price = given_prices[0]; - let rate_pdf = new PDF(start_rate_min, start_rate_max); - let prob = 1; - - for (let i = start; i < start + length; i++) { - let min_pred = get_price(rate_pdf.min_value(), buy_price); - let max_pred = get_price(rate_pdf.max_value(), buy_price); - if (!isNaN(given_prices[i])) { - if (given_prices[i] < min_pred - FUDGE_FACTOR || given_prices[i] > max_pred + FUDGE_FACTOR) { - // Given price is out of predicted range, so this is the wrong pattern - return 0; - } - // TODO: How to deal with probability when there's fudge factor? - // Clamp the value to be in range now so the probability won't be totally biased to fudged values. - const real_rate_range = - rate_range_from_given_and_base(clamp(given_prices[i], min_pred, max_pred), buy_price); - prob *= rate_pdf.range_limit(real_rate_range); - if (prob == 0) { - return 0; - } - min_pred = given_prices[i]; - max_pred = given_prices[i]; - } +class Predictor { - predicted_prices.push({ - min: min_pred, - max: max_pred, - }); + constructor(prices, first_buy, previous_pattern) { + this.prices = prices; + this.first_buy = first_buy; + this.previous_pattern = previous_pattern; + } - rate_pdf.decay(rate_decay_min, rate_decay_max); + intceil(val) { + return Math.trunc(val + 0.99999); } - return prob; -} + minimum_rate_from_given_and_base(given_price, buy_price) { + return RATE_MULTIPLIER * (given_price - 0.99999) / buy_price; + } -/* - * This corresponds to the code: - * rate = randfloat(rate_min, rate_max); - * sellPrices[work++] = intceil(randfloat(rate_min, rate) * basePrice) - 1; - * sellPrices[work++] = intceil(rate * basePrice); - * sellPrices[work++] = intceil(randfloat(rate_min, rate) * basePrice) - 1; - * - * Would return the conditional probability given the given_prices, and modify - * the predicted_prices array. - * If the given_prices won't match, returns 0. - */ -function generate_peak_price( - given_prices, predicted_prices, start, rate_min, rate_max) { - rate_min *= RATE_MULTIPLIER; - rate_max *= RATE_MULTIPLIER; - - const buy_price = given_prices[0]; - let prob = 1; - let rate_range = [rate_min, rate_max]; - - // * Calculate the probability first. - // Prob(middle_price) - const middle_price = given_prices[start + 1]; - if (!isNaN(middle_price)) { - const min_pred = get_price(rate_min, buy_price); - const max_pred = get_price(rate_max, buy_price); - if (middle_price < min_pred - FUDGE_FACTOR || middle_price > max_pred + FUDGE_FACTOR) { - // Given price is out of predicted range, so this is the wrong pattern - return 0; - } - // TODO: How to deal with probability when there's fudge factor? - // Clamp the value to be in range now so the probability won't be totally biased to fudged values. - const real_rate_range = - rate_range_from_given_and_base(clamp(middle_price, min_pred, max_pred), buy_price); - prob *= range_intersect_length(rate_range, real_rate_range) / - range_length(rate_range); - if (prob == 0) { - return 0; - } + maximum_rate_from_given_and_base(given_price, buy_price) { + return RATE_MULTIPLIER * (given_price + 0.00001) / buy_price; + } - rate_range = range_intersect(rate_range, real_rate_range); + rate_range_from_given_and_base(given_price, buy_price) { + return [ + this.minimum_rate_from_given_and_base(given_price, buy_price), + this.maximum_rate_from_given_and_base(given_price, buy_price) + ]; } - const left_price = given_prices[start]; - const right_price = given_prices[start + 2]; - // Prob(left_price | middle_price), Prob(right_price | middle_price) - // - // A = rate_range[0], B = rate_range[1], C = rate_min, X = rate, Y = randfloat(rate_min, rate) - // rate = randfloat(A, B); sellPrices[work++] = intceil(randfloat(C, rate) * basePrice) - 1; - // - // => X->U(A,B), Y->U(C,X), Y-C->U(0,X-C), Y-C->U(0,1)*(X-C), Y-C->U(0,1)*U(A-C,B-C), - // let Z=Y-C, Z1=A-C, Z2=B-C, Z->U(0,1)*U(Z1,Z2) - // Prob(Z<=t) = integral_{x=0}^{1} [min(t/x,Z2)-min(t/x,Z1)]/ (Z2-Z1) - // let F(t, ZZ) = integral_{x=0}^{1} min(t/x, ZZ) - // 1. if ZZ < t, then min(t/x, ZZ) = ZZ -> F(t, ZZ) = ZZ - // 2. if ZZ >= t, then F(t, ZZ) = integral_{x=0}^{t/ZZ} ZZ + integral_{x=t/ZZ}^{1} t/x - // = t - t log(t/ZZ) - // Prob(Z<=t) = (F(t, Z2) - F(t, Z1)) / (Z2 - Z1) - // Prob(Y<=t) = Prob(Z>=t-C) - for (const price of [left_price, right_price]) { - if (isNaN(price)) { - continue; - } - const min_pred = get_price(rate_min, buy_price) - 1; - const max_pred = get_price(rate_range[1], buy_price) - 1; - if (price < min_pred - FUDGE_FACTOR || price > max_pred + FUDGE_FACTOR) { - // Given price is out of predicted range, so this is the wrong pattern - return 0; + get_price(rate, basePrice) { + return this.intceil(rate * basePrice / RATE_MULTIPLIER); + } + + * multiply_generator_probability(generator, probability) { + for (const it of generator) { + yield {...it, probability: it.probability * probability}; } - // TODO: How to deal with probability when there's fudge factor? - // Clamp the value to be in range now so the probability won't be totally biased to fudged values. - const rate2_range = rate_range_from_given_and_base(clamp(price, min_pred, max_pred)+ 1, buy_price); - const F = (t, ZZ) => { - if (t <= 0) { - return 0; + } + + /* + * This corresponds to the code: + * for (int i = start; i < start + length; i++) + * { + * sellPrices[work++] = + * intceil(randfloat(rate_min / RATE_MULTIPLIER, rate_max / RATE_MULTIPLIER) * basePrice); + * } + * + * Would return the conditional probability given the given_prices, and modify + * the predicted_prices array. + * If the given_prices won't match, returns 0. + */ + generate_individual_random_price( + given_prices, predicted_prices, start, length, rate_min, rate_max) { + rate_min *= RATE_MULTIPLIER; + rate_max *= RATE_MULTIPLIER; + + const buy_price = given_prices[0]; + const rate_range = [rate_min, rate_max]; + let prob = 1; + + for (let i = start; i < start + length; i++) { + let min_pred = this.get_price(rate_min, buy_price); + let max_pred = this.get_price(rate_max, buy_price); + if (!isNaN(given_prices[i])) { + if (given_prices[i] < min_pred - FUDGE_FACTOR || given_prices[i] > max_pred + FUDGE_FACTOR) { + // Given price is out of predicted range, so this is the wrong pattern + return 0; + } + // TODO: How to deal with probability when there's fudge factor? + // Clamp the value to be in range now so the probability won't be totally biased to fudged values. + const real_rate_range = + this.rate_range_from_given_and_base(clamp(given_prices[i], min_pred, max_pred), buy_price); + prob *= range_intersect_length(rate_range, real_rate_range) / + range_length(rate_range); + min_pred = given_prices[i]; + max_pred = given_prices[i]; } - return ZZ < t ? ZZ : t - t * (Math.log(t) - Math.log(ZZ)); - }; - const [A, B] = rate_range; - const C = rate_min; - const Z1 = A - C; - const Z2 = B - C; - const PY = (t) => (F(t - C, Z2) - F(t - C, Z1)) / (Z2 - Z1); - prob *= PY(rate2_range[1]) - PY(rate2_range[0]); - if (prob == 0) { - return 0; + + predicted_prices.push({ + min: min_pred, + max: max_pred, + }); } + return prob; } - // * Then generate the real predicted range. - // We're doing things in different order then how we calculate probability, - // since forward prediction is more useful here. - // - // Main spike 1 - min_pred = get_price(rate_min, buy_price) - 1; - max_pred = get_price(rate_max, buy_price) - 1; - if (!isNaN(given_prices[start])) { - min_pred = given_prices[start]; - max_pred = given_prices[start]; - } - predicted_prices.push({ - min: min_pred, - max: max_pred, - }); - - // Main spike 2 - min_pred = predicted_prices[start].min; - max_pred = get_price(rate_max, buy_price); - if (!isNaN(given_prices[start + 1])) { - min_pred = given_prices[start + 1]; - max_pred = given_prices[start + 1]; - } - predicted_prices.push({ - min: min_pred, - max: max_pred, - }); - - // Main spike 3 - min_pred = get_price(rate_min, buy_price) - 1; - max_pred = predicted_prices[start + 1].max - 1; - if (!isNaN(given_prices[start + 2])) { - min_pred = given_prices[start + 2]; - max_pred = given_prices[start + 2]; + /* + * This corresponds to the code: + * rate = randfloat(start_rate_min, start_rate_max); + * for (int i = start; i < start + length; i++) + * { + * sellPrices[work++] = intceil(rate * basePrice); + * rate -= randfloat(rate_decay_min, rate_decay_max); + * } + * + * Would return the conditional probability given the given_prices, and modify + * the predicted_prices array. + * If the given_prices won't match, returns 0. + */ + generate_decreasing_random_price( + given_prices, predicted_prices, start, length, start_rate_min, + start_rate_max, rate_decay_min, rate_decay_max) { + start_rate_min *= RATE_MULTIPLIER; + start_rate_max *= RATE_MULTIPLIER; + rate_decay_min *= RATE_MULTIPLIER; + rate_decay_max *= RATE_MULTIPLIER; + + const buy_price = given_prices[0]; + let rate_pdf = new PDF(start_rate_min, start_rate_max); + let prob = 1; + + for (let i = start; i < start + length; i++) { + let min_pred = this.get_price(rate_pdf.min_value(), buy_price); + let max_pred = this.get_price(rate_pdf.max_value(), buy_price); + if (!isNaN(given_prices[i])) { + if (given_prices[i] < min_pred - FUDGE_FACTOR || given_prices[i] > max_pred + FUDGE_FACTOR) { + // Given price is out of predicted range, so this is the wrong pattern + return 0; + } + // TODO: How to deal with probability when there's fudge factor? + // Clamp the value to be in range now so the probability won't be totally biased to fudged values. + const real_rate_range = + this.rate_range_from_given_and_base(clamp(given_prices[i], min_pred, max_pred), buy_price); + prob *= rate_pdf.range_limit(real_rate_range); + if (prob == 0) { + return 0; + } + min_pred = given_prices[i]; + max_pred = given_prices[i]; + } + + predicted_prices.push({ + min: min_pred, + max: max_pred, + }); + + rate_pdf.decay(rate_decay_min, rate_decay_max); + } + return prob; } - predicted_prices.push({ - min: min_pred, - max: max_pred, - }); - return prob; -} -function* - generate_pattern_0_with_lengths( - given_prices, high_phase_1_len, dec_phase_1_len, high_phase_2_len, - dec_phase_2_len, high_phase_3_len) { /* - // PATTERN 0: high, decreasing, high, decreasing, high - work = 2; - // high phase 1 - for (int i = 0; i < hiPhaseLen1; i++) - { - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + * This corresponds to the code: + * rate = randfloat(rate_min, rate_max); + * sellPrices[work++] = intceil(randfloat(rate_min, rate) * basePrice) - 1; + * sellPrices[work++] = intceil(rate * basePrice); + * sellPrices[work++] = intceil(randfloat(rate_min, rate) * basePrice) - 1; + * + * Would return the conditional probability given the given_prices, and modify + * the predicted_prices array. + * If the given_prices won't match, returns 0. + */ + generate_peak_price( + given_prices, predicted_prices, start, rate_min, rate_max) { + rate_min *= RATE_MULTIPLIER; + rate_max *= RATE_MULTIPLIER; + + const buy_price = given_prices[0]; + let prob = 1; + let rate_range = [rate_min, rate_max]; + + // * Calculate the probability first. + // Prob(middle_price) + const middle_price = given_prices[start + 1]; + if (!isNaN(middle_price)) { + const min_pred = this.get_price(rate_min, buy_price); + const max_pred = this.get_price(rate_max, buy_price); + if (middle_price < min_pred - FUDGE_FACTOR || middle_price > max_pred + FUDGE_FACTOR) { + // Given price is out of predicted range, so this is the wrong pattern + return 0; } - // decreasing phase 1 - rate = randfloat(0.8, 0.6); - for (int i = 0; i < decPhaseLen1; i++) - { - sellPrices[work++] = intceil(rate * basePrice); - rate -= 0.04; - rate -= randfloat(0, 0.06); + // TODO: How to deal with probability when there's fudge factor? + // Clamp the value to be in range now so the probability won't be totally biased to fudged values. + const real_rate_range = + this.rate_range_from_given_and_base(clamp(middle_price, min_pred, max_pred), buy_price); + prob *= range_intersect_length(rate_range, real_rate_range) / + range_length(rate_range); + if (prob == 0) { + return 0; } - // high phase 2 - for (int i = 0; i < (hiPhaseLen2and3 - hiPhaseLen3); i++) - { - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + + rate_range = range_intersect(rate_range, real_rate_range); + } + + const left_price = given_prices[start]; + const right_price = given_prices[start + 2]; + // Prob(left_price | middle_price), Prob(right_price | middle_price) + // + // A = rate_range[0], B = rate_range[1], C = rate_min, X = rate, Y = randfloat(rate_min, rate) + // rate = randfloat(A, B); sellPrices[work++] = intceil(randfloat(C, rate) * basePrice) - 1; + // + // => X->U(A,B), Y->U(C,X), Y-C->U(0,X-C), Y-C->U(0,1)*(X-C), Y-C->U(0,1)*U(A-C,B-C), + // let Z=Y-C, Z1=A-C, Z2=B-C, Z->U(0,1)*U(Z1,Z2) + // Prob(Z<=t) = integral_{x=0}^{1} [min(t/x,Z2)-min(t/x,Z1)]/ (Z2-Z1) + // let F(t, ZZ) = integral_{x=0}^{1} min(t/x, ZZ) + // 1. if ZZ < t, then min(t/x, ZZ) = ZZ -> F(t, ZZ) = ZZ + // 2. if ZZ >= t, then F(t, ZZ) = integral_{x=0}^{t/ZZ} ZZ + integral_{x=t/ZZ}^{1} t/x + // = t - t log(t/ZZ) + // Prob(Z<=t) = (F(t, Z2) - F(t, Z1)) / (Z2 - Z1) + // Prob(Y<=t) = Prob(Z>=t-C) + for (const price of [left_price, right_price]) { + if (isNaN(price)) { + continue; } - // decreasing phase 2 - rate = randfloat(0.8, 0.6); - for (int i = 0; i < decPhaseLen2; i++) - { - sellPrices[work++] = intceil(rate * basePrice); - rate -= 0.04; - rate -= randfloat(0, 0.06); + const min_pred = this.get_price(rate_min, buy_price) - 1; + const max_pred = this.get_price(rate_range[1], buy_price) - 1; + if (price < min_pred - FUDGE_FACTOR || price > max_pred + FUDGE_FACTOR) { + // Given price is out of predicted range, so this is the wrong pattern + return 0; } - // high phase 3 - for (int i = 0; i < hiPhaseLen3; i++) - { - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + // TODO: How to deal with probability when there's fudge factor? + // Clamp the value to be in range now so the probability won't be totally biased to fudged values. + const rate2_range = this.rate_range_from_given_and_base(clamp(price, min_pred, max_pred)+ 1, buy_price); + const F = (t, ZZ) => { + if (t <= 0) { + return 0; + } + return ZZ < t ? ZZ : t - t * (Math.log(t) - Math.log(ZZ)); + }; + const [A, B] = rate_range; + const C = rate_min; + const Z1 = A - C; + const Z2 = B - C; + const PY = (t) => (F(t - C, Z2) - F(t - C, Z1)) / (Z2 - Z1); + prob *= PY(rate2_range[1]) - PY(rate2_range[0]); + if (prob == 0) { + return 0; } - */ - - const buy_price = given_prices[0]; - const predicted_prices = [ - { - min: buy_price, - max: buy_price, - }, - { - min: buy_price, - max: buy_price, - }, - ]; - let probability = 1; - - // High Phase 1 - probability *= generate_individual_random_price( - given_prices, predicted_prices, 2, high_phase_1_len, 0.9, 1.4); - if (probability == 0) { - return; - } + } - // Dec Phase 1 - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, 2 + high_phase_1_len, dec_phase_1_len, - 0.6, 0.8, 0.04, 0.1); - if (probability == 0) { - return; - } + // * Then generate the real predicted range. + // We're doing things in different order then how we calculate probability, + // since forward prediction is more useful here. + // + // Main spike 1 + let min_pred = this.get_price(rate_min, buy_price) - 1; + let max_pred = this.get_price(rate_max, buy_price) - 1; + if (!isNaN(given_prices[start])) { + min_pred = given_prices[start]; + max_pred = given_prices[start]; + } + predicted_prices.push({ + min: min_pred, + max: max_pred, + }); - // High Phase 2 - probability *= generate_individual_random_price(given_prices, predicted_prices, - 2 + high_phase_1_len + dec_phase_1_len, high_phase_2_len, 0.9, 1.4); - if (probability == 0) { - return; - } + // Main spike 2 + min_pred = predicted_prices[start].min; + max_pred = this.get_price(rate_max, buy_price); + if (!isNaN(given_prices[start + 1])) { + min_pred = given_prices[start + 1]; + max_pred = given_prices[start + 1]; + } + predicted_prices.push({ + min: min_pred, + max: max_pred, + }); - // Dec Phase 2 - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, - 2 + high_phase_1_len + dec_phase_1_len + high_phase_2_len, - dec_phase_2_len, 0.6, 0.8, 0.04, 0.1); - if (probability == 0) { - return; - } + // Main spike 3 + min_pred = this.get_price(rate_min, buy_price) - 1; + max_pred = predicted_prices[start + 1].max - 1; + if (!isNaN(given_prices[start + 2])) { + min_pred = given_prices[start + 2]; + max_pred = given_prices[start + 2]; + } + predicted_prices.push({ + min: min_pred, + max: max_pred, + }); - // High Phase 3 - if (2 + high_phase_1_len + dec_phase_1_len + high_phase_2_len + dec_phase_2_len + high_phase_3_len != 14) { - throw new Error("Phase lengths don't add up"); + return prob; } - const prev_length = 2 + high_phase_1_len + dec_phase_1_len + - high_phase_2_len + dec_phase_2_len; - probability *= generate_individual_random_price( - given_prices, predicted_prices, prev_length, 14 - prev_length, 0.9, 1.4); - if (probability == 0) { - return; - } + * generate_pattern_0_with_lengths( + given_prices, high_phase_1_len, dec_phase_1_len, high_phase_2_len, + dec_phase_2_len, high_phase_3_len) { + /* + // PATTERN 0: high, decreasing, high, decreasing, high + work = 2; + // high phase 1 + for (int i = 0; i < hiPhaseLen1; i++) + { + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + } + // decreasing phase 1 + rate = randfloat(0.8, 0.6); + for (int i = 0; i < decPhaseLen1; i++) + { + sellPrices[work++] = intceil(rate * basePrice); + rate -= 0.04; + rate -= randfloat(0, 0.06); + } + // high phase 2 + for (int i = 0; i < (hiPhaseLen2and3 - hiPhaseLen3); i++) + { + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + } + // decreasing phase 2 + rate = randfloat(0.8, 0.6); + for (int i = 0; i < decPhaseLen2; i++) + { + sellPrices[work++] = intceil(rate * basePrice); + rate -= 0.04; + rate -= randfloat(0, 0.06); + } + // high phase 3 + for (int i = 0; i < hiPhaseLen3; i++) + { + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + } + */ - yield { - pattern_description: i18next.t("patterns.fluctuating"), - pattern_number: 0, - prices: predicted_prices, - probability, - }; -} + const buy_price = given_prices[0]; + const predicted_prices = [ + { + min: buy_price, + max: buy_price, + }, + { + min: buy_price, + max: buy_price, + }, + ]; + let probability = 1; + + // High Phase 1 + probability *= this.generate_individual_random_price( + given_prices, predicted_prices, 2, high_phase_1_len, 0.9, 1.4); + if (probability == 0) { + return; + } -function* generate_pattern_0(given_prices) { - /* - decPhaseLen1 = randbool() ? 3 : 2; - decPhaseLen2 = 5 - decPhaseLen1; - hiPhaseLen1 = randint(0, 6); - hiPhaseLen2and3 = 7 - hiPhaseLen1; - hiPhaseLen3 = randint(0, hiPhaseLen2and3 - 1); - */ - for (var dec_phase_1_len = 2; dec_phase_1_len < 4; dec_phase_1_len++) { - for (var high_phase_1_len = 0; high_phase_1_len < 7; high_phase_1_len++) { - for (var high_phase_3_len = 0; high_phase_3_len < (7 - high_phase_1_len - 1 + 1); high_phase_3_len++) { - yield* multiply_generator_probability( - generate_pattern_0_with_lengths(given_prices, high_phase_1_len, dec_phase_1_len, 7 - high_phase_1_len - high_phase_3_len, 5 - dec_phase_1_len, high_phase_3_len), - 1 / (4 - 2) / 7 / (7 - high_phase_1_len)); - } + // Dec Phase 1 + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, 2 + high_phase_1_len, dec_phase_1_len, + 0.6, 0.8, 0.04, 0.1); + if (probability == 0) { + return; } - } -} -function* generate_pattern_1_with_peak(given_prices, peak_start) { - /* - // PATTERN 1: decreasing middle, high spike, random low - peakStart = randint(3, 9); - rate = randfloat(0.9, 0.85); - for (work = 2; work < peakStart; work++) - { - sellPrices[work] = intceil(rate * basePrice); - rate -= 0.03; - rate -= randfloat(0, 0.02); - } - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); - sellPrices[work++] = intceil(randfloat(1.4, 2.0) * basePrice); - sellPrices[work++] = intceil(randfloat(2.0, 6.0) * basePrice); - sellPrices[work++] = intceil(randfloat(1.4, 2.0) * basePrice); - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); - for (; work < 14; work++) - { - sellPrices[work] = intceil(randfloat(0.4, 0.9) * basePrice); - } - */ - - const buy_price = given_prices[0]; - const predicted_prices = [ - { - min: buy_price, - max: buy_price, - }, - { - min: buy_price, - max: buy_price, - }, - ]; - let probability = 1; - - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, 2, peak_start - 2, 0.85, 0.9, 0.03, 0.05); - if (probability == 0) { - return; - } + // High Phase 2 + probability *= this.generate_individual_random_price(given_prices, predicted_prices, + 2 + high_phase_1_len + dec_phase_1_len, high_phase_2_len, 0.9, 1.4); + if (probability == 0) { + return; + } - // Now each day is independent of next - min_randoms = [0.9, 1.4, 2.0, 1.4, 0.9, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4] - max_randoms = [1.4, 2.0, 6.0, 2.0, 1.4, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9] - for (let i = peak_start; i < 14; i++) { - probability *= generate_individual_random_price( - given_prices, predicted_prices, i, 1, min_randoms[i - peak_start], - max_randoms[i - peak_start]); + // Dec Phase 2 + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, + 2 + high_phase_1_len + dec_phase_1_len + high_phase_2_len, + dec_phase_2_len, 0.6, 0.8, 0.04, 0.1); if (probability == 0) { return; } + + // High Phase 3 + if (2 + high_phase_1_len + dec_phase_1_len + high_phase_2_len + dec_phase_2_len + high_phase_3_len != 14) { + throw new Error("Phase lengths don't add up"); + } + + const prev_length = 2 + high_phase_1_len + dec_phase_1_len + + high_phase_2_len + dec_phase_2_len; + probability *= this.generate_individual_random_price( + given_prices, predicted_prices, prev_length, 14 - prev_length, 0.9, 1.4); + if (probability == 0) { + return; + } + + yield { + pattern_description: i18next.t("patterns.fluctuating"), + pattern_number: 0, + prices: predicted_prices, + probability, + }; } - yield { - pattern_description: i18next.t("patterns.large-spike"), - pattern_number: 1, - prices: predicted_prices, - probability, - }; -} -function* generate_pattern_1(given_prices) { - for (var peak_start = 3; peak_start < 10; peak_start++) { - yield* multiply_generator_probability(generate_pattern_1_with_peak(given_prices, peak_start), 1 / (10 - 3)); + * generate_pattern_0(given_prices) { + /* + decPhaseLen1 = randbool() ? 3 : 2; + decPhaseLen2 = 5 - decPhaseLen1; + hiPhaseLen1 = randint(0, 6); + hiPhaseLen2and3 = 7 - hiPhaseLen1; + hiPhaseLen3 = randint(0, hiPhaseLen2and3 - 1); + */ + for (var dec_phase_1_len = 2; dec_phase_1_len < 4; dec_phase_1_len++) { + for (var high_phase_1_len = 0; high_phase_1_len < 7; high_phase_1_len++) { + for (var high_phase_3_len = 0; high_phase_3_len < (7 - high_phase_1_len - 1 + 1); high_phase_3_len++) { + yield* this.multiply_generator_probability( + this.generate_pattern_0_with_lengths(given_prices, high_phase_1_len, dec_phase_1_len, 7 - high_phase_1_len - high_phase_3_len, 5 - dec_phase_1_len, high_phase_3_len), + 1 / (4 - 2) / 7 / (7 - high_phase_1_len)); + } + } + } } -} -function* generate_pattern_2(given_prices) { - /* - // PATTERN 2: consistently decreasing - rate = 0.9; - rate -= randfloat(0, 0.05); - for (work = 2; work < 14; work++) + * generate_pattern_1_with_peak(given_prices, peak_start) { + /* + // PATTERN 1: decreasing middle, high spike, random low + peakStart = randint(3, 9); + rate = randfloat(0.9, 0.85); + for (work = 2; work < peakStart; work++) { sellPrices[work] = intceil(rate * basePrice); rate -= 0.03; rate -= randfloat(0, 0.02); } - break; - */ - - const buy_price = given_prices[0]; - const predicted_prices = [ - { - min: buy_price, - max: buy_price, - }, - { - min: buy_price, - max: buy_price, - }, - ]; - let probability = 1; - - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, 2, 14 - 2, 0.85, 0.9, 0.03, 0.05); - if (probability == 0) { - return; + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + sellPrices[work++] = intceil(randfloat(1.4, 2.0) * basePrice); + sellPrices[work++] = intceil(randfloat(2.0, 6.0) * basePrice); + sellPrices[work++] = intceil(randfloat(1.4, 2.0) * basePrice); + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + for (; work < 14; work++) + { + sellPrices[work] = intceil(randfloat(0.4, 0.9) * basePrice); + } + */ + + const buy_price = given_prices[0]; + const predicted_prices = [ + { + min: buy_price, + max: buy_price, + }, + { + min: buy_price, + max: buy_price, + }, + ]; + let probability = 1; + + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, 2, peak_start - 2, 0.85, 0.9, 0.03, 0.05); + if (probability == 0) { + return; + } + + // Now each day is independent of next + let min_randoms = [0.9, 1.4, 2.0, 1.4, 0.9, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4] + let max_randoms = [1.4, 2.0, 6.0, 2.0, 1.4, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9] + for (let i = peak_start; i < 14; i++) { + probability *= this.generate_individual_random_price( + given_prices, predicted_prices, i, 1, min_randoms[i - peak_start], + max_randoms[i - peak_start]); + if (probability == 0) { + return; + } + } + yield { + pattern_description: i18next.t("patterns.large-spike"), + pattern_number: 1, + prices: predicted_prices, + probability, + }; } - yield { - pattern_description: i18next.t("patterns.decreasing"), - pattern_number: 2, - prices: predicted_prices, - probability, - }; -} + * generate_pattern_1(given_prices) { + for (var peak_start = 3; peak_start < 10; peak_start++) { + yield* this.multiply_generator_probability(this.generate_pattern_1_with_peak(given_prices, peak_start), 1 / (10 - 3)); + } + } -function* generate_pattern_3_with_peak(given_prices, peak_start) { + * generate_pattern_2(given_prices) { + /* + // PATTERN 2: consistently decreasing + rate = 0.9; + rate -= randfloat(0, 0.05); + for (work = 2; work < 14; work++) + { + sellPrices[work] = intceil(rate * basePrice); + rate -= 0.03; + rate -= randfloat(0, 0.02); + } + break; + */ - /* - // PATTERN 3: decreasing, spike, decreasing - peakStart = randint(2, 9); - // decreasing phase before the peak - rate = randfloat(0.9, 0.4); - for (work = 2; work < peakStart; work++) - { - sellPrices[work] = intceil(rate * basePrice); - rate -= 0.03; - rate -= randfloat(0, 0.02); - } - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * (float)basePrice); - sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); - rate = randfloat(1.4, 2.0); - sellPrices[work++] = intceil(randfloat(1.4, rate) * basePrice) - 1; - sellPrices[work++] = intceil(rate * basePrice); - sellPrices[work++] = intceil(randfloat(1.4, rate) * basePrice) - 1; - // decreasing phase after the peak - if (work < 14) - { + const buy_price = given_prices[0]; + const predicted_prices = [ + { + min: buy_price, + max: buy_price, + }, + { + min: buy_price, + max: buy_price, + }, + ]; + let probability = 1; + + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, 2, 14 - 2, 0.85, 0.9, 0.03, 0.05); + if (probability == 0) { + return; + } + + yield { + pattern_description: i18next.t("patterns.decreasing"), + pattern_number: 2, + prices: predicted_prices, + probability, + }; + } + + * generate_pattern_3_with_peak(given_prices, peak_start) { + + /* + // PATTERN 3: decreasing, spike, decreasing + peakStart = randint(2, 9); + // decreasing phase before the peak rate = randfloat(0.9, 0.4); - for (; work < 14; work++) + for (work = 2; work < peakStart; work++) { sellPrices[work] = intceil(rate * basePrice); rate -= 0.03; rate -= randfloat(0, 0.02); } - } - */ - - const buy_price = given_prices[0]; - const predicted_prices = [ - { - min: buy_price, - max: buy_price, - }, - { - min: buy_price, - max: buy_price, - }, - ]; - let probability = 1; - - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, 2, peak_start - 2, 0.4, 0.9, 0.03, 0.05); - if (probability == 0) { - return; - } + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * (float)basePrice); + sellPrices[work++] = intceil(randfloat(0.9, 1.4) * basePrice); + rate = randfloat(1.4, 2.0); + sellPrices[work++] = intceil(randfloat(1.4, rate) * basePrice) - 1; + sellPrices[work++] = intceil(rate * basePrice); + sellPrices[work++] = intceil(randfloat(1.4, rate) * basePrice) - 1; + // decreasing phase after the peak + if (work < 14) + { + rate = randfloat(0.9, 0.4); + for (; work < 14; work++) + { + sellPrices[work] = intceil(rate * basePrice); + rate -= 0.03; + rate -= randfloat(0, 0.02); + } + } + */ - // The peak - probability *= generate_individual_random_price( - given_prices, predicted_prices, peak_start, 2, 0.9, 1.4); - if (probability == 0) { - return; - } + const buy_price = given_prices[0]; + const predicted_prices = [ + { + min: buy_price, + max: buy_price, + }, + { + min: buy_price, + max: buy_price, + }, + ]; + let probability = 1; + + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, 2, peak_start - 2, 0.4, 0.9, 0.03, 0.05); + if (probability == 0) { + return; + } - probability *= generate_peak_price( - given_prices, predicted_prices, peak_start + 2, 1.4, 2.0); - if (probability == 0) { - return; - } + // The peak + probability *= this.generate_individual_random_price( + given_prices, predicted_prices, peak_start, 2, 0.9, 1.4); + if (probability == 0) { + return; + } - if (peak_start + 5 < 14) { - probability *= generate_decreasing_random_price( - given_prices, predicted_prices, peak_start + 5, 14 - (peak_start + 5), - 0.4, 0.9, 0.03, 0.05); + probability *= this.generate_peak_price( + given_prices, predicted_prices, peak_start + 2, 1.4, 2.0); if (probability == 0) { return; } - } - yield { - pattern_description: i18next.t("patterns.small-spike"), - pattern_number: 3, - prices: predicted_prices, - probability, - }; -} + if (peak_start + 5 < 14) { + probability *= this.generate_decreasing_random_price( + given_prices, predicted_prices, peak_start + 5, 14 - (peak_start + 5), + 0.4, 0.9, 0.03, 0.05); + if (probability == 0) { + return; + } + } -function* generate_pattern_3(given_prices) { - for (let peak_start = 2; peak_start < 10; peak_start++) { - yield* multiply_generator_probability(generate_pattern_3_with_peak(given_prices, peak_start), 1 / (10 - 2)); + yield { + pattern_description: i18next.t("patterns.small-spike"), + pattern_number: 3, + prices: predicted_prices, + probability, + }; } -} -function get_transition_probability(previous_pattern) { - if (typeof previous_pattern === 'undefined' || Number.isNaN(previous_pattern) || previous_pattern === null || previous_pattern < 0 || previous_pattern > 3) { - // TODO: Fill the steady state pattern (https://github.com/mikebryant/ac-nh-turnip-prices/pull/90) here. - return [0.346278, 0.247363, 0.147607, 0.258752]; + * generate_pattern_3(given_prices) { + for (let peak_start = 2; peak_start < 10; peak_start++) { + yield* this.multiply_generator_probability(this.generate_pattern_3_with_peak(given_prices, peak_start), 1 / (10 - 2)); + } } - return PROBABILITY_MATRIX[previous_pattern]; -} + get_transition_probability(previous_pattern) { + if (typeof previous_pattern === 'undefined' || Number.isNaN(previous_pattern) || previous_pattern === null || previous_pattern < 0 || previous_pattern > 3) { + // TODO: Fill the steady state pattern (https://github.com/mikebryant/ac-nh-turnip-prices/pull/90) here. + return [0.346278, 0.247363, 0.147607, 0.258752]; + } + + return PROBABILITY_MATRIX[previous_pattern]; + } -function* generate_all_patterns(sell_prices, previous_pattern) { - const generate_pattern_fns = [generate_pattern_0, generate_pattern_1, generate_pattern_2, generate_pattern_3]; - const transition_probability = get_transition_probability(previous_pattern); + * generate_all_patterns(sell_prices, previous_pattern) { + const generate_pattern_fns = [this.generate_pattern_0, this.generate_pattern_1, this.generate_pattern_2, this.generate_pattern_3]; + const transition_probability = this.get_transition_probability(previous_pattern); - for (let i = 0; i < 4; i++) { - yield* multiply_generator_probability(generate_pattern_fns[i](sell_prices), transition_probability[i]); + for (let i = 0; i < 4; i++) { + yield* this.multiply_generator_probability(generate_pattern_fns[i].bind(this)(sell_prices), transition_probability[i]); + } } -} -function* generate_possibilities(sell_prices, first_buy, previous_pattern) { - if (first_buy || isNaN(sell_prices[0])) { - for (var buy_price = 90; buy_price <= 110; buy_price++) { - sell_prices[0] = sell_prices[1] = buy_price; - if (first_buy) { - yield* generate_pattern_3(sell_prices); - } else { - // All buy prices are equal probability and we're at the outmost layer, - // so don't need to multiply_generator_probability here. - yield* generate_all_patterns(sell_prices, previous_pattern) + * generate_possibilities(sell_prices, first_buy, previous_pattern) { + if (first_buy || isNaN(sell_prices[0])) { + for (var buy_price = 90; buy_price <= 110; buy_price++) { + sell_prices[0] = sell_prices[1] = buy_price; + if (first_buy) { + yield* this.generate_pattern_3(sell_prices); + } else { + // All buy prices are equal probability and we're at the outmost layer, + // so don't need to multiply_generator_probability here. + yield* this.generate_all_patterns(sell_prices, previous_pattern) + } } + } else { + yield* this.generate_all_patterns(sell_prices, previous_pattern) } - } else { - yield* generate_all_patterns(sell_prices, previous_pattern) } -} -function analyze_possibilities(sell_prices, first_buy, previous_pattern) { - const generated_possibilities = Array.from(generate_possibilities(sell_prices, first_buy, previous_pattern)); - console.log(generated_possibilities); + analyze_possibilities() { + const sell_prices = this.prices; + const first_buy = this.first_buy; + const previous_pattern = this.previous_pattern; + const generated_possibilities = Array.from(this.generate_possibilities(sell_prices, first_buy, previous_pattern)); + console.log(generated_possibilities); - const total_probability = generated_possibilities.reduce((acc, it) => acc + it.probability, 0); - for (const it of generated_possibilities) { - it.probability /= total_probability; - } + const total_probability = generated_possibilities.reduce((acc, it) => acc + it.probability, 0); + for (const it of generated_possibilities) { + it.probability /= total_probability; + } - for (let poss of generated_possibilities) { - var weekMins = []; - var weekMaxes = []; - for (let day of poss.prices.slice(2)) { - // Check for a future date by checking for a range of prices - if(day.min !== day.max){ - weekMins.push(day.min); - weekMaxes.push(day.max); - } else { - // If we find a set price after one or more ranged prices, the user has missed a day. Discard that data and start again. - weekMins = []; - weekMaxes = []; + for (let poss of generated_possibilities) { + var weekMins = []; + var weekMaxes = []; + for (let day of poss.prices.slice(2)) { + // Check for a future date by checking for a range of prices + if(day.min !== day.max){ + weekMins.push(day.min); + weekMaxes.push(day.max); + } else { + // If we find a set price after one or more ranged prices, the user has missed a day. Discard that data and start again. + weekMins = []; + weekMaxes = []; + } } + if (!weekMins.length && !weekMaxes.length) { + weekMins.push(poss.prices[poss.prices.length -1].min); + weekMaxes.push(poss.prices[poss.prices.length -1].max); + } + poss.weekGuaranteedMinimum = Math.max(...weekMins); + poss.weekMax = Math.max(...weekMaxes); } - if (!weekMins.length && !weekMaxes.length) { - weekMins.push(poss.prices[poss.prices.length -1].min); - weekMaxes.push(poss.prices[poss.prices.length -1].max); - } - poss.weekGuaranteedMinimum = Math.max(...weekMins); - poss.weekMax = Math.max(...weekMaxes); - } - category_totals = {} - for (let i of [0, 1, 2, 3]) { - category_totals[i] = generated_possibilities - .filter(value => value.pattern_number == i) - .map(value => value.probability) - .reduce((previous, current) => previous + current, 0); - } + let category_totals = {} + for (let i of [0, 1, 2, 3]) { + category_totals[i] = generated_possibilities + .filter(value => value.pattern_number == i) + .map(value => value.probability) + .reduce((previous, current) => previous + current, 0); + } - for (let pos of generated_possibilities) { - pos.category_total_probability = category_totals[pos.pattern_number]; - } + for (let pos of generated_possibilities) { + pos.category_total_probability = category_totals[pos.pattern_number]; + } - generated_possibilities.sort((a, b) => { - return b.category_total_probability - a.category_total_probability || b.probability - a.probability; - }); + generated_possibilities.sort((a, b) => { + return b.category_total_probability - a.category_total_probability || b.probability - a.probability; + }); - global_min_max = []; - for (var day = 0; day < 14; day++) { - prices = { - min: 999, - max: 0, - } - for (let poss of generated_possibilities) { - if (poss.prices[day].min < prices.min) { - prices.min = poss.prices[day].min; + let global_min_max = []; + for (var day = 0; day < 14; day++) { + prices = { + min: 999, + max: 0, } - if (poss.prices[day].max > prices.max) { - prices.max = poss.prices[day].max; + for (let poss of generated_possibilities) { + if (poss.prices[day].min < prices.min) { + prices.min = poss.prices[day].min; + } + if (poss.prices[day].max > prices.max) { + prices.max = poss.prices[day].max; + } } + global_min_max.push(prices); } - global_min_max.push(prices); - } - generated_possibilities.unshift({ - pattern_description: i18next.t("patterns.all"), - pattern_number: 4, - prices: global_min_max, - weekGuaranteedMinimum: Math.min(...generated_possibilities.map(poss => poss.weekGuaranteedMinimum)), - weekMax: Math.max(...generated_possibilities.map(poss => poss.weekMax)) - }); + generated_possibilities.unshift({ + pattern_description: i18next.t("patterns.all"), + pattern_number: 4, + prices: global_min_max, + weekGuaranteedMinimum: Math.min(...generated_possibilities.map(poss => poss.weekGuaranteedMinimum)), + weekMax: Math.max(...generated_possibilities.map(poss => poss.weekMax)) + }); - return generated_possibilities; + return generated_possibilities; + } } diff --git a/js/scripts.js b/js/scripts.js index a60bc60..7533090 100644 --- a/js/scripts.js +++ b/js/scripts.js @@ -255,7 +255,8 @@ const calculateOutput = function (data, first_buy, previous_pattern) { return; } let output_possibilities = ""; - let analyzed_possibilities = analyze_possibilities(data, first_buy, previous_pattern); + let predictor = new Predictor(data, first_buy, previous_pattern); + let analyzed_possibilities = predictor.analyze_possibilities(); previous_pattern_number = "" for (let poss of analyzed_possibilities) { var out_line = "" + poss.pattern_description + ""