<jupyter_start><jupyter_text>Extrema<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>extrema() is a classification function used to evaluate a trio of points.The function will evaluate the middle point of trio to determine if itrepresents a relative maxima or minima for the trio. The result will bea boolean value True or False which will be used later.  Note that if themiddle point is not an extrema, the value False will be returned.<jupyter_code>def extrema(a, b, c):
    x = max(a, b, c)
    z = min(a, b, c)
    epsilon = 0.0000001  # This is a safeguard against minor differences.
    result = False
    if abs(b - x) < epsilon:
        result = True
    if abs(b - z) < epsilon:
        result = True
    return result<jupyter_output><empty_output><jupyter_text>This is a user supplied function.  Example is Lial Figure 8 Section 13.1.<jupyter_code>def f(x):
    y = (x ** 8) ** .333 - 16.0 * (x ** 2) ** .33
    return y<jupyter_output><empty_output><jupyter_text>The following extrema evaluation will be over a defined interval. Grid pointswill be defined and the function extreme() will compare trios of values.Define interval endpoints for a closed interval [xa,xb].<jupyter_code>xa = -1.0
xb = +9.0<jupyter_output><empty_output><jupyter_text>n = number of grid points.  The interval [xa,xb] will be subdivided.Adding delta to xb insures xb is included in the array generated.  For thispurpose, np.arange() will be used to create a numpy array of floating pointvalues to be used in subsequent calculations.<jupyter_code>n = 1000
delta = (xb - xa) / n
x = np.arange(xa, xb + delta, delta)
y = f(x)

value = [False]  # This defines the list value which will contain Boolean values.
value = value * len(x)  # This expands the list to the length of x.<jupyter_output><empty_output><jupyter_text>We are going to check each trio of points during the grid search.If a local extrema is found, the boolean value will be set to True.Otherwise it will remain False. The interval endpoints are always localextrema so we define their boolean values first.<jupyter_code>L = len(x)
value[0] = True  # This will correspond to one endpoint.
value[L - 1] = True  # This corresponds to the other.<jupyter_output><empty_output><jupyter_text>The for loop will check each consecutive trios of f values with the functionextrema() to identify local extrema.  Only when an extrema is found will theboolean value in the list value be changed to True.<jupyter_code>for x_index in range(L - 2):
    first_x = x[x_index]
    second_x = x[x_index + 1]
    third_x = x[x_index + 2]
    a = f(first_x)
    b = f(second_x)
    c = f(third_x)
    is_second_x_extrema = extrema(a, b, c)
    value[x_index + 1] = is_second_x_extrema

for k in range(L - 2):
    value[k + 1] = extrema(f(x[k]), f(x[k + 1]), f(x[k + 2]))

max_value = max(y)  # We check the list to find the global maxima.
min_value = min(y)  # We check the list to find the global minima.<jupyter_output><empty_output><jupyter_text>The following for loop checks the boolean value for each point. If the valueis True, that point will be plotted yellow.  The global maximum is plotted asred and the minimum is plotted as green. We follow this up by plotting thevalues of x and y.<jupyter_code>error = 0.0000001  # The error parameter guards against roundoff error.
# The code which follows assigns colors to maxima and minima and plots them.

plt.figure()
for k in range(L):
    if value[k] is True:
        plt.scatter(x[k], y[k], s=60, c='y')
        if abs(max_value - y[k]) < error:
            plt.scatter(x[k], y[k], s=60, c='r')
        if abs(min_value - y[k]) < error:
            plt.scatter(x[k], y[k], s=60, c='b')

plt.plot(x, y, c='k')  # This plots the line on the chart.
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Plot Showing Absolute and Relative Extrema')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09709480122324159, 'fraction_numerical': 0.013761467889908258, 'mean_word_length': 3.9371069182389937, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '51635', 'license': 'MIT', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1291, 'n_words': 501, 'path': 'Fall2019/MSDS400/pythonPractice/Module7/Extrema.ipynb', 'repo_name': 'kbridge14/regression_things', 'size': '3924'}