📄 optionexchangeassets.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*//* * OptionExchangeAssets.java * * Created on May 13, 2002, 3:00 AM */package Examples.Hedging;import Market.*;import Options.*;import Hedging.*;import Triggers.*;import Statistics.*;import com.skylit.io.EasyReader;/** <p>Monte Carlo and Analytic price of the option to exchange assets * on a basic asset pair (constant instantaneous volatility and correlation * of returns). The option is then hedged with analytic deltas and a * histogram of the hedge gains computed.</p> * * @author Michael J. Meyer */public class OptionExchangeAssets { public static void main(String[] args) { EasyReader io=new EasyReader(); double r=0,dt=0,S_10=0,S_20=0,K=0, mu_1=0,mu_2=0,sigma_1=0,sigma_2=0,rho=0,q_1=0,q_2=0, fixed_trc=0, prop_trc=0; int T=0, nPath=0, nBins=0, nBranch=0; boolean done=false; while(!done){ // main loop System.out.print ("Option to receive one share of S_1 for K shares of S_2 will be\n"+ "priced and heged.\n"+ "Histogram of hegde profit and loss will be displayed.\n\n"+ "Test data (0) or user defined data (1)?\n"+ "Enter choice: "); int choice=io.readInt(); if(choice==1){ // user dialogue // read S_1 System.out.print("Asset S_1:\n"+"S_1(0)="); S_10=io.readDouble(); System.out.print("drift of returns="); mu_1=io.readDouble(); System.out.print("instantaneous volatility of returns="); sigma_1=io.readDouble(); System.out.print("Dividend yield="); q_1=io.readDouble(); // read S_2 System.out.print("Asset S_2:\n"+"S_2(0)="); S_20=io.readDouble(); System.out.print("drift of returns="); mu_2=io.readDouble(); System.out.print("instantaneous volatility of returns="); sigma_2=io.readDouble(); System.out.print("Dividend yield="); q_2=io.readDouble(); System.out.print("\ninstantaneous correlation of returns="); rho=io.readDouble(); System.out.print("strike (exchange) rate K="); K=io.readDouble(); System.out.print("riskfree rate="); r=io.readDouble(); System.out.print("\n\nSimulation:\n"); System.out.print("size of time step (years):"); dt=io.readDouble(); System.out.print("number of time steps to expiry:"); T=io.readInt(); System.out.print("number of simulated price paths:"); nPath=io.readInt(); System.out.print ("number of path branches per conditional expectation:"); nBranch=io.readInt(); System.out.print("number of histogram bins:"); nBins=io.readInt(); } else { // test data T=20; dt=0.05; S_10=21; S_20=23; mu_1=0.3; mu_2=-0.2; sigma_1=0.3; sigma_2=0.4; rho=0.6; r=0.03; q_1=0.07; q_2=1.8; K=0.7; nPath=100000; nBranch=1000; nBins=100; // transaction costs fixed_trc=0.0; // fixed transaction costs prop_trc=0.0; // proportional transaction costs } // end else double[] S_0={S_10,S_20}, mu={mu_1,mu_2}, sigma={sigma_1,sigma_2}, q={q_1,q_2}; // pricing System.out.println("\n\nPRICING:\n"); // the underlying BasicAssetPair assets=new BasicAssetPair(T,dt,S_0,r,mu,sigma,rho,q); // the option OptionToExchangeAssets option=new OptionToExchangeAssets(assets,K); // time the path simulation long before=System.currentTimeMillis(), after, time; double price_an=option.discountedAnalyticPrice(0), price_mc=option.discountedMonteCarloPrice(0,nPath), price=FinMath.round(price_an,4); after=System.currentTimeMillis(); time=after-before; System.out.println( "Analytic price: "+price_an+"\n"+ "Monte Carlo price ("+nPath+" paths): "+price_mc+"\n"+ "Time: "+time); // hedging System.out.println("\n\nHEDGING:\n"); Trigger rebalance=new TriggerAtEachTimeStep(T); VectorHedge hedge=new VectorDeltaHedge (assets,option,rebalance,Flag.A_DELTA,nBranch,fixed_trc,prop_trc); // the discounted hedge gain RandomVariable hedge_gain=hedge.discountedHedgeGain(); // show the histogram String title="Option delta hedge gains, option price: "+price; hedge_gain.displayHistogram(nPath,nBins,true,title,""); System.out.println ("\n\nAnother run? Yes(1) No(0):\n"+ "Enter choice:"); choice=io.readInt(); if(choice==0)done=true; } // end main loop } // end main } // end OptionExchangeAssets
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -