Student: Stanley

Computer Science

Stevens Institute of Technology Assignment 2 FE 522 – C++ Programming in Finance Due Date: April 26, 2020 For every problem below, create a different project folder. You should then put all these folders inside a .zip file with your name before submitting everything in Canvas. Remember to delete the cmake-build-debug folders before doing so. This .zip file should be accompanied by a .pdf file containing a report (one single report for the whole assignment). We do not provide test cases for any of the problems, and these must be provided by you. When providing a solution to a problem, you must be able to present test cases alongside it, otherwise it will not be accepted as a valid solution. If a problem requires input and/or output files, please provide the whole file content (if not large) in the body of the report. If the file is too large to be pleasantly presented in a report (larger than a page), please provide a sample. You should include these files in folders named "input" and "output", respectively, in the root folder of each project. In order for your code to work on any machine (not only yours), use relative paths to these files in your source code: for input files, use: "../../input/filename.txt" for output files, use: "../../output/filename.txt" Problem 1 (25 points). Design and implement an AmericanOption class. It should hold information such as option type (call or put), spot price (of the underlying asset), strike price, interest rate, volatility (of the underlying asset) and time to maturity. Don’t accept illegal values. Implement a getPrice() function which gives the price of the option using a binomial tree defined as a matrix (vector< vector >). Hint: https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector Problem 2 (25 points). Design and implement an Option class as the base class for both EuropeanOption and AmericanOption classes. It should contain all the member variables that are common between both option types, as well as a virtual function getPrice(). Moreover, it should contain four (non-virtual) functions to compute the numerical Greeks (getDelta() for spot price, getRho() for interest rate, getVega() for volatility, and getTheta() for time to maturity) of a given option. These functions should: Compute the price of the option by calling the virtual function getPrice(); Apply a small bump to the required pricing factor (the size of the bump should be a parameter of the function); Compute the "bumped price" of the option by calling the virtual function getPrice(); 1 Change the modified pricing factor back to its original value; Return the numeric approximation of the given Greek. After all the above is done, implement an external function (to the class) which takes a Option& as parameter and compute all four Greeks in a row. Compare the results you obtain for American and European (call and put) options. Problem 3 (20 points). In C, a string (often called a C string or a C-style string in C++ literature) is a zero-terminated array of characters. For example, p and q are equivalent: char p = " a s d f " ; char q = new char [ 5 ] ; q [ 0 ] = ’ a ’ ; q [ 1 ] = ’ s ’ ; q [ 2 ] = ’ d ’ ; q [ 3 ] = ’ f ’ ; q [ 4 ] = 0 ; In C, we cannot have member functions, we cannot overload functions, and we cannot define an operator (such as ==) for a struct. It follows that we need a set of (nonmember) functions to manipulate C-style strings: (a) Write a function, char* mystrdup(char*), that copies a C-style string into memory it allocates on the free store. (b) Write a function, char* myfindx(char* s, char* x), that finds the first occurrence of the C-style string x in s. (c) Write a function, int mystrcmp(char* s1, char* s2), that compares C-style strings. Let it return a negative number if s1 is lexicographically before s2, zero if s1 equals s2, and a positive number if s1 is lexicographically after s2. Do not use any standard library functions. Do not use subscripting; use the dereference operator * instead. Problem 4 (30 points). Implement the LinkedNode and SortedList classes as per the interfaces (and descriptions) presented below. Hint: https://www.geeksforgeeks.org/doubly-linked-list/ LinkedNode: This class will be used to store individual nodes of a doubly-linked list data structure. This class should end up being quite short and simple - no significant complexity is needed nor desired. The interface of LinkedNode should be exactly as follows: // The LinkedNode // a d o u b l y l i n k e d c l as s LinkedNode { private : c l a s s w i l l be t h e d a t a t y p e f o r i n d i v i d u a l nodes o f l i s t d a t a s t r u c t u r e . 2 // The v a l u e c o n t a i n e d w i t h i n t h i s node . int m_nodeValue ; // m_prevNode p o i n t s t o t h e node t h a t comes b e f o r e t h i s node i n // t h e d a t a s t r u c t u r e . I t w i l l be n u l l p t r i f t h i s i s t h e f i r s t // node . LinkedNode m_prevNode ; // m_nextNode p o i n t s t o t h e node t h a t comes a f t e r t h i s node i n t h e // d a t a s t r u c t u r e . I t w i l l be n u l l p t r i f t h i s i s t h e l a s t node . LinkedNode m_nextNode ; public : // The ONLY c o n s t r u c t o r f o r t h e LinkedNode c l a s s i t t a k e s i n t h e // newly c r e a t e d node ’ s p r e v i o u s p o i n t e r , v a l u e , and n e x t p o i n t e r , // and a s s i g n s them . I n p u t : // p o i n t e r t o t h e node t h a t comes b e f o r e t h i s node // v a l u e t o be s t o r e d i n t h i s node // p o i n t e r t o t h e node t h a t comes a f t e r t h i s one LinkedNode ( int value , LinkedNode prev , LinkedNode next ) ; // Returns t h e v a l u e s t o r e d w i t h i n t h i s node . int g e t V a l u e ( ) const ; // Returns t h e a d d r e s s o f t h e node t h a t comes b e f o r e t h i s node . LinkedNode g e t P r e v ( ) const ; // Returns t h e a d d r e s s o f t h e node t h a t f o l l o w s t h i s node . LinkedNode getNext ( ) const ; // S e t s t h e o b j e c t ’ s p r e v i o u s node p o i n t e r t o n u l l p t r . void s e t P r e v i o u s P o i n t e r T o N u l l ( ) ; // S e t s t h e o b j e c t ’ s n e x t node p o i n t e r t o n u l l p t r . void s e t N e x t P o i n t e r T o N u l l ( ) ; // This f u n c t i o n DOES NOT modify " t h i s " node . I n s t e a d , i t u s e s t h e // p o i n t e r s c o n t a i n e d w i t h i n t h i s node t o change t h e p r e v i o u s and // n e x t nodes so t h a t t h e y p o i n t t o t h i s node a p p r o p r i a t e l y . In // o t h e r words , i f " t h i s " node i s s e t up such t h a t i t s prevNode // p o i n t e r p o i n t s t o a node ( c a l l i t "A") , and " t h i s " node ’ s // nextNode p o i n t e r p o i n t s t o a n o t h e r node ( c a l l i t "B" ) , t h e n // c a l l i n g s e t B e f o r e A n d A f t e r P o i n t e r s ( ) r e s u l t s i n t h e node we ’ r e // c a l l i n g "A" t o be u p d a t e d so i t s "m_nextNode" p o i n t s t o " t h i s " // node , and t h e node we ’ r e c a l l i n g "B" i s u p d a t e d so i t s // "m_prevNode" p o i n t s t o " t h i s " node . " t h i s " node i t s e l f remains // unchanged . void s e t B e f o r e A n d A f t e r P o i n t e r s ( ) ; } ; SortedList: This class will be used to store a doubly-linked list in an always-sorted manner, such that the user does not specify where in the list a new value should be inserted, but rather the new value is inserted in the correct place to maintain a sorted order. The interface to the SortedList 3 should be exactly as follows: // The S o r t e d L i s t c l a s s d o e s not s t o r e any d a t a d i r e c t l y . I n s t e a d , i t // c o n t a i n s a c o l l e c t i o n o f LinkedNode o b j e c t s , each o f which c o n t a i n s // one e l e m e n t . c l as s S o r t e d L i s t { private : // P o i n t s t o t h e f i r s t node i n a l i s t , or n u l l p t r i f l i s t i s // empty . LinkedNode m_head ; // P o i n t s t o t h e l a s t node i n a l i s t , or n u l l p t r i f l i s t i s empty . LinkedNode m_tail ; public : // D e f a u l t C o n s t r u c t o r . I t w i l l p r o p e r l y i n i t i a l i z e a l i s t t o be // an empty l i s t , t o which v a l u e s can be added . S o r t e d L i s t ( ) ; // Copy c o n s t r u c t o r . I t w i l l make a c o m p l e t e ( deep ) copy o f t h e // l i s t , such t h a t one can be changed w i t h o u t a f f e c t i n g t h e o t h e r . S o r t e d L i s t ( const S o r t e d L i s t& s o u r c e ) ; // Copy a s s i g n m e n t o p e r a t o r . I t w i l l make a c o m p l e t e ( deep ) copy // o f t h e l i s t such t h a t one can be changed w i t h o u t a f f e c t i n g t h e // o t h e r . S o r t e d L i s t& operator=(const S o r t e d L i s t& r h s ) ; // Returns t h e number o f nodes c o n t a i n e d i n t h e l i s t . int getNumElems ( ) const ; // P r o v i d e s t h e v a l u e s t o r e d i n t h e node a t t h e i n d e x p r o v i d e d i n // t h e 0 b a s e d " i n d e x " parameter . I f t h e i n d e x i s o u t o f range , // t h e n t h e f u n c t i o n r e t u r n s a p a i r w i t h t h e f i r s t e l e m e n t s e t t o // f a l s e t o i n d i c a t e f a i l u r e , and t h e c o n t e n t s o f t h e second // e l e m e n t i s u n d e f i n e d . Otherwise , t h e f u n c t i o n r e t u r n s a p a i r // w i t h t h e f i r s t e l e m e n t s e t t o t r u e and t h e second e l e m e n t w i l l // c o n t a i n a copy o f t h e v a l u e a t p o s i t i o n " i n d e x " . p a i r getElemAtIndex ( int i n d e x ) const ; // A l l o w s t h e u s e r t o i n s e r t a v a l u e i n t o t h e l i s t . S i n c e t h i s i s // a s o r t e d l i s t , t h e r e i s no need t o s p e c i f y where i n t h e l i s t t o // i n s e r t t h e e l e m e n t . I t w i l l i n s e r t i t i n t h e a p p r o p r i a t e // l o c a t i o n b a s e d on t h e v a l u e b e i n g i n s e r t e d . I f t h e node v a l u e // b e i n g i n s e r t e d i s found t o be " e q u a l t o " one or more node // v a l u e s a l r e a d y i n t h e l i s t , t h e newly i n s e r t e d node w i l l be // p l a c e d AFTER t h e p r e v i o u s l y i n s e r t e d nodes . void i n s e r t V a l u e ( int v a l u e ) ; // Removes t h e f r o n t item from t h e l i s t . I f t h e l i s t was empty , // t h e f u n c t i o n r e t u r n s a p a i r w i t h t h e f i r s t e l e m e n t s e t t o f a l s e // t o i n d i c a t e f a i l u r e , and t h e c o n t e n t s o f t h e second e l e m e n t i s // u n d e f i n e d . I f t h e l i s t was not empty and t h e f i r s t item was 4 // s u c c e s s f u l l y removed , t h e f u n c t i o n r e t u r n s a p a i r w i t h t h e // f i r s t e l e m e n t s e t t o t r u e , and t h e second e l e m e n t w i l l be s e t // t o t h e v a l u e t h a t was removed . p a i r removeFront ( ) ; // Removes t h e b a c k item from t h e l i s t . I f t h e l i s t was empty , t h e // f u n c t i o n r e t u r n s a p a i r w i t h t h e f i r s t e l e m e n t s e t t o f a l s e t o // i n d i c a t e f a i l u r e , and t h e c o n t e n t s o f t h e second e l e m e n t i s // u n d e f i n e d . I f t h e l i s t was not empty and t h e l a s t item was // s u c c e s s f u l l y removed , t h e f u n c t i o n r e t u r n s a p a i r w i t h t h e // f i r s t e l e m e n t s e t t o t r u e , and t h e second e l e m e n t w i l l be s e t // t o t h e v a l u e t h a t was removed . p a i r removeBack ( ) ; // P r i n t s t h e c o n t e n t s o f t h e l i s t from head t o t a i l t o t h e // s c r e e n . Begins w i t h a l i n e r e a d i n g " Forward L i s t C o n t e n t s // F o l l o w : " , t h e n p r i n t s one l i s t e l e m e n t p e r l i n e , i n d e n t e d two // s p a c e s , t h e n p r i n t s t h e l i n e "End o f L i s t C o n t e n t s " t o i n d i c a t e // t h e end o f t h e l i s t . void p r i n t F o r w a r d ( ) const ; // P r i n t s t h e c o n t e n t s o f t h e l i s t from t a i l t o head t o t h e // s c r e e n . Begins w i t h a l i n e r e a d i n g " Backward L i s t C o n t e n t s // F o l l o w : " , t h e n p r i n t s one l i s t e l e m e n t p e r l i n e , i n d e n t e d two // s p a c e s , t h e n p r i n t s t h e l i n e "End o f L i s t C o n t e n t s " t o i n d i c a t e // t h e end o f t h e l i s t . void printBackward ( ) const ; // C l e a r s t h e l i s t t o an empty s t a t e w i t h o u t r e s u l t i n g i n any // memory l e a k s . void c l e a r ( ) ; // D e s t r u c t o r , which w i l l f r e e up a l l dynamic memory a s s o c i a t e d // w i t h t h i s l i s t when t h e l i s t i s d e s t r o y e d ( i . e . when a // s t a t i c a l l y a l l o c a t e d l i s t g o e s o u t o f s c o p e or a d y n a m i c a l l y // a l l o c a t e d l i s t i s d e l e t e d ) . ~ S o r t e d L i s t ( ) ; } ; Bonus (15 points). Modify the LinkedNode and SortedList classes from Problem 4 to be template classes such that the type of the stored value is defined by the template argument. E.g. SortedList could be used to store sorted double values. 5

Budget: $25.00

Due on: April 28, 2020 00:00

Posted: 7 months ago.

Answers (0)