UNPKG

@formant/ava

Version:

A framework for automated visual analytics.

55 lines (54 loc) 2.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTrendInfo = exports.findTimeSeriesTrend = void 0; var tslib_1 = require("tslib"); var regression_1 = tslib_1.__importDefault(require("regression")); var lodash_1 = require("lodash"); var algorithms_1 = require("../../algorithms"); var util_1 = require("../util"); var constant_1 = require("../../constant"); function findTimeSeriesTrend(series, trendParameter) { var _a; var significance = (_a = trendParameter === null || trendParameter === void 0 ? void 0 : trendParameter.threshold) !== null && _a !== void 0 ? _a : constant_1.SIGNIFICANCE_LEVEL; var testResult = algorithms_1.trendDirection.mkTest(series, significance); var pValue = testResult.pValue, trend = testResult.trend; var regressionResult = regression_1.default.linear(series.map(function (item, index) { return [index, item]; })); var r2 = regressionResult.r2, points = regressionResult.points, equation = regressionResult.equation; return { trend: trend, significance: 1 - pValue, regression: { r2: r2, points: points.map(function (item) { return item[1]; }), equation: equation, }, }; } exports.findTimeSeriesTrend = findTimeSeriesTrend; var getTrendInfo = function (props) { var _a; var valid = (0, util_1.preValidation)(props); var insightType = 'trend'; if ((0, lodash_1.isString)(valid)) return (0, util_1.getNonSignificantInsight)({ detailInfo: valid, insightType: insightType, infoType: 'verificationFailure' }); var _b = (0, util_1.getAlgorithmCommonInput)(props), dimension = _b.dimension, values = _b.values, measure = _b.measure; if ((0, lodash_1.isNil)(dimension) || (0, lodash_1.isNil)(measure)) return (0, util_1.getNonSignificantInsight)({ detailInfo: 'Measure or dimension is empty.', insightType: insightType, infoType: 'verificationFailure', }); var trendParameter = (0, lodash_1.get)(props, 'options.algorithmParameter.trend'); var result = findTimeSeriesTrend(values, trendParameter); return [ tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, result), { type: 'trend', dimension: dimension, measure: measure }), (result.trend === 'no trend' ? { significantInsight: false, info: "The Mann-Kendall (MK) test does not pass at the specified significance level ".concat((_a = trendParameter === null || trendParameter === void 0 ? void 0 : trendParameter.threshold) !== null && _a !== void 0 ? _a : constant_1.SIGNIFICANCE_LEVEL, "."), } : { significantInsight: true, })), ]; }; exports.getTrendInfo = getTrendInfo;