<p>If you read <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html" rel="nofollow noreferrer">the documentation</a> for the <code>curve_fit</code> function, you’ll see that it has an optional parameter (<code>p0</code>) for the initial guess for the parameter values. Since you didn’t provide anything for this, it defaults to 1 for all parameter values. Since these were then returned as the output, it means the fit failed to converge (probably because [1,1,1,1] is much too far away from the true values). Try running it with sensible initial-guess values for the parameters.</p>

CC BY-SA 4.0
2022-05-10T09:40:14.947Z
I have tried arround with different guesses for the initial values, and my results differ by a lot. The problem I keep getting is that even with the initial parameter guesses relatively close the fit data still differs significantly from my real data. Also small changes in my initial guesses give me huge differences in my final fit. f.e. initial guess [1250, 200, 0, 0] gives me the fit [ 929.68956083 1277.90277059 -10.92366431 9.70117469] which is wrong. contrary the initial guess [1000, 100, 1, 1] results in the fit [-387.78 720.37 7.72 5.54] which is also totally off
trynerror
CC BY-SA 4.0
2022-05-10T13:48:42.860Z
I don't know if I can help at this point. There may be something pathological about your "data" (aside from it being smooth and noiseless) and the fact that you haven't defined any sigma values, though if I define "data" as `xx = np.arange(500, 2250, 1)` and `yy = losvd_param(xx, 1300, 300, 0, 0)` and then call `p = curve_fit(losvd_param, xx, yy, p0=[1250, 250, 0, 0])` (smooth "data", no sigmas), I get [1300, 300, 1e-10, 2e-10]. (And similarly for p0=[1000,100,0,0].)
Peter Erwin
CC BY-SA 4.0
2022-05-06T11:53:13.727Z
2022-05-06T11:53:13.727Z
Peter Erwin
<p>I have recovered the following LOSVD from an elliptical galaxy, that I now want to fit a gauss-hermite-parametrization to and derive the kinematical parameters rotational velocity, velocity dispersion and hermite moments h3 and h4. <a href="https://i.sstatic.net/WSlQs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WSlQs.png" alt="enter image description here" /></a>
The gauss hermite parametrization consists of a classical gaussian and hermite polynoms. I am only looking at the hermite moments h3 (skewness of the curve, asymmetric) and h4 (kurtosis, symmetric). The parametrization I want to use has the following formula (everything is a function of velocities v).</p>
<p><a href="https://i.sstatic.net/kORJb.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kORJb.jpg" alt="enter image description here" /></a></p>
<p>I have written this function in python:</p>
<pre><code>    def losvd_param(v, v_rot, v_disp, h3, h4):
    &quot;&quot;&quot;parametrized LOSVD, to create a synthetic galaxy spectrum&quot;&quot;&quot;
    y = np.asarray((np.asarray(v)-v_rot)/(v_disp))        # define new variably y for compact notation
    return (np.exp(-0.5 * y**2) * (1 + h3*((2*np.sqrt(2)*y**3-3*np.sqrt(2)*y)/np.sqrt(6)) + h4*((4*y**4-12*y**2+3)/np.sqrt(24))))
</code></pre>
<p>This parametrization should work fine plugging in approximate values reading off the data:</p>
<pre><code>plt.plot(vel_corr_peak, losvd_param(vel_corr_peak, 1318, 300, 0, 0), label='read of fit')
</code></pre>
<p>Resulting in the plot:</p>
<p><a href="https://i.sstatic.net/cnPsl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cnPsl.png" alt="enter image description here" /></a></p>
<p>If I know try to fit using curve_fit from the scipy.optimize library, I dont get the values that I expect at all (saying I get 1s for all parameters).</p>
<pre><code>gh_moments = curve_fit(losvd_param, vel_corr_peak, broadening_func)[0]
print(gh_moments)
</code></pre>
<p>Output: [1. 1. 1. 1.] and not as expected roughly [1318, 300, 0, 0] h3 and h4 can have small positive or negative values. Can anybody tell me what could be the problem here ?</p>

<observational-astronomy><galaxy><data-analysis><spectra><python>
How to fit LOSVD of a galaxy with gauss hermite parametrization
trynerror
# How to fit LOSVD of a galaxy with gauss hermite parametrization

(Asked by: username_0 on 2022-05-05)

I have recovered the following LOSVD from an elliptical galaxy, that I now want to fit a gauss-hermite-parametrization to and derive the kinematical parameters rotational velocity, velocity dispersion and hermite moments h3 and h4. 
The gauss hermite parametrization consists of a classical gaussian and hermite polynoms. I am only looking at the hermite moments h3 (skewness of the curve, asymmetric) and h4 (kurtosis, symmetric). The parametrization I want to use has the following formula (everything is a function of velocities v).

I have written this function in python:
    def losvd_param(v, v_rot, v_disp, h3, h4):
    """parametrized LOSVD, to create a synthetic galaxy spectrum"""
    y = np.asarray((np.asarray(v)-v_rot)/(v_disp))        # define new variably y for compact notation
    return (np.exp(-0.5 * y**2) * (1 + h3*((2*np.sqrt(2)*y**3-3*np.sqrt(2)*y)/np.sqrt(6)) + h4*((4*y**4-12*y**2+3)/np.sqrt(24))))

This parametrization should work fine plugging in approximate values reading off the data:
plt.plot(vel_corr_peak, losvd_param(vel_corr_peak, 1318, 300, 0, 0), label='read of fit')

Resulting in the plot:

If I know try to fit using curve_fit from the scipy.optimize library, I dont get the values that I expect at all (saying I get 1s for all parameters).
gh_moments = curve_fit(losvd_param, vel_corr_peak, broadening_func)[0]
print(gh_moments)

Output: [1. 1. 1. 1.] and not as expected roughly [1318, 300, 0, 0] h3 and h4 can have small positive or negative values. Can anybody tell me what could be the problem here ?

**Tags:** observational-astronomy, galaxy, data-analysis, spectra, python

---

## 1 answers

### Answer by username_1 on 2022-05-06. Score: 3

If you read [the documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html) for the `curve_fit` function, you’ll see that it has an optional parameter (`p0`) for the initial guess for the parameter values. Since you didn’t provide anything for this, it defaults to 1 for all parameter values. Since these were then returned as the output, it means the fit failed to converge (probably because [1,1,1,1] is much too far away from the true values). Try running it with sensible initial-guess values for the parameters.

> **Comments:**
> **username_0**: I have tried arround with different guesses for the initial values, and my results differ by a lot. The problem I keep getting is that even with the initial parameter guesses relatively close the fit data still differs significantly from my real data. Also small changes in my initial guesses give me huge differences in my final fit. f.e. initial guess [1250, 200, 0, 0] gives me the fit [ 929.68956083 1277.90277059 -10.92366431 9.70117469] which is wrong. contrary the initial guess [1000, 100, 1, 1] results in the fit [-387.78 720.37 7.72 5.54] which is also totally off  
> **username_1**: I don't know if I can help at this point. There may be something pathological about your "data" (aside from it being smooth and noiseless) and the fact that you haven't defined any sigma values, though if I define "data" as `xx = np.arange(500, 2250, 1)` and `yy = losvd_param(xx, 1300, 300, 0, 0)` and then call `p = curve_fit(losvd_param, xx, yy, p0=[1250, 250, 0, 0])` (smooth "data", no sigmas), I get [1300, 300, 1e-10, 2e-10]. (And similarly for p0=[1000,100,0,0].)  

---

