# -*- coding: utf-8 -*- """ Created on Wed Nov 4 13:58:00 2020 @author: Gabriel Turinici """ import numpy as np import matplotlib.pyplot as plt import seaborn as sns from scipy.stats import normaltest T=1.0 #N=52 N=255 #xN=12 M=3000 # nombre de scenarios dt = T/N W0=0 timerange = np.linspace(0,T,N+1,endpoint=True) timerange=timerange[:,None] dW= np.sqrt(dt)*np.random.randn(N,M) W=np.zeros((N+1,M)) W[0,:]=W0 W[1:,:]=W0+np.cumsum(dW,0) S0=100. mu=0.1 sigma=0.15 taux_r=0.03 def st(mu,sigma,t,wt): return np.exp((mu-sigma**2/2.)*t + sigma*wt) St = st(mu,sigma,timerange,W)*S0 plt.figure(1) for ii in range(15): plt.plot(timerange,St[:,ii]) V0=1000 m=0.5 Xt=np.zeros_like(St) Mt=np.zeros_like(St) Vt=np.zeros_like(St) #mise en place de la strategie Vt[0,:]=V0 Xt[0,:]=m*Vt[0,:] Mt[0,:]=Vt[0,:]-Xt[0,:] for jj in range(N): Mt[jj+1,:] =Mt[jj,:]*np.exp(taux_r*dt)#actualisation de M Xt[jj+1,:] =Xt[jj,:]*St[jj+1,:]/St[jj,:]#mise a jour partie risquee avec St Vt[jj+1,:] =Xt[jj+1,:]+Mt[jj+1,:] # calcul de valeur du portfeuille # re-allocation selon les prescriptions de la regle CPPI: Xt[jj+1,:]=m*Vt[jj+1,:] Mt[jj+1,:]=Vt[jj+1,:]-Xt[jj+1,:] #livraison cash = Xt[-1,:]+Mt[-1,:] # calcul de la valeur finale plt.figure(10,figsize=(9, 3)) plt.subplot(1,2,1) plt.hist(cash*100/V0) plt.title('Vt/V0') plt.subplot(1,2,2) #plt.figure(8) rendements_vt=np.log((Vt[1:,:]/Vt[0:-1,:]).flatten()) sns.distplot(rendements_vt,hist=True,kde=True,bins=100) plt.title('hist du rdt de Vt') #test de normalite: _,pvalue=normaltest(rendements_vt) print('test normalite=: p value=', pvalue) if pvalue < 0.05: # null hypothesis: x comes from a normal distribution print("Le donnees ne semblent pas compatibles avec l'hypothese de normalite") else: print("les donnees sembles etre en accord avec l'hypothese de normalite.")