What is the mathematically correct way to proportionally increment these numbers in javascript?

Viewed 155

I am building a random walk generator based on the Box Müller transform. The transform produces numbers from a starting point of 0 to create "trend-normal" normalized random walk in both positive and negative directions linearly. What I want to do is apply the results of that to a base number, say 100, but apply it proportionally for that particular number size. By "apply", I think I want to multiply, but I'm not 100% sure.

As an example, if the base number is 1, I wouldn't want to necessarily add the random walk result 2.145... to it, but perhaps 0.02145... to it. Or if the base number is 1000, I would want to add perhaps 21.45... to it.

I wrote the following which seems to work correctly by eyeballing the results:

let result = boxMullerResult // results from my boxMuller random walk
let base = 100 // set a starting number

// apply the results to the starting number
// not sure if this function is even needed, because there could be simple math for it?
function getMultiplier(base) {
    let digits = Math.floor(Math.log10(base)) + 1 // get a count of digits in base
    return digits == 6 ? 100000
         : digits == 5 ? 10000
         : digits == 4 ? 1000
         : digits == 3 ? 100
         : digits == 2 ? 10
         : digits == 1 ? 1
         : 0.01
}
let multiplier = getMultiplier(base)
result = base+((result/base)*multiplier) //not sure of this math, really
console.log(Math.abs(result)) // Math.abs for cryptocurrency example (price can not be negative)

Sample output:

102.65786245109346
102.80789090168939
103.31327926664707
103.81866763160474
104.01517135943189
104.21167508725905
104.25864043241288
104.3056057775667
104.59017554334307
104.87474530911942
105.39312728890096
105.91150926868248
105.97736617138204
106.04322307408161
105.57535946204703
105.10749585001244
105.31044946513785
105.51340308026325
105.42995897505484
105.34651486984644
104.94740888496884
104.54830290009122
100.58712054941161
101.17424109882323
101.29026537304657
101.4062896472699
101.02832352564248
100.65035740401505

But I'm not sure if it's mathematically accurate.

To further help clarify my question, let's say I'm simulating a cryptocurrency price movement and the base price is 13,016.30. When I get my random walk result, I want to fairly realistically apply that result to the base price for its given size.

So, What is the mathematically correct way to apply my random walk result proportionally to a given base number? I'm not a mathematician, and I don't know the correct name for the concept I'm trying to describe, and apologize for that. Any assistance on the math and/or code is greatly valued.

1 Answers

What you are describing is percentage change.

2.145% of 1 is 0.02145 and 2.145% of 1000 is 21.45.

Therefore just treat it as percentage change:

function percent (percentage, base) {
    return percentage * base / 100; // the usual percentage calculation we
                                    // learn at school and do in our heads
                                    // when shopping
}

function applyPercentageChange (percentage, value) {
    return value + percent(pecentage,value); // add the percentage change
}

This is basically your "apply" function.

It does not have to be percentage. You can use any fractional system. For example, instead of percentage you can scale it by part per score (part per twenty):

function partPerScore (change, base) {
    return change * base / 20; // 20 instead of 100
}

function applyScaledChange (change, value) {
    return value + partPerScore(change,value); // add the percentage change
}

Your base+((result/base)*multiplier) is almost correct except it should be:

base+(result*(base/multiplier))

If you are not familiar with this form of the formula then this version may be more intuitive:

base+((result/multiplier)*base)

If you change the multiplier to 100 you can clearly see that it's percentage:

base+((result/100)*base)
Related