【问题标题】:Is there a method to find a Y value that is exceeded X % of the time in a time series?有没有一种方法可以找到时间序列中超过 X % 的 Y 值?
【发布时间】:2020-05-04 18:45:39
【问题描述】:

我正在尝试集思广益来计算以下场景的解决方案(在 js 中):

假设我得到了一个具有相当大方差的标准时间序列(例如https://codepen.io/quirkules/pen/wvBYarM)。这些时间序列会根据数据集而变化。

示例数据:

var data = [
  {
    "date": "30/04/2012", // DD/MM/YYYY
    "close": 14
  },
  {
    "date": "1/05/2012",
    "close": 2
  },
  {
    "date": "2/05/2012",
    "close": 14
  },
  {
    "date": "3/05/2012",
    "close": 5
  },
  {
    "date": "4/05/2012",
    "close": 14
  }

我希望能够识别正确的 Y 值,该值将超过 X % 的时间。例如,如果日期范围是 50 天,我想知道什么值将超过 50% 的时间,这意味着该 Y 值总共超过 25 天。

注意:根据数据集的性质,这可能需要是与所需 Y 值最接近的值。再看前面的例子,图的约束可能意味着我们只能通过尽最大努力找到超过 40% 的时间的 Y 值。

我假设这将是一个线性回归类型的问题,但我以前没有以这种方式使用它。我也不确定这是否可以使用纯 JS 或使用其他库(例如 tensorflow)来解决,但我愿意接受想法。

任何帮助将不胜感激

【问题讨论】:

  • 可能有更快的方法,但想到的方法是:按值降序对数组进行排序。现在sortedArray[Math.floor(targetPct*sortedArray.length) + 1] 将是当时超过targetPct 的值。如果有一堆具有相同值的条目,可能会出现边缘情况
  • 看看这个stackoverflow.com/questions/20811131/… 你正在寻找异常值。您不需要使用回归,回归是一种找到最佳拟合线的方法。
  • 感谢@Brandon 和 Happy Machine,我认为有更简单的方法

标签: javascript node.js tensorflow d3.js


【解决方案1】:

如果数据不是很大,一个好的旧 for 循环就足够简单了:https://codepen.io/Alexander9111/pen/rNaqwrg

document.querySelector("#search").addEventListener('click', function(e){
    var search_val = document.querySelector("#input").value;
    var times_over_val = 0;
    for (i = 0; i < data.length; i++){
        if (i >= search_val){
            times_over_val++;
        }
    }
    document.querySelector("#output").innerText = "The data was over the value of: " + search_val + ", " + Math.round(100 * (times_over_val/data.length),2) + "% of the time (" + times_over_val + " out of " + data.length + ")";
});

注意我修改了你的 HTML:

<div id="search-container">
    <input id="input" value=""/>
    <button id="search">Search</button>
    <span id="output">Enter a value in the input then click search</span>
</div>
<div id="container">
</div>

还有一行 d3 代码:

var svg = d3.select("#container").append("svg")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多