0

I'm getting a stream of numbers from a random walk, and I'm successfully getting the min and max from that random walk using this code:

const rw = require('random-walk')
const num = new rw

let nums = []

num.on("result", number => {
    nums.push(number.toFixed(4))

    let minmax = minMax(30,30)  // minMax(low-period, high-period) <-- Usage

    function minMax(l, h){

        let lows = nums.slice(-l) //slice a given range for highs
        let highs = nums.slice(-h) //slice a given range for lows
    
        let low = Math.min(...lows) //get the min for the range
        let high = Math.max(...highs) //get the max for the range
    
        return {low: low, high: high}
    }

    console.log(minmax)

    let ha = angle(number, minmax.high) // high angle
    let la = angle(minmax.low, number) // low angle
    function angle(a, h){
        return Math.acos(a/h)*180/Math.PI
    }

    if(la == 0 || ha == 0){
        nums = nums.slice(-30)
    }

})

num.get("walk", {rate: 50, base: 200, type: "positive", scale: 2000, pseudo: false})

What I'm trying to figure out, is how to further find a min and max of the maximums (A); and then find a min and a max of the minimums (B).

Please refer to this image for sections A and B in purple respectively:

enter image description here

I believe I need to set a new variable to the output of my minMax function but use smaller periods, but I'm not 100% sure. When I have tried this, I find that sometimes the max of the maximum range is also the max of the minimum range and the same is true for the min of the maximum and minimum ranges. Apologies if that's confusing. I hope the image makes it clear what I'm trying to accomplish.

Any thoughts about how to solve this problem would be great!

Dshiz
  • 3,099
  • 3
  • 26
  • 53
  • 1
    I'm not sure if intentional, but currently A and B refer to the same range. Meaning that the drawing is incorrect since it depicts A and B as different ranges (in the code). Even if you where to change the parameters of `minMax` A would a subset of B or the other way around, they are never truly separate at the moment. – 3limin4t0r Dec 08 '20 at 00:13
  • Yes, it's the same overall range that I'm successfully getting now. What I'm trying to get after getting that is the min and max of range A, and the min and max of range B, both of which would be separate (even if not exactly depicted in the drawing/code) from the first min/max. So, potentially three separate ranges: the first being the entire set of numbers, the second being a subset of the 1st (min to the next max above that), and the third being a subset of the 1st (max to the next min below that). Sorry if it's confusing.. Assume there are more peaks/valleys between all mins/maxs depicted. – Dshiz Dec 08 '20 at 00:19
  • 1
    The problem is, if there are more peaks/valleys how do you know when you finished checking for values. Is there a threshold that may not be passed? For example is the yellow "max" line of B always a set distance from the min of B? Is a percentage? How is the threshold determined? Or has it nothing to do with a threshold and do you simply want to take the next peak after "min" for B, even if the second peak after it could be higher? – 3limin4t0r Dec 08 '20 at 00:27
  • That is part of the problem I'm trying to figure out. What I have as a threshold is the angular difference between max and current, or min and current. I didn't show that in the code, because I don't know if I'm using it effectively. Basically I clear the nums array if the angle is zero for the current num against the min or max using `arccosine`. – Dshiz Dec 08 '20 at 00:32
  • I have added code for how I use `arccosine` and fixed a couple typos I introduced while doing that. – Dshiz Dec 08 '20 at 01:11

0 Answers0