Shortcuts

fit

alpsplot.utils.fit.poly_fit(x, y, x_grid, degree=1)[source]

Use a polynomial of certain degree to fit (x, y) series and return y_grid wrt x_grid.

Parameters:
Returns:

numpy.ndarray – The y_grid array wrt x_grid.

Example:
import numpy as np
import matplotlib.pyplot as plt
from alpsplot.utils.fit import poly_fit

x = np.arange(10, step=0.5)
x_grid = np.arange(10, step=0.1)
y = x + 3 * np.sin(x)
y1_grid = poly_fit(x, y, x_grid)
y2_grid = poly_fit(x, y, x_grid, degree=4)

plt.scatter(x, y, color='red')
plt.plot(x_grid, y1_grid, color='green', label='degree = 1')
plt.plot(x_grid, y2_grid, color='blue', label='degree = 4')
plt.legend()
plt.show()
../_images/poly_fit.svg
alpsplot.utils.fit.tanh_fit(x, y, x_grid, degree=1, mean_offset=0.0, eps=1e-05)[source]

Use a tanh(polynomial) of certain degree to fit (x, y) series and return y_grid wrt x_grid.

{μ=min(y)+max(y)2+mean_offsetσ=max(yμ)(1+ε)y^=σtanh(p(x))+μ\begin{cases} \mu &= \frac{min(y) + max(y)}{2} + mean\_offset \\ \sigma &= \max(|y - \mu|) * (1 + \varepsilon) \end{cases} \\ \hat{y} = \sigma \cdot \tanh(p(x)) + \mu
Parameters:
  • x (numpy.ndarray) – The data to fit.

  • y (numpy.ndarray) – The data to fit.

  • x_grid (numpy.ndarray) – The x grid array.

  • degree (int) – The degree of polynomial to fit. Defaults to 1.

  • mean_offset (float) – The mean offset. Defaults to 0.0.

  • eps (float) – ε\varepsilon to control the scale. Defaults to 1e-5.

Returns:

numpy.ndarray – the y_grid array wrt x_grid.

Example:
import numpy as np
import matplotlib.pyplot as plt
from alpsplot.utils.fit import tanh_fit

x = np.arange(3, 4, step=0.1)
x_grid = np.arange(3, 4, step=0.02)
y = 3 * np.tanh(x**3 - 3 * x**2 - 7) + 4
y1_grid = tanh_fit(x, y, x_grid)
y2_grid = tanh_fit(x, y, x_grid, mean_offset=0.5)
y3_grid = tanh_fit(x, y, x_grid, degree=3)

plt.scatter(x, y, color='red')
plt.plot(x_grid, y1_grid, color='green', label='degree = 1')
plt.plot(x_grid, y2_grid, color='orange', label='degree = 1 (offset)')
plt.plot(x_grid, y3_grid, color='blue', label='degree = 3')
plt.legend()
plt.show()
../_images/tanh_fit.svg
alpsplot.utils.fit.atan_fit(x, y, x_grid, degree=1, mean_offset=0.0, eps=1e-05)[source]

Use a tanh(polynomial) of certain degree to fit (x, y) series and return y_grid wrt x_grid.

{μ=min(y)+max(y)2+mean_offsetσ=max(yμ)(1+ε)y^=σarctan(p(x))+μ\begin{cases} \mu &= \frac{min(y) + max(y)}{2} + mean\_offset \\ \sigma &= \max(|y - \mu|) * (1 + \varepsilon) \end{cases} \\ \hat{y} = \sigma \cdot \arctan(p(x)) +\mu
Parameters:
  • x (numpy.ndarray) – The data to fit.

  • y (numpy.ndarray) – The data to fit.

  • x_grid (numpy.ndarray) – The x grid array.

  • degree (int) – The degree of polynomial to fit. Defaults to 1.

  • mean_offset (float) – The mean offset. Defaults to 0.0.

  • eps (float) – ε\varepsilon to control the scale. Defaults to 1e-5.

Returns:

numpy.ndarray – The y_grid array wrt x_grid.

Example:
import numpy as np
import matplotlib.pyplot as plt
from alpsplot.utils.fit import atan_fit

x = np.arange(3, 4, step=0.1)
x_grid = np.arange(3, 4, step=0.02)
y = 3 * np.arctan(x**3 - 3 * x**2 - 7) + 4
y1_grid = atan_fit(x, y, x_grid)
y2_grid = atan_fit(x, y, x_grid, mean_offset=0.5)
y3_grid = atan_fit(x, y, x_grid, degree=3)

plt.scatter(x, y, color='red')
plt.plot(x_grid, y1_grid, color='green', label='degree = 1')
plt.plot(x_grid, y2_grid, color='orange', label='degree = 1 (offset)')
plt.plot(x_grid, y3_grid, color='blue', label='degree = 3')
plt.legend()
plt.show()
../_images/atan_fit.svg
alpsplot.utils.fit.inverse_fit(x, y, x_grid, degree=1, sign=None, eps=1e-05)[source]

Use a tanh(polynomial) of certain degree to fit (x, y) series and return y_grid wrt x_grid.

y^=1p(x)ε+{min(y)if signmax(y)elsey^=1p(x)+ε\hat{y} = \frac{1}{p(x)} - \varepsilon + \begin{cases} \min(y) &\text{if } sign \\ \max(y) &\text{else} \end{cases} \hat{y}=\frac{1}{p(x)}+\varepsilon
Parameters:
  • x (numpy.ndarray) – The data to fit.

  • y (numpy.ndarray) – The data to fit.

  • x_grid (numpy.ndarray) – The x grid array.

  • degree (int) – The degree of polynomial to fit. Defaults to 1.

  • sign (bool) – Whether reciprocal is positive, None means auto pick. Defaults to None.

  • eps (float) – ε\varepsilon. Defaults to 1e-5.

Returns:

numpy.ndarray – the y_grid array wrt x_grid.

Example:
import numpy as np
import matplotlib.pyplot as plt
from alpsplot.utils.fit import inverse_fit

fig, (ax1, ax2) = plt.subplots(1,2)

x = np.arange(1, 2, step=0.1)
x_grid = np.arange(1, 2, step=0.01)
y = 6 / (x**3 - 2 * x**2 + 4 * x - 2) + 4
y1_grid = inverse_fit(x, y, x_grid, eps=1)
y2_grid = inverse_fit(x, y, x_grid, degree=3, eps=1)

ax1.scatter(x, y, color='red')
ax1.plot(x_grid, y1_grid, color='green', label='degree = 1')
ax1.plot(x_grid, y2_grid, color='blue', label='degree = 3')
ax1.legend()

y1_grid = inverse_fit(x, -y, x_grid, eps=1)
y2_grid = inverse_fit(x, -y, x_grid, degree=3, eps=1)

ax2.scatter(x, -y, color='red')
ax2.plot(x_grid, y1_grid, color='green', label='degree = 1')
ax2.plot(x_grid, y2_grid, color='blue', label='degree = 3')
ax2.legend()

plt.show()
../_images/inverse_fit.svg
alpsplot.utils.fit.interp_fit(x, y, x_grid, **kwargs)[source]

Use scipy.interpolate.UnivariateSpline() to fit (x, y) series and return y_grid wrt x_grid.

Parameters:
Returns:

numpy.ndarray – The y_grid array wrt x_grid.

Example:
import numpy as np
import matplotlib.pyplot as plt
from alpsplot.utils.fit import interp_fit

x = np.arange(10, step=0.5)
x_grid = np.arange(10, step=0.1)
y = x + 3 * np.sin(x)
y_grid = interp_fit(x, y, x_grid)

plt.scatter(x, y, color='red')
plt.plot(x_grid, y_grid, color='green')
plt.legend()
plt.show()
../_images/interp_fit.svg

Docs

Access comprehensive developer documentation for AlpsPlot

View Docs