### MF 803 Advanced Programming for Math Finance Practice Midterm Exam

Problem 1 (10 points)SQL Programming

Consider the following table design structure:

instrument info

instrument id (int)

ticker (char(5))

sector id (int)

shares outstanding (numeric)

instrument prices

instrument id (int)

ticker (char(5))

quote date (datetime)

close (numeric)

open (numeric)

low (numeric)

daily return (numeric)

Table names are in bold, columns are listed in each line and data types are in parentheses.

1.(3 Points) Write a query using only the instrument prices table that selects a time series of historical closing prices for a given ticker. Make sure the results are sorted from oldest date to newest.

2.(3 points) Write a query that selects the closing price and shares outstanding from the instrument info and instrument prices tables for a given ticker and quote date. Only return the data if the instrument has a price in the instrument prices table.

3.(3 points) Write a query that computes the average daily return for each sector for a given date.

4.(1 point) You are asked to remove one redundant column from the instrument prices table. Which column would you remove and why?

Problem 2 (10 points) Python Basic Concepts

1.(2.5 points) Consider the following code:

str2="programming" print(str2)

str=str1+str2 print(str)

test="gram"instr print(test) print(str)

str ="p" print(str)

What will be the result of the code? What will be printed to the console?

2.(2.5 points) Consider the following code that implements the Black-Scholes for- mula for a European call:

def callPx(s_0,k,r,sigma,tau):

sigma Rt T= (sigma*math.sqrt(tau))

rSigTerm= (r+sigma*sigma/ 2.0) *tau

d1= (math.log(s_0/k) +r Sig Term) /sigma Rt T d2=d1-sigma Rt T

term1=s_0*norm.cdf(d1) term2=k*math.exp(-r*tau) *norm.cdf(d2)

return term1-term2

What happens when a negative value is passed for s 0? How about a negative sigma? Add the proper exception handling to this function to handle these parameter values.

3.(2.5 points) You are given two DataFrames which contain historical price data for two different ETF’s. Unfortunately, the dates in the two data frames don’t match exactly. Write the Python code necessary to merge these two data frames, returning only dates where there are prices for both ETF’s.

4.(2.5 point) Suppose mat1 and mat2 are two Python numpy matrix objects, and arr1 and arr2 are numpy array objects.

If we same data is stored in mat1 and arr1, and mat2 and arr2, respectively, what will be the difference, if any, in behavior between multiplying mat1 * mat2 and multiplying arr1 * arr2?

Problem 3 (20 points) Object-Oriented Programming in Python

1.(10 points) Consider the following Python code:

Class BasePosition:

def __ init__(self,shrs,long):

self.shares=shrs self.is Long=long

print("callingBase Positionclassconstructor")

def __ del__(self):

print("callingBasePositionclassdestructor")

def printPos(self):

print("callingprint PosfromBase")

print(self.shares)

print(self.is Long)

class ChildPosition(BasePosition):

def __ init__(self,shrs,long,child Shrs):

self.childShares=childShrs

BasePosition.__ init__(self,shrs,long)

print("callingChild Positionclassconstructor")

def __del__(self):

print("callingChild Positionclassdestructor")

def printPos(self):

print("callingprint PosfromChild")

print(self.shares)

print(self.is Long)

print(self.child Shares)

base Pos 1=Base Position(100 , 1)

base Pos 2=Base Position(75 , 0)

child Pos 1=Child Position(25 , 0 , 5)

child Pos 2=Child Position(150 , 1 , 0)

What will be the output of this program?

2.(5 points) What are the del functions and at what point will they be called? 3.(5 points) What will be the result of the following line of code?

base Pos 3=base Pos 1+base Pos 2

What, if any changes would need to be made to the BasePosition class for this to work?

Problem 4 (10 points)C++ Basic Concepts

Consider the following piece of C++ code:

classFoo{

Foo(intbar_){ bar=bar_;

}

intbar;

};

intmain(intargc,constchar*argv[]) { Foof;

f.bar= 5; std::cout<<f.bar<<std::endl;

return0;

}

