ta-pattern-lib
Version:
Technical Analysis and Backtesting Framework for Node.js
104 lines • 3.73 kB
JavaScript
import round from "lodash.round";
export const stats_mean = (data) => {
const sum = data.reduce((acc, val) => acc + val, 0);
return sum / data.length;
};
export const stats_median = (numbers) => {
const sorted = Array.from(numbers).sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
return (sorted[middle - 1] + sorted[middle]) / 2;
}
return sorted[middle];
};
export const stats_standard_deviation = (numbers) => {
const n = numbers.length;
// Calculate the mean
const mean = numbers.reduce((sum, num) => sum + num, 0) / n;
// Calculate the sum of squared differences
const squared_differences_sum = numbers.reduce((sum, num) => sum + Math.pow(num - mean, 2), 0);
// Calculate the variance
const variance = squared_differences_sum / n;
// Calculate the standard deviation (square root of variance)
return Math.sqrt(variance);
};
export const stats_normalize = (data) => {
const min = Math.min(...data);
const max = Math.max(...data);
return data.map((value) => (value - min) / (max - min));
};
export const stats_resample_data = (data, new_length) => {
const step = (data.length - 1) / (new_length - 1);
return Array.from({ length: new_length }, (_, i) => {
const index = i * step;
const lower_index = Math.floor(index);
const upper_index = Math.ceil(index);
const weight = index - lower_index;
return data[lower_index] * (1 - weight) + data[upper_index] * weight;
});
};
export const stats_calculate_similarity = (sample, pattern) => {
const correlation = correlate(sample, pattern);
return Math.max(...correlation) / (pattern.length * stats_standard_deviation(sample) * stats_standard_deviation(pattern));
};
export const correlate = (a, b) => {
const result = [];
for (let i = 0; i <= a.length - b.length; i++) {
let sum = 0;
for (let j = 0; j < b.length; j++) {
sum += a[i + j] * b[j];
}
result.push(sum);
}
return result;
};
export const stats_min_max_scaling = (array, key) => {
let min_value = Number.POSITIVE_INFINITY;
let max_value = Number.NEGATIVE_INFINITY;
// Find the minimum and maximum values of the specified key
for (const obj of array) {
const value = obj[key];
if (value < min_value) {
min_value = value;
}
if (value > max_value) {
max_value = value;
}
}
// Scale the values to the range [0, 1]
const range = max_value - min_value;
for (const obj of array) {
const value = obj[key];
obj[key] = round((value - min_value) / range, 2);
}
return [...array];
};
export const compute_ema = (numbers, period, multiplier = 2) => {
const ema_array = [];
const multiplier_value = multiplier / (period + 1);
let ema = round(numbers[0], 3);
ema_array.push(ema);
for (let i = 1; i < numbers.length; i++) {
ema = round((numbers[i] - ema) * multiplier_value + ema, 3);
if (ema !== undefined) {
ema_array.push(ema);
}
}
return ema_array;
};
export const stats_sma = (numbers, period) => {
const sma_array = [];
for (let i = 0; i <= numbers.length - period; i++) {
const subset = numbers.slice(i, i + period);
const sum = subset.reduce((acc, num) => acc + num, 0);
const sma = sum / period;
if (sma !== undefined) {
sma_array.push(sma);
}
}
return sma_array;
};
export const stats_calculate_slope = (point1, point2, distance) => {
return (point2 - point1) / distance;
};
//# sourceMappingURL=feature_statistics.js.map