vmarket: /* $Id: vmarket.java,v 1.13 2004/02/06 23:43:31 pde Exp $ */
vmarket: 
vmarket: import java.util.*;
vmarket: import java.text.*;
vmarket: import java.awt.*;
vmarket: import java.lang.Math;
vmarket: import java.applet.Applet;
vmarket: 
vmarket: 
vmarket: 
vmarket: /*****************************************************************************
vmarket: *  VMarket -- a virtual market calculator for financial products in java
vmarket: *  
vmarket: *  Andre JAUN (jaun@.kth.se).  Copyright 2002-2004. All Rights Reserved.
vmarket: *  @version 4.1 (CVS/RCS $Revision: 1.13 $)
vmarket: *  Educational software distributed by www.lifelong-learners.com.
vmarket: *
vmarket: *  The authors makes no representations or warranties about the suitability 
vmarket: *  of the software, either express or implied, including but not limited 
vmarket: *  to the implied warranties of merchantability, fitness for a particular 
vmarket: *  purpose, or non-infringement. 
vmarket: *  The authors shall not be liable for any damages suffered by licensee as 
vmarket: *  a result of using or modifying this software or its derivatives.
vmarket: ******************************************************************************/
vmarket: public class vmarket extends Applet implements RunDataNotable, Runnable {
vmarket:                                                          
vmarket:   /** Stock option */
vmarket:   final static String STKOPTION = "StckOption";
vmarket:   /** Bond */
vmarket:   final static String BOND =      "ZeroCpBond";
vmarket:   /** Caplet */
vmarket:   final static String BNDOPTION = "BondOption";
vmarket:   /** Swap */
vmarket:   final static String SWAP =      "IRateSwap";
vmarket:   /** Financial models (Black, Vasicek, CIR, BDT, etc) */
vmarket:   final static String FMODEL =    "CreditModel";
vmarket:   /** Random walk processes in physics */
vmarket:   final static String RNDWALK = "RandomWalk";
vmarket:    /** Exercise to be inserted */
vmarket:   final static String EXERCISE =  "Exercise";
vmarket:   /** Vector of the names of different topics @see Solution */
vmarket:   final static String[] topicNames = {STKOPTION, BOND, BNDOPTION, SWAP, 
vmarket:                                       FMODEL, RNDWALK, EXERCISE};
vmarket:   /** GUI list of all the topics */
vmarket:   private MyChoice topicSelection;
vmarket:                                                         
vmarket:   /** Solve with finite differances */
vmarket:   final static String FD =  "FinDifferen";
vmarket:   /** Solve with finite elements method */
vmarket:   final static String FEM = "FinElements";
vmarket:   /** Solve expected value with Monte-Carlo sampling methods */
vmarket:   final static String SMP = "Monte-Carlo";
vmarket:   /** Solve expected value with Monte-Carlo sampling methods plot dots */
vmarket:   final static String SMPS= "Monte-Carlo*";
vmarket:   /** Distribution function from a random walk of particles */
vmarket:   final static String PPP = "DistribFct";
vmarket:   /** Distribution function from a random walk of particles plot dots */
vmarket:   final static String PPPS= "DistribFct*";
vmarket:   /** Method selector */
vmarket:   static String[] methodNames  = {FD, FEM, SMP, SMPS, PPP, PPPS};
vmarket:   /** GUI list of all the methods */
vmarket:   private MyChoice methodSelection;
vmarket:                                                         
vmarket:   /** Solve with Standard scheme */
vmarket:   final static String DEFAULT = "Standard";
vmarket:   /** European option without normalizing */
vmarket:   final static String EUSTY = "European";
vmarket:   /** European option in normalized variables */
vmarket:   final static String EUNORM = "European logn";
vmarket:   /** American option without normalizing */
vmarket:   final static String AMSTY = "American";
vmarket:   /** American option in normalized variables */
vmarket:   final static String AMNORM = "American logn";
vmarket:   /** Monte-Carlo for in-barrier option */
vmarket:   final static String INBAR = "inBarrier";
vmarket:   /** Monte-Carlo for out-barrier option */
vmarket:   final static String OUTBAR = "outBarrier";
vmarket:   /** Monte-Carlo particles*/
vmarket:   final static String MCPART = "particles";
vmarket:   /** Solve by sampling with a tree */
vmarket:   final static String TREE2 = "binomTree";
vmarket:   /** Vasicek model */
vmarket:   final static String VASICEK = "Vasicek model";
vmarket:   /** Cox Inggersoll and Ross model */
vmarket:   final static String CIR = "CIR model";
vmarket:   /** Black Derman and Toy model */
vmarket:   final static String BDT = "BDT model";
vmarket:   /** Hull and White model */
vmarket:   final static String HW = "Hull White";
vmarket:   /** Solution of exercise 1.01 */ final static String EXC1_01="Exercise 1.01";
vmarket:   /** Solution of exercise 1.02 */ final static String EXC1_02="Exercise 1.02";
vmarket:   /** Solution of exercise 1.03 */ final static String EXC1_03="Exercise 1.03";
vmarket:   /** Solution of exercise 1.04 */ final static String EXC1_04="Exercise 1.04";
vmarket:   /** Solution of exercise 1.05 */ final static String EXC1_05="Exercise 1.05";
vmarket:   /** Solution of exercise 1.06 */ final static String EXC1_06="Exercise 1.06";
vmarket:   /** Solution of exercise 1.07 */ final static String EXC1_07="Exercise 1.07";
vmarket:   /** Solution of exercise 1.08 */ final static String EXC1_08="Exercise 1.08";
vmarket:   /** Solution of exercise 1.09 */ final static String EXC1_09="Exercise 1.09";
vmarket:   /** Solution of exercise 1.10 */ final static String EXC1_10="Exercise 1.10";
vmarket:   /** Solution of exercise 2.01 */ final static String EXC2_01="Exercise 2.01";
vmarket:   /** Solution of exercise 2.02 */ final static String EXC2_02="Exercise 2.02";
vmarket:   /** Solution of exercise 2.03 */ final static String EXC2_03="Exercise 2.03";
vmarket:   /** Solution of exercise 2.04 */ final static String EXC2_04="Exercise 2.04";
vmarket:   /** Solution of exercise 2.05 */ final static String EXC2_05="Exercise 2.05";
vmarket:   /** Solution of exercise 2.06 */ final static String EXC2_06="Exercise 2.06";
vmarket:   /** Solution of exercise 2.07 */ final static String EXC2_07="Exercise 2.07";
vmarket:   /** Solution of exercise 2.08 */ final static String EXC2_08="Exercise 2.08";
vmarket:   /** Solution of exercise 2.09 */ final static String EXC2_09="Exercise 2.09";
vmarket:   /** Solution of exercise 2.10 */ final static String EXC2_10="Exercise 2.10";
vmarket:   /** Solution of exercise 3.01 */ final static String EXC3_01="Exercise 3.01";
vmarket:   /** Solution of exercise 3.02 */ final static String EXC3_02="Exercise 3.02";
vmarket:   /** Solution of exercise 3.03 */ final static String EXC3_03="Exercise 3.03";
vmarket:   /** Solution of exercise 3.04 */ final static String EXC3_04="Exercise 3.04";
vmarket:   /** Solution of exercise 3.05 */ final static String EXC3_05="Exercise 3.05";
vmarket:   /** Solution of exercise 4.01 */ final static String EXC4_01="Exercise 4.01";
vmarket:   /** Solution of exercise 4.02 */ final static String EXC4_02="Exercise 4.02";
vmarket:   /** Solution of exercise 4.03 */ final static String EXC4_03="Exercise 4.03";
vmarket:   /** Solution of exercise 4.04 */ final static String EXC4_04="Exercise 4.04";
vmarket:   /** Solution of exercise 4.05 */ final static String EXC4_05="Exercise 4.05";
vmarket:   /** Solution of exercise 4.06 */ final static String EXC4_06="Exercise 4.06";
vmarket:   /** Solution of exercise 4.07 */ final static String EXC4_07="Exercise 4.07";
vmarket:   /** Solution of exercise 4.08 */ final static String EXC4_08="Exercise 4.08";
vmarket:   /** Solution of exercise 4.09 */ final static String EXC4_09="Exercise 4.09";
vmarket:   /** Solution of exercise 4.10 */ final static String EXC4_10="Exercise 4.10";
vmarket:   /** Solution of exercise 4.11 */ final static String EXC4_11="Exercise 4.11";
vmarket:   /** Solution of exercise 4.12 */ final static String EXC4_12="Exercise 4.12";
vmarket:   /** Solution of exercise 4.13 */ final static String EXC4_13="Exercise 4.13";
vmarket:   /** Solution of exercise 4.14 */ final static String EXC4_14="Exercise 4.14";
vmarket:   /** Solution of exercise 4.15 */ final static String EXC4_15="Exercise 4.15";
vmarket:   /** Solution of exercise 5.01 */ final static String EXC5_01="Exercise 5.01";
vmarket:   /** Solution of exercise 5.02 */ final static String EXC5_02="Exercise 5.02";
vmarket:   /** Solution of exercise 5.03 */ final static String EXC5_03="Exercise 5.03";
vmarket:   /** Solution of exercise 5.04 */ final static String EXC5_04="Exercise 5.04";
vmarket:   /** Solution of exercise 5.05 */ final static String EXC5_05="Exercise 5.05";
vmarket:   /** Solution of exercise 5.06 */ final static String EXC5_06="Exercise 5.06";
vmarket:   /** Solution of exercise 5.07 */ final static String EXC5_07="Exercise 5.07";
vmarket:   /** Solution of exercise 5.08 */ final static String EXC5_08="Exercise 5.08";
vmarket:   /** Solution of exercise 5.09 */ final static String EXC5_09="Exercise 5.09";
vmarket:   /** Solution of exercise 5.10 */ final static String EXC5_10="Exercise 5.10";
vmarket:   /** Solution of exercise 5.11 */ final static String EXC5_11="Exercise 5.11";
vmarket:   /** Solution of exercise 5.12 */ final static String EXC5_12="Exercise 5.12";
vmarket:   /** Solution of exercise 5.13 */ final static String EXC5_13="Exercise 5.13";
vmarket:   /** Solution of exercise 5.14 */ final static String EXC5_14="Exercise 5.14";
vmarket:   /** Solution of exercise 5.15 */ final static String EXC5_15="Exercise 5.15";
vmarket:   /** Solution of exercise 6.01 */ final static String EXC6_01="Exercise 6.01";
vmarket:   /** Solution of exercise 6.02 */ final static String EXC6_02="Exercise 6.02";
vmarket:   /** Solution of exercise 6.03 */ final static String EXC6_03="Exercise 6.03";
vmarket:   /** Solution of exercise 6.04 */ final static String EXC6_04="Exercise 6.04";
vmarket:   /** Solution of exercise 6.05 */ final static String EXC6_05="Exercise 6.05";
vmarket:   /** Solution of exercise 6.06 */ final static String EXC6_06="Exercise 6.06";
vmarket:   /** Solution of exercise 6.07 */ final static String EXC6_07="Exercise 6.07";
vmarket:   /** Solution of exercise 6.08 */ final static String EXC6_08="Exercise 6.08";
vmarket:   /** Solution of exercise 6.09 */ final static String EXC6_09="Exercise 6.09";
vmarket:   /** Solution of exercise 6.10 */ final static String EXC6_10="Exercise 6.10";
vmarket:   /** Solution of exercise 6.11 */ final static String EXC6_11="Exercise 6.11";
vmarket:   /** Solution of exercise 6.12 */ final static String EXC6_12="Exercise 6.12";
vmarket:   /** Solution of exercise 6.13 */ final static String EXC6_13="Exercise 6.13";
vmarket:   /** Solution of exercise 6.14 */ final static String EXC6_14="Exercise 6.14";
vmarket:   /** Solution of exercise 6.15 */ final static String EXC6_15="Exercise 6.15";
vmarket:   /** Solution of exercise 7.01 */ final static String EXC7_01="Exercise 7.01";
vmarket:   /** Solution of exercise 7.02 */ final static String EXC7_02="Exercise 7.02";
vmarket:   /** Solution of exercise 7.03 */ final static String EXC7_03="Exercise 7.03";
vmarket:   /** Solution of exercise 7.04 */ final static String EXC7_04="Exercise 7.04";
vmarket:   /** Solution of exercise 7.05 */ final static String EXC7_05="Exercise 7.05";
vmarket:   /** Solution of exercise 7.06 */ final static String EXC7_06="Exercise 7.06";
vmarket:   /** Solution of exercise 7.07 */ final static String EXC7_07="Exercise 7.07";
vmarket:   /** Solution of exercise 7.08 */ final static String EXC7_08="Exercise 7.08";
vmarket:   /** Solution of exercise 7.09 */ final static String EXC7_09="Exercise 7.09";
vmarket:   /** Solution of exercise 7.10 */ final static String EXC7_10="Exercise 7.10";
vmarket:   /** Solution of exercise A.01 */ final static String EXCA_01="Exercise A.01";
vmarket:   /** Solution of exercise B.01 */ final static String EXCB_01="Exercise B.01";
vmarket:   /** Solution of exercise C.01 */ final static String EXCC_01="Exercise C.01";
vmarket:   /** Solution of exercise D.01 */ final static String EXCD_01="Exercise D.01";
vmarket:   /** Solution of exercise E.01 */ final static String EXCE_01="Exercise E.01";
vmarket:   /** Solution of exercise F.01 */ final static String EXCF_01="Exercise F.01";
vmarket:   /** Vector of the names of all the schemes @see Solution */
vmarket:   final static String[] schemeNames = 
vmarket:      {EUSTY,    AMSTY,    EUNORM,   AMNORM,
vmarket:       INBAR,    OUTBAR,   MCPART,   VASICEK,  
vmarket:       EXC1_01,  EXC1_02,  EXC1_03,  EXC1_04,  EXC1_05,
vmarket:       EXC1_06,  EXC1_07,  EXC1_08,  EXC1_09,  EXC1_10,
vmarket:       EXC2_01,  EXC2_02,  EXC2_03,  EXC2_04,  EXC2_05,
vmarket:       EXC2_06,  EXC2_07,  EXC2_08,  EXC2_09,  EXC2_10,
vmarket:       EXC3_01,  EXC3_02,  EXC3_03,  EXC3_04,  EXC3_05,
vmarket:       EXC4_01,  EXC4_02,  EXC4_03,  EXC4_04,  EXC4_05,
vmarket:       EXC4_06,  EXC4_07,  EXC4_08,  EXC4_09,  EXC4_10,
vmarket:       EXC4_11,  EXC4_12,  EXC4_13,  EXC4_14,  EXC4_15,
vmarket:       EXC5_01,  EXC5_02,  EXC5_03,  EXC5_04,  EXC5_05,
vmarket:       EXC5_06,  EXC5_07,  EXC5_08,  EXC5_09,  EXC5_10,
vmarket:       EXC5_11,  EXC5_12,  EXC5_13,  EXC5_14,  EXC5_15,
vmarket:       EXC6_01,  EXC6_02,  EXC6_03,  EXC6_04,  EXC6_05,
vmarket:       EXC6_06,  EXC6_07,  EXC6_08,  EXC6_09,  EXC6_10,
vmarket:       EXC6_11,  EXC6_12,  EXC6_13,  EXC6_14,  EXC6_15,
vmarket:       EXC7_01,  EXC7_02,  EXC7_03,  EXC7_04,  EXC7_05,
vmarket:       EXC7_06,  EXC7_07,  EXC7_08,  EXC7_09,  EXC7_10,
vmarket:       EXCA_01,  EXCB_01,  EXCC_01,  EXCD_01,  EXCE_01,
vmarket:       EXCF_01};
vmarket:   /** GUI list of all the schemes */
vmarket:   private MyChoice schemeSelection;
vmarket:                                                             
vmarket:   /** Call option teminal payoff */
vmarket:   final static String CALL = "Call";
vmarket:   /** Put option teminal payoff */
vmarket:   final static String PUT = "Put";
vmarket:   /** Binary or digital option teminal payoff */
vmarket:   final static String BINARY = "VSpread";
vmarket:   /** A box as initial condition */
vmarket:   final static String BOX = "SuperShr";
vmarket:   /** Discount function or Swap initial condition */
vmarket:   final static String CONST = "Constant";
vmarket:   /** Yield curve, interest rate term structure */
vmarket:   final static String YIELD = "Yield";
vmarket:   /** A gaussian as initial condition */
vmarket:   final static String GAUSSIAN = "Gaussian";
vmarket:   /** Vector of the names of initial conditions @see ShapeFunction */
vmarket:   final static String[] icNames = {PUT, CALL, BINARY, BOX, 
vmarket:                                    CONST,  YIELD, GAUSSIAN};
vmarket:   /** GUI list of all the ICs */
vmarket:   private MyChoice icSelection;
vmarket: 
vmarket:   /** Operate edit mode with TAG parameters displayed by default */
vmarket:   final static String EDIT = "Double-click below:";
vmarket:   /** Operate edit mode with ALL parameters displayed */
vmarket:   final static String EDITALL = "Show all parameters";
vmarket:   /** Operate a console output of the function values */
vmarket:   final static String CONSOLE = "Print data to console  ";
vmarket:   /** Vector of the names of all the operations */
vmarket:   static String[] operNames  = {EDIT, EDITALL, CONSOLE};
vmarket:   /** GUI list of all the operations */
vmarket:   private MyChoice operSelection;
vmarket: 
vmarket:   /** The plot area */
vmarket:   private PlotArea plotArea;
vmarket:   /** The run parameters */
vmarket:   private RunData runData;
vmarket:   /** Text for Start/Stop button */
vmarket:   private final String startName = "Start/Stop";
vmarket:   /** Text for Step 1 button */
vmarket:   private final String step1Name = "Step 1";
vmarket:   /** Text for Step 10 button */
vmarket:   private final String step10Name = "Step 10";
vmarket:   /** Text for toggle display button */
vmarket:   private final String displayName = "Toggle Display";
vmarket:   /** Text for initialize button */
vmarket:   private final String initializeName = "Reset";
vmarket: 
vmarket:   /** Whether the simulation is running */
vmarket:   private boolean frozen = true;
vmarket:   /** Current step number */
vmarket:   private int step = 0;
vmarket:   /** Operate nsteps before stopping */
vmarket:   private int nstep = -1;
vmarket:   /** Milliseconds between plots */
vmarket:   private int delay = 84;
vmarket:   /** Thread label */
vmarket:   Thread runThread;
vmarket:   /** Potentially reset by main */
vmarket:   private boolean isAnApplet = true;
vmarket: 
vmarket:   /** The solution */
vmarket:   private Solution solution;
vmarket: 
vmarket: 
vmarket:   /** Information
vmarket:   ****************************************************************************/
vmarket:   public String getAppletInfo() {
vmarket:     return "VMARKET 4.0 -- an educational software (C) 2002-2004 by A. Jaun";
vmarket:   }
vmarket: 
vmarket:   /** Master initialization and layout 
vmarket:   ****************************************************************************/
vmarket:   public void init() {
vmarket:     topicSelection = new MyChoice(topicNames);
vmarket:     methodSelection = new MyChoice(methodNames);
vmarket:     schemeSelection = new MyChoice(schemeNames);
vmarket:     icSelection = new MyChoice(icNames);
vmarket:     operSelection = new MyChoice(operNames);
vmarket:     runData = new RunData(this);
vmarket: 
vmarket:     if (isAnApplet) tagModify();
vmarket:     createWindow();
vmarket:     createSolution();
vmarket:     setInitialCondition(solution);
vmarket:   } // init
vmarket: 
vmarket:   /** Instanciate a GUI window
vmarket:       @see Solution
vmarket:   ***************************************************************************/
vmarket:   private void createWindow(){
vmarket:     // First the layout
vmarket:     GridBagLayout gb = new GridBagLayout();
vmarket:     GridBagConstraints c = new GridBagConstraints();
vmarket:     
vmarket:     setFont(new Font("Courier", Font.PLAIN, 12));
vmarket:     setLayout(gb);
vmarket: 
vmarket:     // Prepare for top row of buttons
vmarket:     c.gridwidth = 1;
vmarket:     c.gridheight = 1;
vmarket:     c.weightx = 1;
vmarket:     c.fill = GridBagConstraints.HORIZONTAL;
vmarket: 
vmarket:     // Engage
vmarket:     gbAdd(gb, c, methodSelection);
vmarket:     gbAdd(gb, c, schemeSelection);
vmarket:     gbAdd(gb, c, icSelection);
vmarket:     gbAdd(gb, c, topicSelection);
vmarket:     c.gridwidth = GridBagConstraints.REMAINDER; //end of row
vmarket:     gbAdd(gb, c, operSelection);
vmarket: 
vmarket:     // Mr Data, take us to the next row please
vmarket:     c.gridwidth = GridBagConstraints.RELATIVE;
vmarket:     c.gridheight = 1;
vmarket:     c.gridwidth = 4;
vmarket:     c.weightx = 0;
vmarket:     c.weighty = 1;
vmarket:     c.fill = GridBagConstraints.BOTH;
vmarket:     plotArea = new PlotArea();
vmarket:     gbAdd(gb, c, plotArea);
vmarket:     c.gridwidth = GridBagConstraints.REMAINDER; //end of row
vmarket:     
vmarket:     gbAdd(gb, c, runData);
vmarket: 
vmarket:     // Finally the last row
vmarket:     c.gridwidth = 1;
vmarket:     c.weighty = 0;
vmarket:     c.fill = GridBagConstraints.HORIZONTAL;
vmarket:     gbAdd(gb, c, new Button(startName));
vmarket:     gbAdd(gb, c, new Button(step1Name));
vmarket:     gbAdd(gb, c, new Button(step10Name));
vmarket:     gbAdd(gb, c, new Button(displayName));
vmarket:     c.gridwidth = GridBagConstraints.REMAINDER; //end of row
vmarket:     gbAdd(gb, c, new Button(initializeName));
vmarket: 
vmarket:   } // createWindow
vmarket: 
vmarket: 
vmarket:   /** Helper method for adding objects to a GridBagLayout
vmarket:       @param gb The layout
vmarket:       @param c The constraints
vmarket:       @param item The object to add
vmarket:   */
vmarket:   private void gbAdd(GridBagLayout gb, GridBagConstraints c, Component item){
vmarket:     gb.setConstraints(item, c);
vmarket:     add(item);
vmarket:   } // gbAdd
vmarket: 
vmarket:  
vmarket:   /** Instanciate a solution and select the method and scheme for computations
vmarket:       @see Solution
vmarket:       @return A new Solution
vmarket:   ***************************************************************************/
vmarket:   private void createSolution(){
vmarket:     runData.createMesh();
vmarket:     if(methodSelection.getSelectedItem().equals(FD)) {
vmarket:       solution = new FDSolution(runData);
vmarket:     } else if(methodSelection.getSelectedItem().equals(FEM)){
vmarket:       solution = new FEMSolution(runData);
vmarket:     } else if(methodSelection.getSelectedItem().equals(PPP)){
vmarket:       solution = new MCPSolution(runData);
vmarket:     } else if(methodSelection.getSelectedItem().equals(PPPS)){
vmarket:       solution = new MCPDrawSolution(runData);
vmarket:     } else if(methodSelection.getSelectedItem().equals(SMP)){
vmarket:       solution = new MCSSolution(runData);
vmarket:     } else if(methodSelection.getSelectedItem().equals(SMPS)){
vmarket:       solution = new MCSDrawSolution(runData);
vmarket:     } else {
vmarket:       /**  throw new MethodException("Can't find method"
vmarket:  + methodSelection.getSelectedItem());  */
vmarket:     } // if
vmarket: 
vmarket:     String ch;                        //Synchronize the choices is delicate
vmarket:     topicSelection.sync(solution);
vmarket:     ch=topicSelection.getSelectedItem();
vmarket:     if(ch==null)  System.out.println("ERROR in vmarket: topicSelection ="+ch);
vmarket:     solution.setTopic(ch);
vmarket:     ch=methodSelection.getSelectedItem();
vmarket:     if(ch==null)  System.out.println("ERROR in vmarket: methodSelection ="+ch);
vmarket:     solution.setMethod(ch);
vmarket:     schemeSelection.sync(solution);
vmarket:     ch=schemeSelection.getSelectedItem();
vmarket:     if(ch==null)  System.out.println("ERROR in vmarket: schemeSelection ="+ch);
vmarket:     solution.setScheme(ch);
vmarket:     plotArea.setSolution(solution);
vmarket:   } // createSolution
vmarket: 
vmarket:   /** Set the initial condition according to the runData parameters.
vmarket:       @param solution The solution to initialize
vmarket:       @see Solution
vmarket:   ****************************************************************************/
vmarket:   private void setInitialCondition(Solution solution){
vmarket:     step = 0;
vmarket:     double strike = runData.getParamValue(runData.strikePriceNm);
vmarket:     double amplit = runData.getParamValue(runData.shapeAmplNm);
vmarket:     double slope  = runData.getParamValue(runData.shapeSlopeNm);
vmarket:     double convex = runData.getParamValue(runData.shapeConvexNm);
vmarket:     double meshLen= runData.getParamValue(runData.meshLengthNm);
vmarket:     if(icSelection.getSelectedItem().equals(CALL)){
vmarket:       solution.discretize(new ShapeCall(strike));
vmarket:     } else if(icSelection.getSelectedItem().equals(PUT)){
vmarket:       solution.discretize(new ShapePut(strike));
vmarket:     } else if(icSelection.getSelectedItem().equals(BOX)){
vmarket:       solution.discretize(new ShapeBox(strike,amplit,slope));
vmarket:     } else if(icSelection.getSelectedItem().equals(BINARY)){
vmarket:       solution.discretize(new ShapeSpread(strike,amplit,slope));
vmarket:     } else if(icSelection.getSelectedItem().equals(CONST)){
vmarket:       solution.discretize(new ShapeBox(meshLen,amplit,2*meshLen));
vmarket:     } else if(icSelection.getSelectedItem().equals(YIELD)){
vmarket:       solution.discretize(new ShapeYield(strike,amplit,slope,convex));
vmarket:     } else if(icSelection.getSelectedItem().equals(GAUSSIAN)){
vmarket:       solution.discretize(new ShapeGaussian(strike,amplit,slope));
vmarket:     } else {
vmarket:       /**  throw new ShapeException("Can't find shape"
vmarket:           + icSelection.getSelectedItem()); */
vmarket:     } // if
vmarket:     solution.setIC(icSelection.getSelectedItem());
vmarket:     solution.setTime(0);
vmarket:     solution.rescale(false);
vmarket:     solution.previous(runData);
vmarket:     solution.updateHeaders(runData, step);
vmarket:     plotArea.repaint();
vmarket:   } // setInitialCondition
vmarket: 
vmarket:   /** Modify defaults parameters the HTML tags from the web page
vmarket:   ****************************************************************************/
vmarket:   public void tagModify() {
vmarket:     String tmp;
vmarket:     if((tmp = getParameter("topic")) != null)   topicSelection.select(tmp);
vmarket:     if((tmp = getParameter("scheme")) != null) schemeSelection.select(tmp);
vmarket:     if((tmp = getParameter("ic")) != null)         icSelection.select(tmp);
vmarket:     if((tmp = getParameter("method")) != null) methodSelection.select(tmp);
vmarket:     runData.tagModify(this);
vmarket:   } // tagModify
vmarket: 
vmarket:   /**  Parameter info
vmarket:   ****************************************************************************/
vmarket:   public String[][] getParameterInfo() {
vmarket:     String[][] info = {
vmarket:       {"topic",       "name",   "Type of the financial product"},
vmarket:       {"scheme",      "name",   "Particular flavour of the produt"},
vmarket:       {"method",      "name",   "Numerical method to use for the calculation"},
vmarket:       {"ic",          "name",   "Shape of the initial condition"},
vmarket:       {"RunTime",     "double", "Run time [years]"},
vmarket:       {"Drift",       "double", "Drift of the stochastic variable"},
vmarket:       {"Volatility",  "double", "Volatility of the stochastic variable"},
vmarket:       {"LogNkappa",   "double", "Exponent in the random walk"},
vmarket:       {"SpotRate",    "double", "Present value of the interest rate [1/year]"},
vmarket:       {"Dividend",    "double", "Dividend yield [1/years]"},
vmarket:       {"StrikePrice", "double", "Option exercise price at expiry"},
vmarket:       {"MktPriceRsk", "double", "Market price of risk"},
vmarket:       {"MeanRevTarg", "double", "Target of mean reversion process"},
vmarket:       {"MeanRevVelo", "double", "Velocity of mean reversion process"},
vmarket:       {"Shape0",      "double", "Initial shape amplitude"},
vmarket:       {"Shape1",      "double", "Initial shape slope"},
vmarket:       {"Shape2",      "double", "Initial shape convexity"},
vmarket:       {"MeshLeft",    "double", "Lower value of x-axis"},
vmarket:       {"MeshLength",  "double", "Length of x-axis"},
vmarket:       {"MeshPoints",  "int",    "The number of mesh points on x-axis"},
vmarket:       {"Walkers",     "int",    "The number of random walkers"},
vmarket:       {"TimeStep",    "double", "Time step [1/year]"},
vmarket:       {"TimeTheta",   "double", "Implicity parameter for time integration"},
vmarket:       {"TuneQuad",    "double", "Tunable quadrature parameter for FEM"},
vmarket:       {"UserInteger", "int",    "User defined data"},
vmarket:       {"UserDouble",  "double", "User defined data"}
vmarket:      };
vmarket:     return info;
vmarket:   } // getParameterInfo
vmarket: 
vmarket:   /** Applet start a new thread
vmarket:   ****************************************************************************/
vmarket:   public void start() {
vmarket:     if (runThread == null) {               //Start a new thread
vmarket:       runThread = new Thread(this);
vmarket:     }
vmarket:     if (frozen) {                          //Wait for an action from the user
vmarket:     } else { runThread.start(); }          //... or on with the calculation
vmarket:   }
vmarket: 
vmarket:   /** Applet stop
vmarket:   ****************************************************************************/
vmarket:   public void stop() {
vmarket:     runThread = null;
vmarket:     frozen    = true;
vmarket:    }
vmarket: 
vmarket:   /** Contains the main loop for the time stepping. 
vmarket:       @see ShapeFunction
vmarket:       @see Mesh
vmarket:       @see Solution
vmarket:   ****************************************************************************/
vmarket:   public void run() {
vmarket:     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
vmarket:     long startTime = System.currentTimeMillis();
vmarket:     Thread currentThread = Thread.currentThread();
vmarket:     double runTime  = runData.getParamValue(runData.runTimeNm);
vmarket:     double timeStep = runData.getParamValue(runData.timeStepNm);
vmarket: 
vmarket:     while (currentThread == runThread && !frozen &&
vmarket:            ( solution.getTime() < runTime && nstep < 0 ||
vmarket:            nstep > 0 ) ) {
vmarket:       step++; nstep--;                               //One step forward in time
vmarket: 
vmarket:       solution.incTime(timeStep);
vmarket:       solution.updateHeaders(runData, step);
vmarket:       solution.next(runData);
vmarket:       
vmarket:       if(operSelection.getSelectedItem().equals(CONSOLE))
vmarket:         solution.output(step);                       //Output in ASCII
vmarket:       plotArea.repaint();                            //Update graphics
vmarket: 
vmarket:       try { startTime += delay;                      //Delay according to lag
vmarket:       Thread.sleep(Math.max(30, startTime-System.currentTimeMillis()));
vmarket:       } catch (InterruptedException e) { break; }
vmarket:     } // while
vmarket:     frozen=true;
vmarket:   } // run
vmarket: 
vmarket:   /** A new mesh is created by RunData
vmarket:       @see RunData
vmarket:   ****************************************************************************/
vmarket:   public void runDataNotifyMesh(){
vmarket:     createSolution();
vmarket:     setInitialCondition(solution);
vmarket:   } // runDataNotifyMesh
vmarket: 
vmarket:   /** The number of particles is changed by RunData
vmarket:       @see RunData
vmarket:   ****************************************************************************/
vmarket:   public void runDataNotifyWalkers(){
vmarket:     createSolution();
vmarket:     setInitialCondition(solution);
vmarket:   } // runDataNotifyWalkers
vmarket: 
vmarket:   /** Responds to the user actions through the mouse and buttons (Java1.0).
vmarket:       Yes, we know this sucks compare to Java 1.1, but we want to be
vmarket:       compatible with as many browsers as possible. There is a lot of
vmarket:       old stuff out there...
vmarket:       @deprecated
vmarket:   ****************************************************************************/
vmarket:   public boolean action(Event e, Object arg) {
vmarket:     if(e.target instanceof Choice){
vmarket:       for(int i = 0; i < topicNames.length; i++)
vmarket:  if(((String)arg).equals(topicNames[i])){
vmarket:          solution.setTopic(topicSelection.getSelectedItem());
vmarket:          schemeSelection.sync(solution);
vmarket:          solution.setScheme(schemeSelection.getSelectedItem());
vmarket:          return true;
vmarket:  } // if
vmarket:       for(int i = 0; i < methodNames.length; i++)
vmarket:  if(((String)arg).equals(methodNames[i])){
vmarket:          createSolution();
vmarket:          solution.setScheme(schemeSelection.getSelectedItem());
vmarket:          setInitialCondition(solution);
vmarket:          return true;
vmarket:  } // if
vmarket:       for(int i = 0; i < schemeNames.length; i++)
vmarket:  if(((String)arg).equals(schemeNames[i])){
vmarket:          solution.setScheme(schemeSelection.getSelectedItem());
vmarket:          setInitialCondition(solution);
vmarket:          return true;
vmarket:  } // if
vmarket:       for(int i = 0; i < icNames.length; i++)
vmarket:  if(((String)arg).equals(icNames[i])){
vmarket:          setInitialCondition(solution);
vmarket:          solution.setIC(icSelection.getSelectedItem());
vmarket:          return true;
vmarket:  } // if
vmarket:       if (EDIT.equals(operSelection.getSelectedItem()))
vmarket:          runData.displayTag(this);
vmarket:       if (EDITALL.equals(operSelection.getSelectedItem()))
vmarket:          runData.displayAll();
vmarket:     } // if
vmarket:     if(e.target instanceof Button) {
vmarket:       boolean ret = false;
vmarket:  if(((String)arg).equals(startName)) {
vmarket:          frozen = !frozen;
vmarket:          nstep = -1;
vmarket:          ret = true;
vmarket:  } else if(((String)arg).equals(step1Name)) {
vmarket:          frozen = false;
vmarket:          nstep = 1;
vmarket:          ret = true;
vmarket:  } else if(((String)arg).equals(step10Name)) {
vmarket:          frozen = false;
vmarket:          nstep = 10;
vmarket:          ret = true;
vmarket:  } else if(((String)arg).equals(displayName)) {
vmarket:          boolean headers=plotArea.toggleHeaders();
vmarket:          solution.rescale(headers);
vmarket:          plotArea.repaint();
vmarket:          ret = true;
vmarket:  } else if(((String)arg).equals(initializeName)) {
vmarket:          frozen = true;
vmarket:           runData.createMesh();
vmarket:           createSolution();
vmarket:          setInitialCondition(solution);
vmarket:          step = 0;
vmarket:          nstep = -1;
vmarket:          ret = true;
vmarket:  } // if
vmarket:  if(ret) {
vmarket:          runThread = null;
vmarket:          start();
vmarket:  } // if
vmarket:  return ret;
vmarket:     } // if
vmarket:     return false;
vmarket:   } // action
vmarket:  
vmarket:   /** Destroy application
vmarket:       @deprecated
vmarket:   ****************************************************************************/
vmarket:   public boolean handleEvent(Event e) {                               //Java1.0
vmarket:     if (e.id == Event.WINDOW_DESTROY) { System.exit(0); }
vmarket:     return super.handleEvent(e);
vmarket:   }
vmarket: 
vmarket:   /** Print mouse coordinates to console
vmarket:       @deprecated
vmarket:   ****************************************************************************/
vmarket:   //Java1.0:  public boolean mouseXXX(Event e, int x, int y)
vmarket:   //Java1.0:                 with XXX={Up, Down, Drag, Enter, Exit}
vmarket:   public boolean mouseDown(Event e, int x, int y) {                   //Java1.0
vmarket:     double[] coord = solution.measure(x,y);
vmarket:     double timeStep = runData.getParamValue(runData.timeStepNm);
vmarket:    
vmarket:     if (isAnApplet)
vmarket:       showStatus("Clicked coord ("+coord[0]+" ; "+coord[1]+")");
vmarket:     System.out.println("step="+step+
vmarket:                        "  time="+(step*timeStep)+
vmarket:                        "  coord=("+coord[0]+" ; "+coord[1]+")");
vmarket:     return true;
vmarket:   }
vmarket: 
vmarket:   /** Method to start the Applet as an application
vmarket:       @param args Not used
vmarket:   ****************************************************************************/
vmarket:   public static void main(String[] args) {
vmarket:     Frame f = new Frame("vmarket");
vmarket:     vmarket vMarket = new vmarket();
vmarket:     vMarket.isAnApplet = false;
vmarket:     vMarket.init();
vmarket:     f.add("Center", vMarket);
vmarket:     f.pack();
vmarket:     f.show();
vmarket:   } // main
vmarket: 
vmarket: } // vmarket
BandMatrix: /* $Id: BandMatrix.java,v 1.10 2002/07/24 16:22:55 pde Exp $ */
BandMatrix: 
BandMatrix: /***************************************************************************
BandMatrix:  * BandMatrix - Linear algebra package for band matrices
BandMatrix:  ***************************************************************************/
BandMatrix: public class BandMatrix {
BandMatrix:   double[][] m;                      //Matrix
BandMatrix:   double[]   d;&