<jupyter_start><jupyter_text>Optimizers> Optimize Objective Functions<jupyter_code>#export
def minimize_objective(
    objective: Callable,
    param_names: List[str],
    param_ranges: List[Tuple[float, float]],
    *,
    ts: np.array,
    score_f: Callable = metrics.__mean_absolute_error,
) -> Tuple[float, float]:

    def smoothing_partial(*args: List[float]) -> float:

        forecast = smoothing.__single(
            ts=ts,
            alpha=alpha[0],
        )
        val = score_f(ts=ts, f=forecast)
        return val

    res = gp_minimize(
        smoothing_partial,
        [(0.0, 1), (0.0, 1)],
        n_calls=20,
    )
    print("x*=%.2f f(x*)=%.2f" % (res.x[0], res.fun))
    return res.x[0]
#export
def minimize_simple_smoothing(
    ts: np.array,
    score_f=metrics.__mean_absolute_error,
) -> float:

    def smoothing_partial(alpha: List[float]) -> float:
        forecast = smoothing.__single(
            ts=ts,
            alpha=alpha[0],
        )
        val = score_f(ts=ts, f=forecast)
        return val

    res = gp_minimize(
        smoothing_partial,
        [(0.0, 1)],
        n_calls=20,
    )
    print("x*=%.2f f(x*)=%.2f" % (res.x[0], res.fun))
    return res.x[0]
ts_df = simulate_data.pandas_time_series()
alpha = minimize_simple_smoothing(ts=ts_df.time_series.to_numpy(),)
_, fig = smoothing.SINGLE(
    alpha,
    df=ts_df,
    ts_col='time_series',
)
#export
def minimize_double_smoothing(
    ts: np.array,
    score_f=metrics.__mean_absolute_error,
) -> float:

    def smoothing_partial(args: List[float]) -> float:

        forecast = smoothing.__double(
            ts=ts,
            alpha=args[0],
            beta=args[1],
        )
        val = score_f(ts=ts, f=forecast)
        return val

    res = gp_minimize(
        smoothing_partial,
        [(0.0, 1), (0.0, 1)],
        n_calls=20,
    )
    print("x*=%.2f, y*=%.2f, f(x*,y*)=%.2f" % (
        res.x[0],
        res.x[1],
        res.fun,
    ))
    return res.x
ts_df = simulate_data.pandas_time_series()
res = minimize_double_smoothing(ts=ts_df.time_series.to_numpy(),)
res
_, fig = smoothing.DOUBLE(
    res,
    df=ts_df,
    ts_col='time_series',
)
fig.show()
datafile = '../data/superstore_sales.csv'
url = "https://raw.githubusercontent.com/" + \
      "BigDataGal/Python-for-Data-Science/master/Superstore-Sales.csv"

if not path.isfile(datafile):
    with open(datafile, 'w') as file:
        r = requests.get(url)
        file.write(r.text)

df = pd.read_csv(
    datafile,
    index_col='Order Date',
    dtype={
        'Row ID': str,
        'Order ID': str,
    },
    parse_dates=['Order Date', 'Ship Date'],
)

df.columns = ['_'.join(x.lower().split(' ')) for x in df.columns]
dff = df.head(int(0.4 * len(df)))

minimize_simple_smoothing(dff.sales.to_numpy())
_, fig = smoothing.SINGLE(1., df=dff.copy(), ts_col='sales')
fig.show()
del sma(**args):
    
    ar<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15892002734107996, 'fraction_numerical': 0.014012303485987697, 'mean_word_length': 2.6091245376078915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653734', 'license': 'Apache-2.0', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1090, 'n_words': 186, 'path': 'nbs/04_Optimizers.ipynb', 'repo_name': 'kikejimenez/time_series_model_basics', 'size': '2926'}