%lsmagic %matplotlib inline import numpy as np import matplotlib.pyplot as plt %timeit np.linalg.eigvals(np.random.rand(100,100)) %%timeit a = np.random.rand(100, 100) np.linalg.eigvals(a) %%capture capt from __future__ import print_function import sys print('Hello stdout') print('and stderr', file=sys.stderr) capt.stdout, capt.stderr capt.show() %%writefile foo.py print('Hello world') %run foo %%script python import sys print 'hello from Python %s' % sys.version %%script python3 import sys print('hello from Python: %s' % sys.version) %%ruby puts "Hello from Ruby #{RUBY_VERSION}" %%bash echo "hello from $BASH" %%script ./lnum.py my first line my second more %%bash echo "hi, stdout" echo "hello, stderr" >&2 %%bash --out output --err error echo "hi, stdout" echo "hello, stderr" >&2 print(error) print(output) %%ruby --bg --out ruby_lines for n in 1...10 sleep 1 puts "line #{n}" STDOUT.flush end ruby_lines print(ruby_lines.read()) %load_ext cythonmagic %%cython_pyximport foo def f(x): return 4.0*x f(10) %%cython cimport cython from libc.math cimport exp, sqrt, pow, log, erf @cython.cdivision(True) cdef double std_norm_cdf(double x) nogil: return 0.5*(1+erf(x/sqrt(2.0))) @cython.cdivision(True) def black_scholes(double s, double k, double t, double v, double rf, double div, double cp): """Price an option using the Black-Scholes model. s : initial stock price k : strike price t : expiration time v : volatility rf : risk-free rate div : dividend cp : +1/-1 for call/put """ cdef double d1, d2, optprice with nogil: d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t)) d2 = d1 - v*sqrt(t) optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \ cp*k*exp(-rf*t)*std_norm_cdf(cp*d2) return optprice black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1) %timeit black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1) %%cython -lm from libc.math cimport sin print 'sin(1)=', sin(1) %load_ext rmagic %matplotlib inline import numpy as np import matplotlib.pyplot as plt X = np.array([0,1,2,3,4]) Y = np.array([3,5,4,6,7]) plt.scatter(X, Y) %Rpush X Y %R lm(Y~X)$coef %R resid(lm(Y~X)); coef(lm(X~Y)) b = %R a=resid(lm(Y~X)) %Rpull a print(a) assert id(b.data) == id(a.data) %R -o a from __future__ import print_function v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y) print('v1 is:', v1) v2 = %R mean(X)*mean(Y) print('v2 is:', v2) %%R -i X,Y -o XYcoef XYlm = lm(Y~X) XYcoef = coef(XYlm) print(summary(XYlm)) par(mfrow=c(2,2)) plot(XYlm) %load_ext octavemagic x = %octave [1 2; 3 4]; x a = [1, 2, 3] %octave_push a %octave a = a * 2; %octave_pull a a %%octave -i x -o y y = x + 3; y %%octave -f svg p = [12 -2.5 -8 -0.1 8]; x = 0:0.01:1; polyout(p, 'x') plot(x, polyval(p, x)); %%octave -s 500,500 # butterworth filter, order 2, cutoff pi/2 radians b = [0.292893218813452 0.585786437626905 0.292893218813452]; a = [1 0 0.171572875253810]; freqz(b, a, 32); %%octave -s 600,200 -f png subplot(121); [x, y] = meshgrid(0:0.1:3); r = sin(x - 0.5).^2 + cos(y - 0.5).^2; surf(x, y, r); subplot(122); sombrero()