Logarithmic Sampling

I am working with values between [minValue,maxValue] and I want to create a vector of values in between this range. But I want more values near to the minValue.

Example:

min = 1 max = 100

vector = [1,1.1,1.5,2,3,5,10,15,30,50,100];

Something like that.

The goal is to be more accurate around the minimum.

Is that possible to implement that?

2 Answers

You can start with by generating numbers from 0 to 1 with constant step (for example 0.1). Then power them with some exponent - the bigger exponent, the sharper curve. Then shift and multiply to get into your desired min-max range.

Pseudocode:

min = 1.0
max = 100.0
exponent = 2.0 // Sharpness
result = []

for(i = 0.0; i <= 1.0; i += 0.1) {
    result.push(pow(i, exponent) * (max - min) + min)
}

I had the same problem. I wanted well spaced points, but with much more point near the minimal value. I used a logarithmic transformation. Firstly the code:

function SampleData (min, max, points) {

    min = min || 1; // Minimum value
    max = max || 1600; // Maximum value
    points = points || 20; // data points between Min&Max

    var step = (Math.log(max)-Math.log(min))/(points-1);

    var data = [];

    var D= 100; // max asy
    var A= 0; // min asy
    var C= 50;  // inflectio
    var B= 1; // Hills slope

    for (i = Math.log(min); i <= Math.log(max); i=i+step) { 
        data.push ([Math.exp(i), math.eval (D+'+('+A+'-'+D+')/(1+('+math.exp(i)+'/'+C+')^'+B+')')]);
    }
}

The trick I used is to compress the data range (here 1 to 1600) with the logarithmic function; thereby, I was able to use a linear constant step width. Before feeding the x value into the math function, you have to back transform (math.exp) the values.

The function in math.eval is a rather complicated 4 paramater logistic fit, you might of course use something else.

In the image you see a plot of above mentioned function once with linear step width (orange) and once with my logarithmic step width (red). Visualisation of linear and logarithmic step width in data sampling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest