📄 calldeltahedge.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * CallHedgeHistogram.java * * Created on March 20, 2002, 9:18 PM */package Examples.Hedging;import Statistics.*;import Market.*;import Options.*;import Hedging.*;import Triggers.*;import TradingStrategies.*;import IO.*;/** <p>Computes mean and standard deviation of the call hedge profit over a * variety of strikes and maturities. Compares the performance of * classical analytic deltas with analytic market deltas and analytic quotient * deltas.</p> * * <p>The hedge is rebalanced at each time step. Paths of the underlying are * simulated in the market probability. Output is written to the file * "CallDeltaHedge.txt".</p> * * <p>Console * program, no user interaction. All parameters fixed in source code.</p> * * @author Michael J. Meyer */public class CallDeltaHedge { // file writer to write in columns of fixed width w static final int w=35, // width of output columns timeSteps=20, // maximal number of time steps to horizon strikes=7; // number of strikes static final String fileName="CallDeltaHedge.txt"; // output file // initialized in constructor FixedFieldWidthFileWriter fout; // underlying static final int nSignChange=5, // irrelevant nBranch=10, // irrelevant nPaths=400000; // number of paths static final double S_0=50, mu=0.9, sigma=0.2, r=0.1, q=0.0, dt =0.05, K_low=40, // smallest strike K_high=70; // largest strike // the quotients [0]=|aM/M|, [1]=|aS/S|, [2]=|qM/M|, [3]=|qS/S| // depending on strike K, time to expiration T*dt static final double[][][] quotient=new double[4][strikes][timeSteps]; // constructor CallDeltaHedge() { try{ fout=new FixedFieldWidthFileWriter(fileName,w); } catch(java.io.IOException e) { System.out.println ("Cannot open output file. Exiting."); System.exit(0); } } // end constructor // compute the quotients of hedge error means and // hedge error standard deviations: void computeQuotients() { int items=(timeSteps-2)*strikes; System.out.print("Computing "+items+" items:\n"); for(int k=0;k<strikes;k++) for(int T=2;T<timeSteps;T+=2){ double K=K_low+(K_high-K_low)*k/(strikes-1); int item=k*(timeSteps-2)+(T-1); System.out.print(item+","); if(item%20==0)System.out.print("\n"); // the underlying, nSignChange=8 ConstantVolatilityAsset asset=new ConstantVolatilityAsset(T,dt,8,S_0,r,q,mu,sigma); // the call BlackScholesCall call=new BlackScholesCall(K,asset); // hedge with analytic deltas, transaction costs = 0 RandomVariable deltaHedge=call.discountedHedgeGain (call.ANALYTIC_DELTA,Flag.MARKET_PROBABILITY,nBranch,0,0); // hedge with analytic market deltas, transaction costs = 0 RandomVariable amDeltaHedge=call.discountedHedgeGain (call.ANALYTIC_MINIMUM_VARIANCE_DELTA, Flag.MARKET_PROBABILITY,nBranch,0,0); // hedge with analytic market deltas, transaction costs = 0 RandomVariable aqDeltaHedge=call.discountedHedgeGain (call.ANALYTIC_QUOTIENT_DELTA,Flag.MARKET_PROBABILITY,nBranch,0,0); // the discounted hedge gain double[] deltaHedgeSTATS= deltaHedge.meanAndStandardDeviation(nPaths), amDeltaHedgeSTATS= amDeltaHedge.meanAndStandardDeviation(nPaths), aqDeltaHedgeSTATS= aqDeltaHedge.meanAndStandardDeviation(nPaths); double deltaHedgeMEAN=deltaHedgeSTATS[0], deltaHedgeSTDV=deltaHedgeSTATS[1], amDeltaHedgeMEAN=amDeltaHedgeSTATS[0], amDeltaHedgeSTDV=amDeltaHedgeSTATS[1], aqDeltaHedgeMEAN=aqDeltaHedgeSTATS[0], aqDeltaHedgeSTDV=aqDeltaHedgeSTATS[1]; quotient[0][k][T]= FinMath.round(Math.abs(amDeltaHedgeMEAN/deltaHedgeMEAN),2); quotient[1][k][T]= FinMath.round(Math.abs(amDeltaHedgeSTDV/deltaHedgeSTDV),2); quotient[2][k][T]= FinMath.round(Math.abs(aqDeltaHedgeMEAN/deltaHedgeMEAN),2); quotient[3][k][T]= FinMath.round(Math.abs(aqDeltaHedgeSTDV/deltaHedgeSTDV),2); } // for k } // end computeQuotients // routine to write tables of results void writeResults() throws java.io.IOException { String message= "Tables of quotients |mM/M|, |qM/M|, |mS/S|, |qS/S|, where\n"+ "M is the hedge error mean using analytic deltas\n"+ "S is the hedge error standard deviation using analytic deltas\n"+ "aM is the hedge error mean using analytic market deltas\n"+ "aS is the hedge error standard deviation using analytic market deltas\n"+ "qM is the hedge error mean using analytic quotient deltas\n"+ "qS is the hedge error standard deviation using analytic quotient deltas\n"; fout.write(message+"\n\n"); message= "S_0="+S_0+", mu="+mu+", sigma="+sigma+", r="+r+", dt="+dt+"\n"+ "nPaths="+nPaths; fout.write(message+"\n\n\n"); for(int j=0;j<4;j++)writeTable(j); fout.close(); System.out.println ("\n\nFinished, results in file CallDeltaHedge.txt"); } // end writeResults // write table of quotients[j], j<4. void writeTable(int j) throws java.io.IOException { String message=" "; switch(j) { case 0: message="\n\n\nQuotient |aM/M|:+\n\n"; break; case 1: message="\n\n\nQuotient |aS/S|:+\n\n"; break; case 2: message="\n\n\nQuotient |qM/M|:+\n\n"; break; case 3: message="\n\n\nQuotient |qS/S|:+\n\n"; } fout.write(message); fout.writeField(" ",9); for(int T=2;T<timeSteps;T+=2){ double Tc=FinMath.round(T*dt,2); fout.writeField("T="+Tc,9); } fout.write("\n\n"); for(int k=0;k<strikes;k++) { double K=K_low+(K_high-K_low)*k/(strikes-1); fout.writeField("K="+(int)K,9); for(int T=2;T<timeSteps;T+=2) fout.writeField(""+quotient[j][k][T],9); fout.write("\n"); } } //end writeTable public static void main(String[] args) throws java.io.IOException { CallDeltaHedge CHV=new CallDeltaHedge(); CHV.computeQuotients(); CHV.writeResults(); } // end main } // end CallHedgeVariance
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -