Graphing a sorted array of values such that each x-value is the index of the value and the y-value represents distance from the mean, while 0 < y < 1?

13 Views Asked by At

This might be an incredibly simple concept, but I don't have a background in stats (if this is even statistics related) beyond one Statistics class in college many moons ago, so I apologize in advance if this is well documented, I'm just not familiar with this area whatsoever.

Essentially, I have a sorted set of positive integers. I want to create a bar chart that represents the distribution of values, with the peak being the mean and I want the $x$-axis to be the sorted list of integers, with the height of each bar inversely representing the distance from the mean.

An example might look something like this: enter image description here

Note that the height difference between $e$ and $f$ is greater than the difference between $c$ and $b,$ because $f-e > b-c$.

So essentially the higher the $Y$ value, the closer it is to the mean. Although in my example it might've been better to graph $g$ as zero, since it has the highest distance from the mean.

So if the $x$ axis would look like: $$[1,2,3,5,5,7,11]$$ Then the $y$ axis might look something like:

$$[0.2, 0.5, 0.75, 0.95, 0.95, 0.45, 0.0]$$

Is there some kind of simple formula or equation I'm missing to calculate a $y$ value that represents this?

1

There are 1 best solutions below

0
On

I ate some food, sat down and thought about it for a bit and ended up with this solution:

Essentially calculate mean. Then calculate which is higher, mean - min or mean + max, and call that greatest distance. Now for each value, I subtract the greatest distance from the value, or I subtract the value from the greatest distance. This results in the value that has the greatest distance being 1, and the mean ends up being 0. I then just subtract that value from 1 and that's the value at each point.

Not sure how to write it mathematically, but codewise I ended up with this quick javascript snippet:

const testData = [1, 1, 2, 3, 4, 5, 7, 12, 15, 18, 23, 28, 29, 31, 33, 36, 48, 52];

const sum = testData.reduce((acc, curr) => {
  acc = acc + curr;
  return acc;
}, 0)

const mean = sum / testData.length;
const greatestDistance = Math.max(mean - testData[0], testData[testData.length - 1] - mean)

const finalValues = testData.map(v => {
  const distanceFromMean = v > mean ? v - mean : mean - v;
  return 1 - (value / greatestDistance);
})

// Test print
finalValues.forEach(v => {
  const dots = v * 100;
  let dotLine = '';
  for(let i = 0; i < dots; i++){
    dotLine+= '.'
  }
  console.log(dotLine);
})

// result
/*
............................................
............................................
...............................................
...................................................
......................................................
.........................................................
...............................................................
..............................................................................
.......................................................................................
................................................................................................
.........................................................................................
..........................................................................
.......................................................................
.................................................................
...........................................................
.................................................
.............

*/