1.(4 points) Will this code compile? If not, fix any errors so that it will compile.

If it will compile, what will the output be?

2.(4 points) Consider the following set of functions added to the Foo class:

int calcFooBar1(intval,intmult){

bar=val*mult;

val=bar; returnbar;

}

int calcFooBar2(int&val,int&mult){

bar=val*mult;

val=bar; returnbar;

}

int calcFooBar3(int*val,int*mult){

bar= (*val) * (*mult);

val= &bar; returnbar;

}

(1 point) Which calcFooBar function takes a pointer?

(3 points) What will be the output of the following code?

Foof; intval= 1.0;

intmult=     5.0; intret=f.calc Foo Bar 1(val,mult);

std::cout<<val<<std::endl; std::cout<<mult<<std::endl; std::cout<<ret<<std::endl;

val= 1.0;

mult=         5.0; ret=f.calc Foo Bar 2(val,mult);

std::cout<<val<<std::endl; std::cout<<mult<<std::endl; std::cout<<ret<<std::endl;

val= 1.0;

mult=            5.0; ret=f.calc Foo Bar 3(&val, &mult);

std::cout<<val<<std::endl; std::cout<<mult<<std::endl; std::cout<<ret<<std::endl;

3.(2 point) Consider the function calcFooBar4:

int calcFooBar4(constint&val,constint&mult){ bar=val*mult;

val=bar; returnbar;

}

What will be the result of adding this function to the Foo class and calling it from the main function?

Problem 5 (20 points) Object-Oriented Programming in C++

Consider the following C++ code:

classFoo{

public:

Foo(){

std::cout<<"callingFooconstructor"<<std::endl;

}

virtual~Foo(){

std::cout<<"callingFoodestructor"<<std::endl;

}

intbar;

};

classFoo Kid:publicFoo{ public:

Foo Kid(){

std::cout<<"callingFoo Kidconstructor"<<std::endl;

}

virtual~Foo Kid(){ std::cout<<"callingFoo Kiddestructor"<<std::endl;

}

};

intmain(intargc,constchar*argv[]) { Foo*f(newFoo());

Foof2(*f);

Foo Kid*fk;

std::cout<< (f== &f2) <<std::endl; deletef;

std::shared_ ptr<Foo>f3; fk=newFoo    Kid(); f3=std::shared_ ptr<Foo>(newFoo());

return0;

}

1.(10 points) What will be the output of the above code?

2.(2.5 points) Consider adding a function func() to the definition of Foo. What would be the significance of making the function virtual? In what cases would this be useful?

3.(2.5 points) When specifying that FooKid inherits from Foo, what is the signif- icance of the public keyword? What would happen if instead this line read:

1

4.(2.5 points) Are there any dangling pointers created in the above code? If so, what code would you change to fix this?

5.(2.5 points) Suppose the following function definition was added to the definition of Foo:

1

What type of function is this? What impact would adding this have?

Problem 6 (20 points)Simulation Algorithm

Consider the Black-Scholes SDE:

dSt = µStdt + σStdWt

You are asked to use this SDE to write a simulation algorithm to price an American upside one touch option. Recall that the payoff of an American one touch is defined as:

c0 = E˜ Σe−rT 1M>K^Σ

where MT is the maximum value of the asset over the period.

1.(2.5 points) List the set of parameters that you will have in your algorithm and describe their meaning.

2.(7 Points) Write a piece of pseudo-code that you can use to simulate from the given stochastic process.

3.(7 Points) Write a piece of pseudo-code that will define the payoff function for your exotic option.

4.(3.5 points) Describe three unit tests that you would create to ensure that your model prices are correct.

Problem 7 (10 points)Algorithms & Finance Applications

1.(2.5 points) What is the singleton pattern? When should it be used?

2.(2.5 points) Using pseudocode, write a brief sketch of the code needed to imple- ment the singleton pattern.

3.(2.5 points) Estimate the sign and rough magnitude of the correlation between the S&P 500 and its implied volatility.

4.(2.5 points) Suppose we buy a straddle on the S&P and delta-hedge it continuously. What other factors will determine our profit or loss on the trade?