Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the length of a segment in a logarithmic scale

I want to calculate the length of a line for a series of events.

I'm doing this with the following code.

var maxLineLength = 20;
var lineLen = function(x, max) {
    return maxLineLength * (x / max);
}
var events = [0.1, 1, 5, 20, 50];
var max = Math.max.apply(null, events);
events.map(function (x) {
    console.log(lineLen(x, max));
});

This works, but I'm using linear scaling, while I'd like to use logarithms, because I don't want small events to become too small numbers when big ones are present.

I modified the lineLen function as you can see below, but - obviously - it doesn't work for events equals to one, because the log of one is zero. I want to show events equals to one (opportunely scaled) and not make them become zero. I also need positive numbers to remain positive (0.1 becomes a negative number)

How should I modify lineLen to use a logarithmic scale?

var maxLineLength = 20;
var lineLen = function(x, max) {
   return maxLineLength * (Math.log(x) / Math.log(max));
}
var events = [0.1, 1, 5, 20, 50];
var max = Math.max.apply(null, events);
events.map(function (x) {
    console.log(lineLen(x, max));
});
like image 418
cdarwin Avatar asked Nov 08 '17 20:11

cdarwin


People also ask

How do you calculate log scale?

The equation y = log b (x) means that y is the power or exponent that b is raised to in order to get x. The common base for logarithmic scales is the base 10.

What does it mean for something to be measured on a logarithmic scale?

Definition of logarithmic scale : a scale on which the actual distance of a point from the scale's zero is proportional to the logarithm of the corresponding scale number rather than to the number itself — compare arithmetic scale.

How do you convert data to log scale?

To transform your data to logs: Click the Analyze button, choose built-in analyses, and then select Transforms from the list of data manipulations. Choose X = log(X). Also check the box at the bottom of the dialog to Create a New Graph of the results.

Is a logarithmic scale exponential?

A logarithmic scale shows exponential growth on a graph. It's a nonlinear scale that's frequently used for analyzing a large range of quantities compactly. It is extremely useful when graphing a large variance in data.


Video Answer


2 Answers

You can take log(x+1) instead of log(x), that doesn't change the value too much and the ratios are maintained for smaller numbers.

var maxLineLength = 20;
var lineLen = (x, max) => maxLineLength * Math.log(x+1)/Math.log(max+1);
var events = [ 0.1, 1, 5, 20, 50];
var visualizer = function(events){
    var max = Math.max.apply(null, events);
    return events.reduce((y, x) => {
         y.push(lineLen(x, max));
         return y;
    }, []);
};

console.log(visualizer(events));
like image 154
TheChetan Avatar answered Sep 30 '22 02:09

TheChetan


You can use an expression like Math.pow(x, 0.35) instead of Math.log(x). It keeps all values positive, and gives the behavior that you want for small ratios. You can experiment with different exponent values in the range (0,1) to find the one that fits your needs.

var maxLineLength = 20;
var exponent = 0.35;
var lineLen = function(x, max) {
   return maxLineLength * Math.pow(x/max, exponent);
}
var events = [0, 0.01, 0.1, 1, 5, 20, 50];
var max = Math.max.apply(null, events);
events.map(function (x) {
    console.log(lineLen(x, max));
});
like image 44
ConnorsFan Avatar answered Sep 30 '22 03:09

ConnorsFan