def calc_max_curv(magnitudes, plotvar=False):
counts = Counter(magnitudes)<EOL>df = np.zeros(len(counts))<EOL>mag_steps = np.zeros(len(counts))<EOL>grad = np.zeros(len(counts) - <NUM_LIT:1>)<EOL>grad_points = grad.copy()<EOL>for i, magnitude in enumerate(sorted(counts.keys(), reverse=True)):<EOL><INDENT>mag_steps[i] = magnitude<EOL>if i > <NUM_LIT:0>:<EOL><INDENT>df[i] = counts[magnitude] + df[i - <NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>df[i] = counts[magnitude]<EOL><DEDENT><DEDENT>for i, val in enumerate(df):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>grad[i - <NUM_LIT:1>] = (val - df[i - <NUM_LIT:1>]) / (mag_steps[i] - mag_steps[i - <NUM_LIT:1>])<EOL>grad_points[i - <NUM_LIT:1>] = mag_steps[i] - ((mag_steps[i] -<EOL>mag_steps[i - <NUM_LIT:1>]) / <NUM_LIT>)<EOL><DEDENT><DEDENT>curvature = np.zeros(len(grad) - <NUM_LIT:1>)<EOL>curvature_points = curvature.copy()<EOL>for i, _grad in enumerate(grad):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>curvature[i - <NUM_LIT:1>] = (_grad - grad[i - <NUM_LIT:1>]) / (grad_points[i] -<EOL>grad_points[i - <NUM_LIT:1>])<EOL>curvature_points[i - <NUM_LIT:1>] = grad_points[i] - ((grad_points[i] -<EOL>grad_points[i - <NUM_LIT:1>]) /<EOL><NUM_LIT>)<EOL><DEDENT><DEDENT>if plotvar:<EOL><INDENT>plt.scatter(mag_steps, df, c='<STR_LIT:k>', label='<STR_LIT>')<EOL>plt.plot(mag_steps, df, c='<STR_LIT:k>')<EOL>plt.scatter(grad_points, grad, c='<STR_LIT:r>', label='<STR_LIT>')<EOL>plt.plot(grad_points, grad, c='<STR_LIT:r>')<EOL>plt.scatter(curvature_points, curvature, c='<STR_LIT:g>', label='<STR_LIT>')<EOL>plt.plot(curvature_points, curvature, c='<STR_LIT:g>')<EOL>plt.legend()<EOL>plt.show()<EOL><DEDENT>return curvature_points[np.argmax(abs(curvature))]<EOL>
Calculate the magnitude of completeness using the maximum curvature method.

:type magnitudes: list
:param magnitudes:
    List of magnitudes from which to compute the maximum curvature which
    will give an estimate of the magnitude of completeness given the
    assumption of a power-law scaling.
:type plotvar: bool
:param plotvar: Turn plotting on and off

:rtype: float
:return: Magnitude at maximum curvature

.. Note:: Should be used as a guide, often under-estimates Mc.

.. rubric:: Example

>>> import numpy as np
>>> mags = []
>>> for mag in np.arange(2.5,3, 0.1):
...     mags.extend([mag] * int(20000 - 10 * mag))
>>> for mag in np.arange(3,7, 0.1):
...     mags.extend([mag] * int(10 ** (7 - 1 * mag)))
>>> calc_max_curv(mags, plotvar=False)
3.0