%pylab inline
import sympy as sp
import fmt
Populating the interactive namespace from numpy and matplotlib
I have read and understood the class policy, especially the section about cheating. Please type your full names below to acknowledge:
Set up your Python and IPython notebook environment and show case the following IPython functionalities:
from platform import python_version
python_version()
'3.6.7'
%pdb on
def div(a, b) :
return a/b
def bug(a, b) :
return div(a, b)
bug(3, 0)
Automatic pdb calling has been turned ON
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-2-eb1c28a85a66> in <module>() 7 return div(a, b) 8 ----> 9 bug(3, 0) <ipython-input-2-eb1c28a85a66> in bug(a, b) 5 6 def bug(a, b) : ----> 7 return div(a, b) 8 9 bug(3, 0) <ipython-input-2-eb1c28a85a66> in div(a, b) 2 3 def div(a, b) : ----> 4 return a/b 5 6 def bug(a, b) : ZeroDivisionError: division by zero
> <ipython-input-2-eb1c28a85a66>(4)div() 2 3 def div(a, b) : ----> 4 return a/b 5 6 def bug(a, b) : ipdb> q
For the first term of the conditional variance expression of the CIR process discused in the class:
$$f(\kappa) = \frac{r_0\sigma^2}{\kappa}(e^{-\kappa t} - e^{-2\kappa t})$$Implement a matrix product function using hand coded loops in python, please feel free to use numpy array and numpy vector multiplications, but obviously you should not just call numpy matrix multiplication.
Compare the speed of your implementation to the numpy matrix product version, plot the difference in computational time with the size of the matrix.
Hint:
a.dot(b)
, for matrix type, the matrix production is a*b
. We recommend you to stick with array type to avoid any potential confusions. The a*b
on array type is element wise multiplication. A common mistake is to use a*b
for matrix production on array type. Please see the following code section as an example.a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = a + 1
print("a=\n", a)
print("\nb=\n", b)
c = a.dot(b) # matrix product
d = a*b #elment wise multiplication
print("\nc=a.dot(b)\n", c)
print("\nd=a*b\n", d)
a= [[1 2 3] [4 5 6] [7 8 9]] b= [[ 2 3 4] [ 5 6 7] [ 8 9 10]] c=a.dot(b) [[ 36 42 48] [ 81 96 111] [126 150 174]] d=a*b [[ 2 6 12] [20 30 42] [56 72 90]]
Hint:
a = np.float32(1.)
b = np.float64(1.)
print(type(a), type(b))
<class 'numpy.float32'> <class 'numpy.float64'>