<jupyter_start><jupyter_text>Performance evaluation of the gradient descent algorithm<jupyter_code>%load_ext autoreload
%autoreload 2
from pycewise import *
import random
import matplotlib.pyplot as plt
import numpy
from plotnine import *
import plotnine
plotnine.options.figure_size = (12, 8)

print(__version__)
print(__git_version__)
min_x = 1
max_x = 1e9
nb_breakpoints = 6
size = 200
numpy.random.seed(27)
#breakpoints = list(sorted(numpy.exp(numpy.random.uniform(numpy.log(min_x), numpy.log(max_x), nb_breakpoints))))
breakpoints = [10**n for n in range(1, 9)]
print(f'Breakpoints: {breakpoints}')
x_list = list(sorted(numpy.exp(numpy.random.uniform(numpy.log(min_x), numpy.log(max_x), size))))
interval_idx = 0
y_list = []
tmp_break = breakpoints + [numpy.inf]
for i, x in enumerate(x_list):
    if x > tmp_break[interval_idx]:
        interval_idx += 1
    assert x <= tmp_break[interval_idx]
    slope = interval_idx*1e-10
    intercept = (interval_idx+1)*10*1e-10
    y_list.append(x*slope + intercept)
dataset = list(zip(x_list, y_list))
sigma = 0.1
noisy_dataset = [(x, y+numpy.random.normal(scale=sigma)*x*1e-10) for x, y in dataset]
x = [t[0] for t in noisy_dataset] 
x = numpy.array(x)
y = [t[1] for t in noisy_dataset] 
y = numpy.array(y)
%%time
leaf = Leaf(x, y, Config(mode='log', epsilon=1e-12))
%%time
df = leaf._compute_log_parameters(start_coeff=abs(leaf._compute_classical_coeff()),
                                  start_intercept=abs(leaf._compute_classical_intercept()),
                                  eps=1e-15,
                                  max_iter=1000, return_search=True,
)
df
df['prev_coeff'] = df['coefficient'].shift(1)
df['prev_inter'] = df['intercept'].shift(1)
df
(ggplot(df)
    + aes(x='coefficient', y='intercept')
    + geom_point(aes(color='error'))
    + geom_segment(aes(xend='prev_coeff', yend='prev_inter'), alpha=0.5)
    + geom_point(data=df.tail(n=1), color='red', size=3)
    + theme_bw()
    + ggtitle('Complete gradient descent')
)
(ggplot(df)
    + aes(x='index', y='error')
    + geom_point()
    + geom_line(alpha=0.5)
    + theme_bw()
    + ggtitle('Evolution of the error (first 10 iterations skipped)')
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1481144934120854, 'fraction_numerical': 0.024988641526578828, 'mean_word_length': 4.293269230769231, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '611388', 'license': 'MIT', 'n_tokens_mistral': 862, 'n_tokens_neox': 811, 'n_words': 159, 'path': 'notebooks/gradient_perf-eval.ipynb', 'repo_name': 'Ezibenroc/pycewise', 'size': '2201'}