From cwou@npac.syr.edu Mon Jul 28 20:59:15 1997 Date: Fri, 25 Jul 1997 16:53:51 -0400 From: Chao-Wei Ou To: paulc@dante.npac.syr.edu Cc: cwou@dante.npac.syr.edu Subject: Change in Miloje's Markov code. Also can be applied to Noise. 1. Format of J. P. Morgan risk matrics for monthly and regular File 1: DV051597.RM3 - daily volatility file MV051597.RM3 - monthly volatility file BV051597.RM3 - regular volatility file Format: SERIES, PRICE/YIELD, DECAYFCTR, PRICEVOL, YIELDVOL ARS.XS.VOLD, 1.000100, 1.000, 0.036706, ND ATS.XS.VOLD, 0.083660, 1.000, 0.758248, ND AUD.XS.VOLD, 0.775800, 1.000, 0.714939, ND BEF.XS.VOLD, 0.028523, 1.000, 0.750566, ND SERIES: (Swift currency code|commodity code).(Maturity and asset). (VOLD) File 2: DC051597.RM3 - daily correlation file MC051597.RM3 - monthly correlation file BC051597.RM3 - regular correlation file Format: SERIES, CORRELATION ARS.XS.ARS.XS.CORD, 1.000000 ARS.XS.ATS.XS.CORD, 0.062541 ARS.XS.AUD.XS.CORD, 0.009498 ARS.XS.BEF.XS.CORD, 0.075360 SERIES: (Swift currency code|commodity code).(Maturity and asset). (Swift currency code|commodity code).(Maturity and asset) 2. Parser for translating from J. P. Morgan data files to Markov input file/parameters. This parser will parse the previous two files to generate a simplified representation in a file. 3. Input file for markov: # JPMorgan data file - aquired from the parser JPMorgan.in # Disturbance data file - user/provider provide disturbance selection and # associated variance Disturbance.in # StartDate 0 # EndDate 10 # PeriodLength 1 # ProxyRatesContainer - output file proxy.out # NumberOfCycles 1 # RNGSeed - seed of random number generator 4567 # NumberOfMeasurementBlocks 5000 # SizeOfMeasurementBlock 10 # NumberOfRelaxBlocks 1000 # Number of Threads 4 # Output for every # samples 100 # Output results for every # samples 1000 4. Interface structure typedef struct mc_interface { int NumberOfRates; /* Total number of rates */ int StartDate; /* Starting simulation date */ int EndDate; /* Ending simulation date */ int PeriodLength; /* Simulation time unit */ int NumberOfTimeSteps; /* Total number of time steps */ char ProxyRatesContainer[80]; /* Output proxy rate file */ int NumberOfCycles; /* Number of MC cycle */ int RNGSeed; /* Random number seed */ int NumberOfMeasurementBlocks; /* Number of measurement blocks */ int SizeOfMeasurementBlock; /* Number of trials in a block */ int NumberOfRelaxBlocks; /* NUmber of relaxation blocks */ int DisturbanceType; /* Disturbance function selection */ REAL *Variances; /* Associated variances */ REAL *InitialValues; /* Initial values for rates */ int *NumberOfCouplings; /* Number of correlations for rates */ REAL **Couplings; /* Correlation matrix */ } MC_INTERFACE; This interface replaced the old three interface files to fit J.P. Morgan input files. This will be the base for designing API for variable time steps interface. 5. Change in codes. File: Interface.c char *GetALine(FILE *fp, char *InputString, int Size) void SimulationInterface(char *InterfaceFile) void FreeInterface() All these three functions were implemented to read the input from the interface file and set global parameters for Monte Carlo simulation. File: Modmarkov.c MOMENTS *CreateMoments(int, int) CreateMoments is newly added to create global moments to average all moments from each thread. void FreeMOMENTS(MOMENTS *, int) FreeMOMENTS is to free MOMENTS space. rateVector ** ProxyRateServer() ProxyRateServer has been changed to be multi-threading main subroutine. It will start user-defined number of threads to run Monte Carlo simulation to generate samples. The following handles were used: RunMutex - to protect sensitive execution statements. MomentMutex - to synchronize the global moments calculation. MemoryMutex - to synchronize the shared memory allocation. SyncMutex - to synchronize all threads. SyncOutputMutex - to synchronize the output from Monte Carlo simulation. OutputMutex - to synchronize output results. SyncSemaphore - Synchroniztion. SyncOutputSemaphore - Synchronization for output. *MCThread - thread handles. File: Sync.c void Sync() Synchronization. void SyncOutput(char *msg, int iter) Synchronizing output. File: Memory.c void *mymalloc(int size, char *msg) Synchronized memory allocation with error checking. void *mycalloc(int num, int size, char *msg) Synchronized memory allocation and clear with error checking. File: Modmc.c Most functions of this file can not be used in multi-threading implementation because there are many global variables have to be changed to local variables. void SetNMCCycles(MONTECARLO *MCParams, int nc) int ImDone(MONTECARLO *MCParams) int IsRelaxed(MONTECARLO *MCParams) void SetRelaxFlag(MONTECARLO *MCParams, int rf) int HowManyCyclesLeft(MONTECARLO *MCParams) void CycleDone(MONTECARLO *MCParams) void SetRNGSeed(MONTECARLO *MCParams, int iseed) void SetNumMeasBlocks(MONTECARLO *MCParams, int mb) void SetMeasBlockSize(MONTECARLO *MCParams, int mbs) int HowManyMeasBlocks(MONTECARLO *MCParams) int HowBigMeasBlock(MONTECARLO *MCParams) int HowManyRelaxBlocks(MONTECARLO *MCParams) void SetNumRelaxBlocks(MONTECARLO *MCParams, int rb) void CreateConfDumpFile(MONTECARLO *MCParams, char *CDFile) int GetConfDumpFD(MONTECARLO *MCParams) void CreateRestartFile(MONTECARLO *MCParams, char *RFile) int GetRestartFD(MONTECARLO *MCParams) int SetupMetropolisMCSimulation(MONTECARLO *MCParams, int LogicThreadID) static int MetropolisUpdate(int MeasBlockSize, PATH *Rates, PROCESS *Params, REAL *AllCurrentRates, REAL *AllPastRates, REAL *AllFutureRates, REAL *shift, REAL *Sshift, REAL **Couplings, ProbFunc *ConditionalProbability, int Translation, int Reflexion) In MetropolisUpdate, there are many memory allocation which will cause NT memory management problem. NT doesn't free memory by using void free(void *) function and these memory allocations will cause out of memory (including virtual memory). Therefore, all temporary working variables (space) are setup before get into MetropolisUpdate. int GetMarkovSample(PATH *Disturbance, PROCESS *DisturbanceParams, int LogicThreadID) LogicThreadID is added to setup different seeds for generating random number. static int MetropolisMonteCarlo(MONTECARLO *MCParams, PATH *Rates, PROCESS *ProcessParams, int LogicThreadID) All temporary variables are setup in the subroutine. This routine will call MetropolisUpdate to generate samples. Periodical output routine was added in the Monte Carlo simulation main loop. Calculation for global paths and moments were added in this routine. void OutputCurrentExecTimeAndRates(int ID, int NumSamples, clock_t WallStart, clock_t WallEnd, PATH *Rates, MEASMOMENTS *Meas, FILE *TimeFD, FILE *MomentFP) This subroutine was designed to periodically output the status and results during the Monte Carlo simulation. This can be modified to save the configuration for stopping and restarting simulation. File: Moments.c MOMENTS *CreateMoments(int Size1, int Size2) To create MOMENTS to accumulate moments. MEASMOMENTS *InitMom(PATH *Path, int symm) To initialize the measurement. void FreeMOMENTS(MOMENTS *Moments, int Size) void FreeMEASMOMENTS(MEASMOMENTS *Meas, int Size) To free memory space. void GlobalAccMoments(MEASMOMENTS *Meas, int Size1, int Size2) To synchronize the accumulation on the global moments.