Make a parabola steeper at both sides while keeping both ends - python

I'm having a parabola with both axes being from 0 to 1 as follows:
The parabola is created and normalized with the following code:
import matplotlib.pyplot as plt
import numpy as np
# normalize array
def min_max_scale_array(arr):
arr = np.array(arr)
return (arr - arr.min())/(arr.max()-arr.min())
x = np.linspace(-50,50,100)
y = x**2
x = min_max_scale_array(x)
y = min_max_scale_array(y)
fig, ax = plt.subplots()
ax.plot(x, y)
I want to create another one with both ends being the same but both sides become steeper like this:
I thought of joining an exponential curve and its reflection but that would make the resulting parabola looks pointy at the bottom.
Can you show me how to achieve this? Thank you!

If you want to modify any arbitrary curve, you can change the x values, for example taking a power of it:
# x and y are defined
for factor in [1.1, 1.5, 2, 3, 4]:
x2 = 2*x-1
x3 = (abs(x2)**(1/factor))*np.sign(x2)/2+0.5
ax.plot(x3, y, label=f'{factor=}')
output:

You can change the exponent to get a steeper curve with the same value at the extremes. You need to pick a larger value that is an even integer (odd numbers won't give a parabola).
y = x**4

Related

How to find crossection of hline and a function in python?

I have created a plot like in the figure below, red plot is original data, blue is a fitted function, the horizontal lines are different levels. I need to find the both intersection points with each line. do you have any suggestions ? Thanks In advance.
An easy way to do this numerically is to subtract the y-value of each horizontal line from your fit and then to solve the equation
fit(x) - y = 0 for x.
For this, scipy.optimize.fsolve can be used as follows (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html):
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve # To find the zeros
from scipy.stats import cauchy
def my_fit(x):
# dummy function for this example
return cauchy().pdf(x)
horizontal_lines = [0.05, 0.15, 0.25]
colors = ['r', 'g', 'm']
x = np.linspace(-5, 5, 1000)
plt.plot(x, my_fit(x))
plt.hlines(horizontal_lines, -5, 5, ls="--", color=colors)
for i, y in enumerate(horizontal_lines):
x0 = fsolve(lambda x: my_fit(x) - y, -1)
x1 = fsolve(lambda x: my_fit(x) - y, 1)
print(f"Intersection points for {y=}: {x0=} {x1=}")
plt.scatter(x0, my_fit(x0), color=colors[i])
plt.scatter(x1, my_fit(x1), color=colors[i])
Output:
Intersection points for y=0.05: x0=array([-2.3165055]) x1=array([2.3165055])
Intersection points for y=0.15: x0=array([-1.05927612]) x1=array([1.05927612])
Intersection points for y=0.25: x0=array([-0.5227232]) x1=array([0.5227232])
A simple solution to this would be to just calculate the intersection points knowing the functions of the lines.
Example:
Line 1 : y = 2x
Line 2 : y = x^2
Intersection points:
2x = x^2
0 = x^2 - 2x
x1 = 2
x2 = 0
For y just substitute x in one of the functions,
y1 = 2x y1 = 2*(2) y1 = 4
y2 = 2x y2 = 2*(0) y2 = 0
Intersection point 1 of line 1 and line 2:
Itersection point 1: (2, 4)
Intersection point 2: (0,0)
enter image description here
Hope this helps.
The simplest non-mathematical solution would be:
take all points with y > y0
take the largest and smallest x from the remaining list.
The result is approximately correct for high point density.

How to plot curve with given polynomial coefficients?

using Python I have an array with coefficients from a polynomial, let's say
polynomial = [1,2,3,4]
which means the equation:
y = 4x³ + 3x² + 2x + 1
(so the array is in reversed order)
Now how do I plot this into a visual curve in the Jupyter Notebook?
There was a similar question:
Plotting polynomial with given coefficients
but I didn't understand the answer (like what is a and b?).
And what do I need to import to make this happen?
First, you have to decide the limits for x in your plot. Let's say x goes from -2 to 2. Let's also ask for a hundred points on our curve (this can be any sufficiently large number for your interval so that you get a smooth-looking curve)
Let's create that array:
lower_limit = -2
upper_limit = 2
num_pts = 100
x = np.linspace(lower_limit, upper_limit, num_pts)
Now, let's evaluate y at each of these points. Numpy has a handy polyval() that'll do this for us. Remember that it wants the coefficients ordered by highest exponent to lowest, so you'll have to reverse the polynomial list
poly_coefs = polynomial[::-1] # [4, 3, 2, 1]
y = np.polyval(poly_coefs, x)
Finally, let's plot everything:
plt.plot(x, y, '-r')
You'll need the following imports:
import numpy as np
from matplotlib import pyplot as plt
If you don't want to import numpy, you can also write vanilla python methods to do the same thing:
def linspace(start, end, num_pts):
step = (end - start) / (num_pts - 1)
return [start + step * i for i in range(num_pts)]
def polyval(coefs, xvals):
yvals = []
for x in xvals:
y = 0
for power, c in enumerate(reversed(coefs)):
y += c * (x ** power)
yvals.append(y)
return yvals

Interpolate between linear and nonlinear values

I have been able to interpolate values successfully from linear values of x to sine-like values of y.
However - I am struggling to interpolate the other way - from nonlinear values of y to linear values of x.
The below is a toy example
import matplotlib.pylab as plt
from scipy import interpolate
#create 100 x values
x = np.linspace(-np.pi, np.pi, 100)
#create 100 values of y where y= sin(x)
y=np.sin(x)
#learn function to map y from x
f = interpolate.interp1d(x, y)
With new values of linear x
xnew = np.array([-1,1])
I get correctly interpolated values of nonlinear y
ynew = f(xnew)
print(ynew)
array([-0.84114583, 0.84114583])
The problem comes when I try and interpolate values of x from y.
I create a new function, the reverse of f:
f2 = interpolate.interp1d(y,x,kind='cubic')
I put in values of y that I successfully interpolated before
ynew=np.array([-0.84114583, 0.84114583])
I am expecting to get the original values of x [-1, 1]
But I get:
array([-1.57328791, 1.57328791])
I have tried putting in other values for the 'kind' parameter with no luck and am not sure if I have got the wrong approach here. Thanks for your help
I guess the problem raises from the fact, that x is not a function of y, since for an arbitrary y value there may be more than one x value found.
Take a look at a truncated range of data.
When x ranges from 0 to np.pi/2, then for every y value there is a unique x value.
In this case the snippet below works as expected.
>>> import numpy as np
>>> from scipy import interpolate
>>> x = np.linspace(0, np.pi / 2, 100)
>>> y = np.sin(x)
>>> f = interpolate.interp1d(x, y)
>>> f([0, 0.1, 0.3, 0.5])
array([0. , 0.09983071, 0.29551713, 0.47941047])
>>> f2 = interpolate.interp1d(y, x)
>>> f2([0, 0.09983071, 0.29551713, 0.47941047])
array([0. , 0.1 , 0.3 , 0.50000001])
Maxim provided the reason for this behavior. This interpolation is a class designed to work for functions. In your case, y=arcsin(x) is only in a limited interval a function. This leads to interesting phenomena in the interpolation routine that interpolates to the nearest y-value which in the case of the arcsin() function is not necessarily the next value in the x-y curve but maybe several periods away. An illustration:
import numpy as np
import matplotlib.pylab as plt
from scipy import interpolate
xmin=-np.pi
xmax=np.pi
fig, axes = plt.subplots(3, 3, figsize=(15, 10))
for i, fac in enumerate([2, 1, 0.5]):
x = np.linspace(xmin * fac, xmax*fac, 100)
y=np.sin(x)
#x->y
f = interpolate.interp1d(x, y)
x_fit = np.linspace(xmin*fac, xmax*fac, 1000)
y_fit = f(x_fit)
axes[i][0].plot(x_fit, y_fit)
axes[i][0].set_ylabel(f"sin period {fac}")
if not i:
axes[i][0].set_title(label="interpolation x->y")
#y->x
f2 = interpolate.interp1d(y, x)
y2_fit = np.linspace(.99 * min(y), .99 * max(y), 1000)
x2_fit = f2(y2_fit)
axes[i][1].plot(x2_fit, y2_fit)
if not i:
axes[i][1].set_title(label="interpolation y->x")
#y->x with cubic interpolation
f3 = interpolate.interp1d(y, x, kind="cubic")
y3_fit = np.linspace(.99 * min(y), .99 * max(y), 1000)
x3_fit = f3(y3_fit)
axes[i][2].plot(x3_fit, y3_fit)
if not i:
axes[i][2].set_title(label="cubic interpolation y->x")
plt.show()
As you can see, the interpolation works along the ordered list of y-values (as you instructed it to), and this works particularly badly with cubic interpolation.

given percentiles find distribution function python

From https://stackoverflow.com/a/30460089/2202107, we can generate CDF of a normal distribution:
import numpy as np
import matplotlib.pyplot as plt
N = 100
Z = np.random.normal(size = N)
# method 1
H,X1 = np.histogram( Z, bins = 10, normed = True )
dx = X1[1] - X1[0]
F1 = np.cumsum(H)*dx
#method 2
X2 = np.sort(Z)
F2 = np.array(range(N))/float(N)
# plt.plot(X1[1:], F1)
plt.plot(X2, F2)
plt.show()
Question: How do we generate the "original" normal distribution, given only x (eg X2) and y (eg F2) coordinates?
My first thought was plt.plot(x,np.gradient(y)), but gradient of y was all zero (data points are evenly spaced in y, but not in x) These kind of data is often met in percentile calculations. The key is to get the data evenly space in x and not in y, using interpolation:
x=X2
y=F2
num_points=10
xinterp = np.linspace(-2,2,num_points)
yinterp = np.interp(xinterp, x, y)
# for normalizing that sum of all bars equals to 1.0
tot_val=1.0
normalization_factor = tot_val/np.trapz(np.ones(len(xinterp)),yinterp)
plt.bar(xinterp, normalization_factor * np.gradient(yinterp), width=0.2)
plt.show()
output looks good to me:
I put my approach here for examination. Let me know if my logic is flawed.
One issue is: when num_points is large, the plot looks bad, but it's a issue in discretization, not sure how to avoid it.
Related posts:
I failed to understand why the answer was so complicated in https://stats.stackexchange.com/a/6065/131632
I also didn't understand why my approach was different than Generate distribution given percentile ranks

Semilogy interpolation; get x from y

I have the following data:
x = [0, 2, 4, 8, 30]
y = [1.2e-3, 3.5e-4, 5.1e-5, 1.6e-5, 2e-7]
I'm trying to interpolate to get y from a given x value.
When plotted the data looks like:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
ax.semilogy(x, y, 'o-')
plt.show()
So say I'm interested in finding what value of x is for a y value of 3e-5.
I can get the x value of a given y by:
z = np.linspace(0, 30, 10000)
logy = np.log10(y)
yy = np.power(10.0, np.interp(z, x, logy))
z[np.isclose(3.5e-5, yy, atol=1e-8)]
Out:
array([5.29852985])
But I have to adjust the atol if I change the value to get a single match and also have to create a load more data points to get the resolution.
Is there a simpler way to do this? Thanks.
Let's say you want to find the x_f corresponding to y_f. Assuming the entries in your original list y are in strictly decreasing order and the entries in x are increasing, you find the first entry in y that is less than or equal to your y_0. Say it is the one at index i, so the x,y tuples that make up your the relevant linear function from your piecewise partition will be (x[i-1],y[i-1]) and (x[i], y[i]).
Using the formula for the line given two points on the line we can get x_f:
x_f = x[i-1] + (x[i]-x[i-1])/(y[i]-y[i-1])*(y_f-y[i-1])

Categories