【问题标题】:volume distribution in rolling window滚动窗口中的体积分布
【发布时间】:2023-01-03 19:31:53
【问题描述】:

我想在 7 天的窗口中每天进行体积分布。

例如第一周:

  • 在星期一我在不同时间得到 4 件物品,每件 1 件,
  • 周二 3 项 1 等

并相应地改变状态。

Mon Tue Wed Thu Fri Sat Sun
4 3 5 0 4 3 1

但问题是当第二周到来时,我希望星期一从 0 开始(并继续求和),其他日子被保留,其他日子也一样,当新的星期二到来时它从 0 开始......等.有什么聪明的方法可以做到这一点吗?

我可以很容易地识别出项目需要用模运算符降落的“箱子”,但我想不出某种方法来知道每一天究竟什么时候需要从 0 开始,因为“新星期一”已经到来(并继续总结星期一新东西)。

【问题讨论】:

    标签: javascript arrays mathematical-optimization moving-average rolling-computation


    【解决方案1】:

    当我使用时间戳时,夏令时可能会出现问题,我对此表示怀疑。但这似乎有效。如果某些代码看起来很奇怪,这可能是因为它是为了适应 solidity 而没有 javascript 的所有优点

    class rollingWindow {
          constructor() {
            this.refTime = new Date("01/01/2023 00:00:00");
            // bins - days of the week (starting from Sunday)
            this.distribution = {
              0: { vol: 0, week: 0 },
              1: { vol: 0, week: 0 },
              2: { vol: 0, week: 0 },
              3: { vol: 0, week: 0 },
              4: { vol: 0, week: 0 },
              5: { vol: 0, week: 0 },
              6: { vol: 0, week: 0 }
            };
          }
        }
        
        rollingWindow.prototype.add = function (volume, datetime) {
          let diff = (datetime.getTime() - this.refTime.getTime());
          let bin = (diff / 1000/60/60/24)  % 7;
         console.log("bin:" + bin); // day of the week
         
          const floorargm = diff / (7 * 24 * 60 * 60 * 1000)
          let currentWeek = floorargm - (floorargm % 1)
        
        
          console.log("Week:"+currentWeek);
        
          if (this.distribution[bin].week < currentWeek) {
            this.distribution[bin].vol = 0;
            this.distribution[bin].week = currentWeek;
          }
          this.distribution[bin].vol += volume;
        };
        
        rollingWindow.prototype.get = function(datetime) {
          let diff = (datetime.getTime() - this.refTime.getTime());
          let bin = (diff / 1000/60/60/24)  % 7;
          const floorargm = diff / (7 * 24 * 60 * 60 * 1000);
          let currentWeek = floorargm - (floorargm % 1);
        
          // sum left bins and current bin
          let sum=0;
          for (let i = bin; i >= 0; i--) {
            if (this.distribution[i].week === currentWeek) {
                sum+= this.distribution[i].vol;
            }
            if(i === 0) {
              break;
            }
          }
          // sum right bins
          for (let i = bin + 1; i < 7;i++) {
            if (this.distribution[i].week === currentWeek - 1) {
              sum+= this.distribution[i].vol;
            }
          }
        
          return sum;
        }
        let rolling = new rollingWindow()
        
        rolling.add(1, new Date("01/01/2023 00:00:00"));
        rolling.add(1, new Date("01/02/2023 00:00:00"));
        rolling.add(1, new Date("01/02/2023 00:00:00"));
        rolling.add(1, new Date("01/03/2023 00:00:00"));
        rolling.add(1, new Date("01/04/2023 00:00:00"));
        rolling.add(1, new Date("01/05/2023 00:00:00"));
        rolling.add(1, new Date("01/06/2023 00:00:00"));
        rolling.add(1, new Date("01/07/2023 00:00:00"));
        rolling.add(1, new Date("01/08/2023 00:00:00"));
        
        let sum = rolling.get(new Date("01/08/2023 00:00:00"));
        
        
        console.log(sum);
    

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2015-08-26
      • 2014-08-14
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多