<jupyter_start><jupyter_text>特定の比率でAに賭けた場合のリターン  最適化<jupyter_code>def bet_a(x):
    return ((1 + 2 * x) ** 0.5 * (1 - x) ** 0.5) * -1
bet_a_bounds = np.array([[0, 1]])
bet_a0 = 0.1
minimize(bet_a, x0=bet_a0, method="SLSQP", bounds=bet_a_bounds)<jupyter_output><empty_output><jupyter_text>可視化<jupyter_code>x = np.linspace(0, 1, 100)

p = figure(plot_width=400, plot_height=300)
p.line(x, [-bet_a(f) for f in x])
p.yaxis.axis_label = 'リターン'
p.xaxis.axis_label = 'Aに賭ける比率'
output_notebook()
show(p)<jupyter_output><empty_output><jupyter_text>A、B、Cを組み合わせた場合の最適化 Aに賭ける金額、Bに賭ける金額、Cに賭ける金額、手元に残す資金をa, b, c, dとすると  下記の最大値をとるようにする\begin{equation}  (3a+d)^{\frac{1}{2}}(2b+d)^{\frac{1}{3}}(6c+d)^{\frac{1}{6}}\end{equation}<jupyter_code>def bet_all(x):
    a, b, c, d = x
    m = \
        (3 * a + d) ** (1 / 2) * \
        (2 * b + d) ** (1 / 3) * \
        (6 * c + d) ** (1 / 6)
    return -m
cons = (
    {'type': 'eq', 'fun': lambda x: 1 - sum(x)}
)
bet_all0 = np.array([0.5, 0.35, 0.15, 0])
bet_all_bounds = np.array([[0, 1], [0, 1], [0, 1], [0, 1]])
minimize(bet_all, x0=np.array([0.4, 0.4, 0.2, 0]),
         constraints=cons, method="SLSQP", bounds=bet_all_bounds)<jupyter_output><empty_output><jupyter_text>下記の比率と大体同じになる  * a: 1 / 2 * b: 1 / 3 * c: 1 / 6 * d: 0<jupyter_code>'a:{0:.2f}, b:{1:.2f}, c:{2:.2f}, d:{3:.2f}'.format(1 / 2, 1 / 3, 1 / 6, 0)
minimize(bet_all, x0=np.array([0.3, 0, 0.1, 0.6]),
         constraints=cons, method="SLSQP", bounds=bet_all_bounds)<jupyter_output><empty_output><jupyter_text>下記の比率と大体同じになる  * a: 5 / 18 * b: 0 * c: 1 / 18 * d: 2 / 3<jupyter_code>'a:{0:.2f}, b:{1:.2f}, c:{2:.2f}, d:{3:.2f}'.format(5 / 18, 0, 1 / 18, 2 / 3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2334123222748815, 'fraction_numerical': 0.07523696682464455, 'mean_word_length': 4.0568862275449105, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '362549', 'license': 'MIT', 'n_tokens_mistral': 983, 'n_tokens_neox': 903, 'n_words': 147, 'path': 'optimize.ipynb', 'repo_name': 'drillan/finpy-study01', 'size': '1688'}