# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Alpha Update
#
# Consider an agent with private belief of $\hat \alpha$ who takes an attestation action while the state is characterized by:
#
# $C$: commitment for payout at successful completion <br/>
# $R$: current reserve funds in the bonding curve <br/>
# $S$: total bond token supply <br/>
# $S_0$: total bond tokens locked in negative attestation <br/>
# $S_1$: total bond tokens locked in positive attestation <br/>
# $S_{free} = S-S_0-S_1$: bond tokens not committed to attestations <br/>
# $Q_0$: total claims on rewards given negative attestation <br/>
# $Q_1$: total claims on rewards given  positive attestation <br/>
# $\alpha$: the systems current belief of the liklihood of success
#
# ### Expected Payout
# The close-out 'expected' payout at the system level can be described as <br/>
#
# Aggregate for holders of the free supply $S_{free}$ 
# $$(\alpha C + R) \frac{S_{free}}{S}$$ 
#
# Aggregate for holders of the negative outcome claims $Q_0$
# $$(1-\alpha) \frac{S_0}{S} R$$
#
# Aggregate for holders of the positive outcome claims $Q_1$
# $$\alpha \frac{S_1}{S} (C+R)$$
#
# Note that the total expected payout for the whole system is given by
# $$\mathbb E(\Theta) = (\alpha C + R) \frac{S_{free}}{S}
#     +(1-\alpha) \frac{S_0}{S} R 
#     + \alpha \frac{S_1}{S} (C+R)$$ 
# Rearranging,
# $$ \mathbb E(\Theta) = (\alpha C + R) \frac{S_{free}}{S} + \alpha \frac{S_1}{S}C + \frac{S_0}{S}R + \alpha R \frac{S_1-S_0}{S}$$
#
# If $S_0=S_1$:
#
# Then the above collapses to $(\alpha C + R)$.
#
# Now let's suppose $S_0\not=S_1$ but let's find $\alpha$ such that this equality holds anyway:
#
# $(\alpha C + R) = (\alpha C + R) \frac{S_{free}}{S} + \alpha \frac{S_1}{S}C + \frac{S_0}{S}R + \alpha R \frac{S_1-S_0}{S}$
#
# Distributing the multiplications:
#
# $\alpha C  + R  = \alpha C\frac{S_{free}}{S} + R \frac{S_{free}}{S} + \alpha \frac{S_1}{S}C + \frac{S_0}{S}R + \alpha R \frac{S_1-S_0}{S}$
#
# grouping the terms with $\alpha$
#
# $\alpha C -\alpha \frac{S_{free}}{S}C-\alpha  \frac{S_1-S_0}{S}R - \alpha \frac{S_1}{S}C = \frac{S_{free}}{S}R + \frac{S_0}{S}R - R$
#
# Regrouping
#
# $\alpha \left(C -\frac{S_{free}}{S}C-\frac{S_1-S_0}{S}R - \frac{S_1}{S}C \right)= \left(\frac{S_{free}}{S} + \frac{S_0}{S} - 1\right)R$
#
# sign swapping
#
# $\alpha \left(\frac{S_{free}}{S}C+\frac{S_1-S_0}{S}R + \frac{S_1}{S}C -C\right)= \left(1-\frac{S_{free}}{S} - \frac{S_0}{S} \right)R$
#
# factoring
#
# $\alpha C \left(\frac{S_{free}}{S}+\frac{S_1-S_0}{S}\frac{R}{C} + \frac{S_1}{S} -1\right)= \left(1-\frac{S_{free}}{S} - \frac{S_0}{S} \right)R$
#
# collapsing by $S = S_0+S_1+S_{free}$
#
# $\alpha C \left(\frac{S_1-S_0}{S}\frac{R}{C}  -\frac{S_0}{S}\right)= \frac{S_1}{S}R$
#
# mulitply both sides by $S$ and distributing
#
# $\alpha \left(S_1R-S_0R  -S_0 C\right)= S_1R$
#
# A valid starting condition $(\alpha, S_1, S_0, R, C)$ must satisify the condition that $S_1 R \ge S_0(R+C)$ with 
#
# $$\alpha = \frac{S_1 R}{S_1R-S_0R  -S_0 C}$$
#
# As $S_1$ dominates $S_0$: $\alpha \rightarrow 1$ <br/>
# As $S_0$ dominates $S_1$: $\alpha \rightarrow 0$ 
#
# Spot checks in desmos imply that alpha should be:
#
# $$\alpha = \frac{S_1 R}{S_1R-S_0R  +S_0 C}$$
#
# https://www.desmos.com/calculator/ucgbgiy7ci
# ![](https://i.imgur.com/BfMSeHS.png)

StarcoderdataJupyterScriptsDedupFiltered