Compute the slope of the straight portion of the graph I have the following four graphs for which I have to compute the slope of the straight portion of the graph (it returns the value of Young's Modulus of a material). I initially received a 4 datasets with a lot of noise but I reduced the noises by fitting an equivalent 13-degree polynomial curve using np.poylfit.
Next step is to calculate the slope of the straight portion of the curve (highlighted with red rectangular boxes). I tried using $\chi$ squared test to fit a linear line but it is very slow and to make it fast I have to specify the program to calculate only until 1000 points (approximate value by which the linear portion ends).
Can anyone suggest me a more generic and faster way to find the slopes of the highlighted segments?




 A: Based on your comments, it sounds like your problem is how to determine the initial segment of your data that we can "reasonably" fit a straight line to. I would suggest a simple local search strategy.
Start with a proportion $p_0=1$. For each iteration $i$, fit a straight line to the initial $p_i$ proportion of your data. Assess which proportion $q_i$ of your observations lies above the fitted line. If $q_i$ is "too large", set $p_{i+1}$ smaller than $p_i$. If $q_i$ is "too small", set $p_{i+1}$ larger than $p_i$. Iterate.
The regression lines should be very fast to fit. You might want to suppress the intercept, i.e., force the line to go through the origin. In that case, you only have to estimate the slope, and this is easily done by
$$ \beta = \frac{\sum_{j=1}^{p_iN} x_jy_j}{\sum_{j=1}^{p_iN} x_j^2}, $$
assuming that your $N$ data points are listed in increasing order of $x_j$. (You could even use an "online" method for updating the slope estimate, but that may be more complicated than the saving in runtime would warrant.)
For updating $p_i$, you could use some variant of bisection search.
Finally, to determine whether $q_i$ is "good", you should experiment a little. Of course, we are aiming for $q=\frac{1}{2}$ if your initial data are symmetrically distributed around the straight line. However, they may not be - it's hard to figure this out based on the full plots you are giving. So in the case of asymmetric distributions in this initial segment, you may want to aim for some other value of $q$. You will need to experiment a little.
