⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 callhedgevariance.java

📁 金融资产定价,随机过程,MONTE CARLO 模拟 JAVA 程序和文档资料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                for(int k=0;k<T;k++)sum+=H(k)*Q(k);        return sum;    }            /** The old formula for the analytic approximation to the variance of      *  the call delta hedge.     *  The hedge is rebalanced at each time step. See     *  <code>Variance.tex</code>     */       public double oldCallDeltaHedgeAnalyticVariance()    {        double sum=0;                for(int k=0;k<T;k++)        sum+=Math.exp(-L*L/(sg2dt*(T+k)))/Math.sqrt(T*T-k*k);                sum*=F;        return sum;    }                /** The analytic approximation to the variance of the call delta hedge.     *  The hedge is rebalanced at each time step. See     *  <code>Variance.tex</code>     */       public double callDeltaHedgeMonteCarloVariance(int nPaths)    {        double[] mstdv;        mstdv=callHedge.hedgeMeanAndStandardDeviation(nPaths);                return mstdv[1]*mstdv[1];    }        /******************************************************************************* * *                          Some Unit Tests * ******************************************************************************/    public static void testFactors()    {        double S_0=30,               sigma=0.3,                r=0,                dt=0.05,               onehalfsg2dt2=sigma*sigma*sigma*sigma*dt*dt/2,               K=45;        int T=30;                CallHedgeVariance chv=new CallHedgeVariance(S_0,sigma,r,T,dt,K);        String report=        "f="+chv.f+        "\nonehalfsg2dt2="+onehalfsg2dt2;                System.out.println(report);    }                   /** Tests the analytic formula for the mean {@link #meanF1},     *  @link #meanF2}, @link #meanF3} against a Monte Carlo mean.    */    public static void testFmeans()   {             int nPaths=100000;       double S_0=30,              sigma=0.3,               r=0,               dt=0.05;              for(int K=25;K<50;K+=10)       for(int T=10;T<31;T+=10){                      CallHedgeVariance chv=new CallHedgeVariance(S_0,sigma,r,T,dt,K);           double aF1mean=chv.meanF1(T-1),                  aF2mean=chv.meanF2(T-1),                  aF3mean=chv.meanF3(T-1),                  F1mean=chv.F1(T-1).expectation(nPaths),                  F2mean=chv.F2(T-1).expectation(nPaths),                  F3mean=chv.F3(T-1).expectation(nPaths);                      String report=           "\nK="+K+", T="+T+           "\nF1mean, analytic: "+aF1mean+           "\nF1mean: Monte Carlo: "+F1mean+           "\nF2mean, analytic: "+aF2mean+           "\nF2mean: Monte Carlo: "+F2mean+           "\nF3mean, analytic: "+aF3mean+           "\nF3mean: Monte Carlo: "+F3mean;            System.out.println(report);                  } // end for K   } // end testFmeans       /******************************************************************************* * *     Static methods to compare the Monte Carlo hedge weights and hedge *     variances with their analytic approximations.                   * ******************************************************************************/      /** Tests the analytic formulas for quotient and minimum variance deltas    *  against the respective Monte Carlo quantities.    */       public static void weightTest()   throws java.io.IOException   {             // file writer to write in columns of fixed width w       final int w=35;                     // width of output columns       String fileName="CallHedgeWeights.txt";          // output file       FixedFieldWidthFileWriter        fout=new FixedFieldWidthFileWriter(fileName,w);             int nSignChange=5, nBranch=10, nPaths=1600000;              int T=40;  // time steps to horizon       double S_0=50, mu=0.3, sigma=0.2, r=0.1, q=0.0, dt =0.05;       String        str="S_0="+S_0+", mu="+mu+", sigma="+sigma+", r="+r+", dt="+dt+",\n"+           "tau denotes time to expiration.";       fout.write(str+"\n\n\n");              for(int t=30;t<31;t+=3){                                    double tau=FinMath.round((T-t)*dt,3);           // time to expiration           fout.write("\n\n\nTime to expiration="+tau+"\n\n");                      ConstantVolatilityAsset            asset=new ConstantVolatilityAsset(T-t,dt,nSignChange,S_0,r,0.0,mu,sigma);                  // the different hedge weights for the call            double delta,            // analytic delta                  mvdelta,          // minimum variance delta                  amvdelta,         // analytic minimum variance delta                  qdelta,           // quotient delta                  aqdelta;          // analytic quotient delta                      fout.writeField("Strike",8);           fout.writeField("delta",10);           fout.writeField("qdelta",11);           fout.writeField("aqdelta",12);           fout.writeField("mvdelta",12);           fout.writeField("amvdelta",13);           fout.write("\n");           for(double K=40;K<81;K+=10)           {              BlackScholesCall call=new BlackScholesCall(K,asset);                   delta=call.analyticDelta(0);              mvdelta=call.minimumVarianceDelta(Flag.MARKET_PROBABILITY,0,nPaths);              qdelta=call.quotientDelta(0,nPaths);              amvdelta=call.analyticMinimumVarianceDelta(Flag.MARKET_PROBABILITY,0);              aqdelta=call.analyticQuotientDelta(0);                             delta=FinMath.round(delta,4);              mvdelta=FinMath.round(mvdelta,4);              qdelta=FinMath.round(qdelta,4);              amvdelta=FinMath.round(amvdelta,4);              aqdelta=FinMath.round(aqdelta,4);                            fout.writeField(""+K,8);              fout.writeField(""+delta,10);              fout.writeField(""+qdelta,11);              fout.writeField(""+aqdelta,12);              fout.writeField(""+mvdelta,12);              fout.writeField(""+amvdelta,13);                       fout.write("\n");                         } // for t             } // for K             fout.close();      System.out.println("Finished, results in file HedgeWeights.txt");      } // end main               /** Tests the the old and new analytic formulas for the call hedge variance    *  against the Monte Carlo sample variance.    */       public static void varianceTest()   throws java.io.IOException   {             // file writer to write in columns of fixed width w       final int w=35;                     // width of output columns       String fileName="CallHedgeVariance.txt";          // output file       FixedFieldWidthFileWriter        fout=new FixedFieldWidthFileWriter(fileName,w);             int nSignChange=5, nBranch=10, nPaths=100000;              double S_0=50, mu=0.3, sigma=0.4, r=0.05, q=0.0, dt =0.01;       String str="S_0="+S_0+", mu="+mu+", sigma="+sigma+", r="+r;       fout.write(str+"\n\n\n");              for(int T=10;T<101;T+=40)       for(double K=40;K<81;K+=10){                      System.out.println("N="+T+", K="+K);                  CallHedgeVariance chv=new CallHedgeVariance(S_0,sigma,r,T,dt,K);           // variance of the call delta hedge           double a_var,            // analytic                  oa_var,           // old analytic formula                  var,              // Monte Carlo                  error,            // percent error of the analytic formula                  oerror;           // percent error of the old analytic formula                        double Tc=T*dt;                   a_var=chv.callDeltaHedgeAnalyticVariance();           oa_var=chv.oldCallDeltaHedgeAnalyticVariance();           var=chv.callDeltaHedgeMonteCarloVariance(nPaths);                                // if var, a_var are very small the percent difference can be           // very large, treat separately           if(Math.abs(var-a_var)<0.000001)error=0;           else error=100*(var-a_var)/var;           if(Math.abs(var-oa_var)<0.000001)oerror=0;           else oerror=100*(var-oa_var)/var;           error=FinMath.round(error,2);           oerror=FinMath.round(oerror,2);                      fout.writeField("N="+T+",",12);           fout.writeField("T="+Tc+",",12);           fout.writeField("K="+K+",",12);           fout.writeField("% error = "+error,25);           fout.writeField("% oerror = "+oerror,25);           fout.write("\n");           if(K==80)fout.write("\n\n");             } // for T             fout.close();      System.out.println("Finished, results in file CallHedgeVariance.txt");      } // end main      /******************************************************************************* * *                    Test program * ******************************************************************************/    public static void main(String[] args)    throws java.io.IOException    {        weightTest();        //varianceTest();        //testFmeans();        //testFactors();    }} // end CallHedgeVariance

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -