Skip to content

Commit

Permalink
Adding black-scholes and phoenix codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdjayanga committed Jul 17, 2013
1 parent d197858 commit 6cf2dde
Show file tree
Hide file tree
Showing 123 changed files with 17,873 additions and 0 deletions.
50 changes: 50 additions & 0 deletions black-scholes/black-scholes/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Makefile
# For CS5923, Assignment 1, Q4 (Monte Carlo)
# Based on Prof. Kathy Yelick's material (at UC Berkeley)
#
#
# Include platform-dependent settings.
#
include Makefile.include

#
# NOTE: This only works for GNU Make

LDFLAGS += -Ldcmt0.4/lib -ldcmt

Q4_INCS = black_scholes.h gaussian.h parser.h random.h timer.h util.h
Q4_C_SRCS = black_scholes.c gaussian.c main.c parser.c random.c dcmt0.4/lib/random_seed.c timer.c util.c
Q4_C_OBJS = $(Q4_C_SRCS:.c=.o)
Q4_EXE = Q4.exe


all: Q4.exe

%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

Q4.exe: $(Q4_C_OBJS) dcmt0.4/lib/libdcmt.a
$(LINKER) $(CFLAGS) $(LDFLAGS) $(Q4_C_OBJS) -o $@

dcmt0.4/lib/libdcmt.a:
make -C dcmt0.4/lib

black_scholes.o: black_scholes.c black_scholes.h gaussian.h random.h util.h

gaussian.o: gaussian.c gaussian.h util.h

main.o: main.c black_scholes.h parser.h random.h timer.h

parser.o: parser.c parser.h

random.o: random.c random.h

dcmt0.4/lib/random_seed.o: dcmt0.4/lib/random_seed.c

timer.o: timer.c timer.h

util.o: util.c util.h

clean:
make -C dcmt0.4/lib clean
rm -f $(Q4_C_OBJS) $(Q4_EXE)
21 changes: 21 additions & 0 deletions black-scholes/black-scholes/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Makefile.include
# Platform-dependent settings for Q4
#

#CC = icc
CC = gcc

COPTFLAGS = -O2
#COPTFLAGS = -g

CPPFLAGS =

#CFLAGS = $(COPTFLAGS)
CFLAGS = -Wall $(COPTFLAGS)

#LINKER = icc
LINKER = gcc

LDFLAGS = -lm -lpthread


180 changes: 180 additions & 0 deletions black-scholes/black-scholes/black_scholes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include "black_scholes.h"
#include "gaussian.h"
#include "random.h"
#include "util.h"

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>


/**
* This function is what you compute for each iteration of
* Black-Scholes. You don't have to understand it; just call it.
* "gaussian_random_number" is the current random number (from a
* Gaussian distribution, which in our case comes from gaussrand1()).
*/
static inline double
black_scholes_value (const double S,
const double E,
const double r,
const double sigma,
const double T,
const double gaussian_random_number)
{
const double current_value = S * exp ( (r - (sigma*sigma) / 2.0) * T +
sigma * sqrt (T) * gaussian_random_number );
return exp (-r * T) *
((current_value - E < 0.0) ? 0.0 : current_value - E);
/* return exp (-r * T) * max_double (current_value - E, 0.0); */
}


/**
* Compute the standard deviation of trials[0 .. M-1].
*/
static double
black_scholes_stddev (void* the_args)
{
black_scholes_args_t* args = (black_scholes_args_t*) the_args;
const double mean = args->mean;
const int M = args->M;
double variance = 0.0;
int k;

for (k = 0; k < M; k++)
{
const double diff = args->trials[k] - mean;
/*
* Just like when computing the mean, we scale each term of this
* sum in order to avoid overflow.
*/
variance += diff * diff / (double) M;
}

args->variance = variance;
return sqrt (variance);
}


/**
* Take a pointer to a black_scholes_args_t struct, and return NULL.
* (The return value is irrelevant, because all the interesting
* information is written to the input struct.) This function runs
* Black-Scholes iterations, and computes the local part of the mean.
*/
static void*
black_scholes_iterate (void* the_args)
{
black_scholes_args_t* args = (black_scholes_args_t*) the_args;

/* Unpack the IN/OUT struct */

/* IN (read-only) parameters */
const int S = args->S;
const int E = args->E;
const int M = args->M;
const double r = args->r;
const double sigma = args->sigma;
const double T = args->T;

/* OUT (write-only) parameters */
double* trials = args->trials;
double mean = 0.0;

/* Temporary variables */
gaussrand_state_t gaussrand_state;
void* prng_stream = NULL;
int k;

/* Spawn a random number generator */
prng_stream = spawn_prng_stream (0);

/* Initialize the Gaussian random number module for this thread */
init_gaussrand_state (&gaussrand_state);

/* Do the Black-Scholes iterations */
for (k = 0; k < M; k++)
{
const double gaussian_random_number = gaussrand1 (&uniform_random_double,
prng_stream,
&gaussrand_state);
trials[k] = black_scholes_value (S, E, r, sigma, T,
gaussian_random_number);

/*
* We scale each term of the sum in order to avoid overflow.
* This ensures that mean is never larger than the max
* element of trials[0 .. M-1].
*/
mean += trials[k] / (double) M;
}

/* Pack the OUT values into the args struct */
args->mean = mean;

/*
* We do the standard deviation computation as a second operation.
*/

free_prng_stream (prng_stream);
return NULL;
}



