0

This is an example for a small dataset with 17 values. The graph to this values looks a bit like a gaussian distribution.

0
0.05
0.1
0.2
0.4
0.7
0.85
0.95
1
0.95
0.85
0.7
0.4
0.2
0.1
0.05
0

graph

Unfortunatly I also need to get values in between. So I need to find a formula, which describes nearly this kind of values - it doesn't have to fit the values exactly.

If I use f(2), I do get 0.1, if I do f(3) I do get 0.2, so obviously f(2.5) should be 0.15.

I tried to start with a parabola function:

f(x) = -x ** 2 + 1

But I would have to move it one to the right and also it doesn't work for the lower y values. I think this attempt will not work...

Update

const getValue = (x) => {
  const u = Math.floor(x)
  const res = (1 + u - x) * u + (x - u) * (u + 1)
  console.log(res)
  return res
}
  • What do you mean by "look like a Gaussian distribution" ? That the histogram of your values is close to a Gaussian curve ? – Beleth Jan 04 '24 at 15:53
  • @Beleth I've added the graph. I mean simple graph with the values in the post. – user3142695 Jan 04 '24 at 16:57
  • Am I miscounting? It looks to me like $f(1)=0$, $f(2)=.05$, and $f(3)=.1$, not $f(2)=.1$ and $f(3)=.2$ as you stated in the problem. –  Jan 04 '24 at 17:35

1 Answers1

1

You said $f(2)=.1$ and $f(3)=.2$, so $f(2.5)$ should be $.15$. So it sounds like you want linear interpolation. Fix $x$ between $1$ and $17$ and pick $u$ to be one of the points $1,2,\ldots,17$ such that $u\leqslant x\leqslant u+1$. Then $$x=(1+u-x)u+(x-u)(u+1),$$ so linear interpolation between $f(u)$ and $f(u+1)$ is $$f(x)=(1+u-x)f(u)+(x-u)f(u+1).$$

Since your datapoints at which you already know the functions are integers, if you actually want to code this up, once you have $x$, $u$ should be the floor of $x$.

In general, suppose we have two points $(u,u')$ and $(v,v')$ ($u\neq v$) and we want $f(x)$, where $f$ is a line going through $(u,u')$ and $(v,v')$. We can write $$x=\Bigl(\frac{v-x}{v-u}\Bigr)u+\Bigl(\frac{x-u}{v-u}\Bigr)v$$ and get $$f(x)=\Bigl(\frac{v-x}{v-u}\Bigr)u'+\Bigl(\frac{x-u}{v-u}\Bigr)v'.$$

If you're coding in python, numpy has a built in linear interpolation function (numpy.interp). This requires inputting an $x$ where you want the function value, an array of input values (for you, $(1,\ldots,17)$), and a list of output values (for you, $(0,.05,\ldots,0)$).

If you want a function that just takes $x$, you can use this code.

import numpy
ylist = [0,0.05,0.1,0.2,0.4,0.7,0.85,0.95,1,0.95,0.85,0.7,0.4,0.2,0.1,0.05,0]
xlist = range(1,1+len(ylist))
f = lambda x:np.interp(x,xlist,ylist)

You can specify values outside of the bounds (ie $x<1$ and $x>17$ in this case).

Instead of a single input $x$, you can pass in an array of $x$-values. The output will be the array of output values.

  • Thanks. I have to code in JS. There is no numpy function in JS – user3142695 Jan 04 '24 at 18:25
  • In that case, you could code the function $g(1)=0$, $g(2)=.05$, etc. Then for $x$, $u=floor(x)$ (I'm assuming you have a floor function). So you can plug this in $$f(x)=(1+floor(x)-x)g(floor(x)) + (x-floor(x))g(1+floor(x)).$$ That only works because your function is already defined on integers. –  Jan 04 '24 at 18:48
  • Sorry, I didn't get it. I've updated the post with the little function, but obviously I'm missing the value data. Could you explain to me where to use it? – user3142695 Jan 04 '24 at 20:00
  • In the function, definition, you have $$res = (1+u-x)u+(x-u)(u+1).$$ Assuming $1\leqslant x\leqslant 17$. This isn't quite what we want. For those $x$, $u=floor(x)$ will be an integer, which means we already know what the function value should be at $u$ and $u+1$ (say $y_u$ and $y_{u+1}$). Then we want $$res=(1+u-x)y_u+(x-u)y_{u+1}.$$ –  Jan 04 '24 at 20:07
  • For example, we already know based on the data set that we should have $f(4)=.2$ and $f(5)=.4$. For $4\leqslant x<5$, we will have $u=floor(x)=4$, and we want $$res=(1+u-x)(.2) + (x-u)(.4).$$ So in the definition of $res$, we aren't multiplying by $u$ (in this case, $4$), and $u+1=5$, we're multiplying by $f(u)=f(4)=.2$ and $f(u+1)=f(5)=.4$ –  Jan 04 '24 at 20:09