import sys; sys.path[:0] = [".", ".."]
import numpy as np
from figstyle import *
def t(s): y,m=s.split('-'); return int(y)+(int(m)-0.5)/12
ser = {
 'A100':[('2021-06',3.05),('2022-06',2.20),('2023-04',2.40),('2023-12',1.90),('2024-08',1.60),('2025-06',1.50),('2026-03',1.42),('2026-08',1.38),('2026-09',1.35),('2026-10',1.35),('2026-11',1.35)],
 'H100':[('2023-04',8.00),('2023-10',5.90),('2024-04',4.20),('2024-10',3.20),('2025-04',2.85),('2025-10',2.75),('2026-03',2.65),('2026-08',2.55),('2026-09',2.49),('2026-10',2.49),('2026-11',2.69)],
 'H200':[('2024-12',6.50),('2025-06',4.80),('2025-12',3.90),('2026-08',3.30),('2026-09',3.44),('2026-10',3.44),('2026-11',3.44)],
}
start=dict(A100='2023-04',H100='2024-10',H200='2024-12')
col=dict(A100=BLUE,H100=RED,H200=PURPLE)
fig,ax=plt.subplots(figsize=(6.0,3.4)); H={}
for c,pts in ser.items():
    tt=np.array([t(s) for s,_ in pts]); pp=np.array([p for _,p in pts])
    H[c,'p']=ax.plot(tt,pp,'o',color=col[c],ms=4)[0]
    m=tt>=t(start[c]); sl,ic=np.polyfit(tt[m],np.log(pp[m]),1)
    xx=np.linspace(tt[m].min(),tt.max(),50)
    H[c,'f']=ax.plot(xx,np.exp(ic+sl*xx),'-',color=col[c],lw=1.2)[0]; H[c,'r']=1-np.exp(sl)
sd={'A100':1.56,'H100':2.89,'B200':6.63}
hsd=ax.plot([2026.82]*3,list(sd.values()),'^',color=NAVY,ms=5)[0]
ax.annotate('B200',(2026.82,6.63),xytext=(-7,0),textcoords='offset points',va='center',ha='right',fontsize=7.5)
ax.annotate('Silicon Data index,\n26 Oct 2026',(2026.80,1.58),xytext=(2026.25,1.95),va='center',ha='right',fontsize=7,color='0.3',arrowprops=dict(arrowstyle='-',color='0.5',lw=0.5,shrinkA=2,shrinkB=4))
ax.set_yscale('log'); ax.set_yticks([1,1.5,2,3,4,6,8]); ax.set_yticklabels(['1','1.5','2','3','4','6','8'])
ax.set_ylabel('USD per GPU-hour (log scale)'); ax.set_xlabel('Year'); ax.minorticks_off(); ax.set_yticks([1,1.5,2,3,4,6,8]); ax.set_yticklabels(['1','1.5','2','3','4','6','8'])
hs=[];ls=[]
for c in ['A100','H100','H200']:
    hs+= [H[c,'p'],H[c,'f']]; ls+=[f'{c} monthly rate',f'{c} fit, {H[c,"r"]:.0%} a year']
ax.legend(hs,ls,loc='upper center',bbox_to_anchor=(0.5,-0.19),ncol=3,fontsize=7.5,handletextpad=0.4)
save(fig,'fig_rent.pdf')


import importlib.util
spec=importlib.util.spec_from_file_location('an','analysis.py')
import io,contextlib
with contextlib.redirect_stdout(io.StringIO()):
    an=importlib.util.module_from_spec(spec); spec.loader.exec_module(an)
d=np.linspace(0,0.30,61)
fig,ax=plt.subplots(figsize=(5.6,3.2))
for c,colr in [('A100',BLUE),('H100',RED)]:
    be=[an.breakeven(c,-np.log(1-x))[0] for x in d]
    ax.plot(d*100,be,'-',color=colr,lw=1.5,label=f'{c} break-even rent')
    ax.axhline(an.P[c],color=colr,ls='--',lw=1,label=f'{c} index, 26 Oct 2026 (\\${an.P[c]:.2f})')
for x,lab in [(9.4,'H100 fit'),(12.9,'A100 fit')]:
    ax.axvline(x,color='0.75',lw=0.8,zorder=0)
    ax.text(x-0.3,0.08,lab,fontsize=7,color='0.35',rotation=90,va='bottom',ha='right')
ax.set_xlabel('Assumed annual decline in the B200 rent (percent)'); ax.set_ylabel('USD per GPU-hour'); ax.set_ylim(0,3.8); ax.set_xlim(0,30)
ax.legend(loc='upper center',bbox_to_anchor=(0.5,-0.19),ncol=2,fontsize=7.5)
save(fig,'fig_exit.pdf')