void
black_scholes (confidence_interval_t* interval,
const double S,
const double E,
const double r,
const double sigma,
const double T,
const int M)
{
black_scholes_args_t args;
double mean = 0.0;
double stddev = 0.0;
double conf_width = 0.0;
double* trials = NULL;

assert (M > 0);
trials = (double*) malloc (M * sizeof (double));
assert (trials != NULL);

args.S = S;
args.E = E;
args.r = r;
args.sigma = sigma;
args.T = T;
args.M = M;
args.trials = trials;
args.mean = 0.0;
args.variance = 0.0;

(void) black_scholes_iterate (&args);
mean = args.mean;
stddev = black_scholes_stddev (&args);

conf_width = 1.96 * stddev / sqrt ((double) M);
interval->min = mean - conf_width;
interval->max = mean + conf_width;

/* Clean up and exit */

deinit_black_scholes_args (&args);
}


void
deinit_black_scholes_args (black_scholes_args_t* args)
{
if (args != NULL)
if (args->trials != NULL)
{
free (args->trials);
args->trials = NULL;
}
}

91 changes: 91 additions & 0 deletions black-scholes/black-scholes/black_scholes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef _black_scholes_h
#define _black_scholes_h

#include <math.h>

/**
* In (read-only) and out (write-only) arguments to the function(s)
* that do(es) Black-Scholes iterations. This is used for both the
* sequential and parallel (threaded with Pthreads) implementation.
*
* Fields marked [IN] are read-only inputs; don't modify them once
* they are set.
*
* Fields marked [OUT] are outputs to be computed by your code.
*
* You might add additional field(s) to this struct.
*
* @note The typedef lets you refer to this type without saying
* "struct" in front of it, but it means that you have to include this
* header file if you ever use this datatype. Example:
*
* #include "black_scholes.h"
* #include <stdio.h>
*
* void
* foo (black_scholes_args_t* args)
* {
* printf ("M = %d\n", args->M);
* ...
* }
*/
typedef struct __black_scholes_args_t {
/**
* [IN] Various parameters of the Black-Scholes MC method.
*/
double S, E, r, sigma, T;
/**
* [IN] Number of Black-Scholes MC iterations.
*/
int M;
/**
* [OUT] Array (of M elements) containing the results of each of the
* M trials.
*/
double* trials;
/**
* [OUT] Arithmetic mean of trials[0 .. M-1].
*/
double mean;

/**
* [OUT] variance of trials[0 .. M-1].
*/
double variance;
} black_scholes_args_t;

/**
* Frees any malloc'd objects in the args struct, without freeing the
* args pointer itself.
*/
void
deinit_black_scholes_args (black_scholes_args_t* args);

/**
* Confidence interval [min,max].
*
* The typedef lets you refer to this type without saying "struct"
* in front of it, but it means that you have to include this header
* file if you ever use this datatype.
*/
typedef struct __confidence_interval_t {
double min, max;
} confidence_interval_t;

/**
* Run the Black-Scholes MC simulation using the parameters S, E, r,
* sigma, and T, with M total trials.
*
* @note You might need to modify the signature of this function.
*/
void
black_scholes (confidence_interval_t* interval,
const double S,
const double E,
const double r,
const double sigma,
const double T,
const int M);


#endif /* _black_scholes_h */
Binary file added black-scholes/black-scholes/dcmt0.4/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions black-scholes/black-scholes/dcmt0.4/CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version 0.4
A bug in sgenrand_mt() in lib/genmtrand.c is fixed. (2007/8/11)

version 0.3
``uint32'' is replaced by ``uint32_t.'' (2006/11/27)

version 0.2.1
``inttypes.h'' is included.
typedef unsigned int uint32; --> typedef uint32_t uint32;
A C99 complier is required. (2006/11/18)

version 0.2
_sgenrand_dc() in lib/mt19937.c is changed. (2002/2/21)
sgenrand_mt() in lib/genmtrand.c is changed. (2002/2/21)

Loading

0 comments on commit 6cf2dde

Please sign in to comment.