|
| 1 | +/** |
| 2 | + * Pruned Exact Linear Time |
| 3 | + */ |
| 4 | +export default class PELT { |
| 5 | + // Optimal detection of changepoints with a linear computational cost |
| 6 | + // https://arxiv.org/pdf/1101.1438 |
| 7 | + // https://github.com/deepcharles/ruptures |
| 8 | + /** |
| 9 | + * @param {number} beta Penalty constant |
| 10 | + * @param {"rbf" | "l1" | "l2" | function (number[][], number, number): number} cost Measure of data |
| 11 | + */ |
| 12 | + constructor(beta, cost = 'rbf') { |
| 13 | + this._jump = 1 |
| 14 | + this._min_size = 1 |
| 15 | + this._penalty = beta |
| 16 | + this._k = beta |
| 17 | + |
| 18 | + if (typeof cost === 'function') { |
| 19 | + this._cost = cost |
| 20 | + } else if (cost === 'rbf') { |
| 21 | + this._cost = (() => { |
| 22 | + const k = [] |
| 23 | + return (data, s, e) => { |
| 24 | + if (k.length === 0) { |
| 25 | + for (let i = 0; i < data.length; i++) { |
| 26 | + k[i] = [] |
| 27 | + for (let j = 0; j < i; j++) { |
| 28 | + k[i][j] = Math.exp(-data[i].reduce((s, v, t) => s + (v - data[j][t]) ** 2, 0)) |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + let c = 0 |
| 34 | + for (let i = s; i < e; i++) { |
| 35 | + for (let j = s; j < i; j++) { |
| 36 | + c -= k[i][j] * 2 |
| 37 | + } |
| 38 | + } |
| 39 | + return c / (e - s) |
| 40 | + } |
| 41 | + })() |
| 42 | + } else if (cost === 'l1') { |
| 43 | + this._min_size = 2 |
| 44 | + this._cost = (data, s, e) => { |
| 45 | + const d = data.slice(s, e) |
| 46 | + const dim = d[0].length |
| 47 | + let c = 0 |
| 48 | + for (let j = 0; j < dim; j++) { |
| 49 | + const dj = d.map(d => d[j]) |
| 50 | + dj.sort((a, b) => a - b) |
| 51 | + const median = |
| 52 | + dj.length % 2 === 0 ? (dj[dj.length / 2] + dj[dj.length / 2 - 1]) / 2 : dj[(dj.length - 1) / 2] |
| 53 | + for (let i = 0; i < e - s; i++) { |
| 54 | + c += Math.abs(d[i][j] - median) |
| 55 | + } |
| 56 | + } |
| 57 | + return c |
| 58 | + } |
| 59 | + } else if (cost === 'l2') { |
| 60 | + this._cost = (data, s, e) => { |
| 61 | + const d = data.slice(s, e) |
| 62 | + const dim = d[0].length |
| 63 | + let c = 0 |
| 64 | + for (let j = 0; j < dim; j++) { |
| 65 | + const mean = d.reduce((s, v) => s + v[j], 0) / d.length |
| 66 | + for (let i = 0; i < e - s; i++) { |
| 67 | + c += (d[i][j] - mean) ** 2 |
| 68 | + } |
| 69 | + } |
| 70 | + return c |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns changepoint or not. |
| 77 | + * @param {number[][]} datas Training data |
| 78 | + * @returns {boolean[]} Predicted values |
| 79 | + */ |
| 80 | + predict(datas) { |
| 81 | + const n = datas.length |
| 82 | + |
| 83 | + const partitions = [[0]] |
| 84 | + let admissible = [] |
| 85 | + |
| 86 | + const idx = [] |
| 87 | + for (let i = 0; i < n; i += this._jump) { |
| 88 | + if (i >= this._min_size) { |
| 89 | + idx.push(i) |
| 90 | + } |
| 91 | + } |
| 92 | + idx.push(n) |
| 93 | + |
| 94 | + for (const backPoint of idx) { |
| 95 | + const admPoint = Math.floor((backPoint - this._min_size) / this._jump) * this._jump |
| 96 | + admissible.push(admPoint) |
| 97 | + |
| 98 | + let bestPartition = null |
| 99 | + let bestCost = Infinity |
| 100 | + const subpart = [] |
| 101 | + for (const t of admissible) { |
| 102 | + if (!partitions[t]) { |
| 103 | + subpart.push(null) |
| 104 | + continue |
| 105 | + } |
| 106 | + const part = partitions[t].concat() |
| 107 | + part[backPoint] = this._cost(datas, t, backPoint) + this._penalty |
| 108 | + const cost = part.reduce((s, v) => s + v, 0) |
| 109 | + if (cost < bestCost) { |
| 110 | + bestPartition = part |
| 111 | + bestCost = cost |
| 112 | + } |
| 113 | + subpart.push(part) |
| 114 | + } |
| 115 | + |
| 116 | + partitions[backPoint] = bestPartition |
| 117 | + admissible = admissible.filter((_, i) => { |
| 118 | + return subpart[i] && subpart[i].reduce((s, v) => s + v, 0) <= bestCost + this._k |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + this._partitions = partitions[n] |
| 123 | + const pred = Array(datas.length).fill(false) |
| 124 | + this._partitions.forEach((_, i) => { |
| 125 | + if (i > 0 && i < datas.length) { |
| 126 | + pred[i] = true |
| 127 | + } |
| 128 | + }) |
| 129 | + return pred |
| 130 | + } |
| 131 | +} |
0 commit comments