In [10]:
import numpy as np
import pandas as pd
from numpy import sqrt
from random import uniform
from time import time
import multiprocessing
import matplotlib.pyplot as plt
%matplotlib
number_of_darts = 10**np.arange(2,6)
number_of_darts.tolist()


Using matplotlib backend: MacOSX
Out[10]:
[100, 1000, 10000, 100000]

In [15]:
def pi_approx_serial(number_of_darts):
    ''' approximate pi using serial method'''
    number_of_darts_in_circle = 0
    start_time = time()
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = np.sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = 4*number_of_darts_in_circle/float(number_of_darts)
    return [execution_time, pi_apx]

time_serial = []
for dart in number_of_darts:
    time_temp = pi_approx_serial(dart)
    time_temp.insert(0, dart)
    time_temp.append(time_temp[1]/dart)
    time_serial.append(time_temp)

time_serial


Out[15]:
[[100, 0.0022699832916259766, 3.28, 2.2699832916259764e-05],
 [1000, 0.016113996505737305, 3.2, 1.6113996505737305e-05],
 [10000, 0.11764311790466309, 3.17, 1.1764311790466309e-05],
 [100000, 1.0162169933319092, 3.1458, 1.0162169933319092e-05]]

In [14]:
time_temp


Out[14]:
[100000, 1.0499749183654785, 3.13824]

In [2]:
def pi_approx(number_of_darts):
    ''' approximate pi using MC method'''
    number_of_darts_in_circle = 0
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    return 4*number_of_darts_in_circle/float(number_of_darts)

In [50]:
from IPython import parallel
rc = parallel.Client()
dview = rc[:]
with dview.sync_imports():
    from numpy import sqrt
    from random import uniform
    from time import time
    
def pi_approx_par(number_of_darts):
    ''' approximate pi using parallel method '''
    number_of_darts_per_thread = number_of_darts/len(dview)
    start_time = time()
    d_result = dview.apply_async(pi_approx, number_of_darts_per_thread)
    result = d_result.get()
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = sum(result)/float(len(result))
    return [execution_time, pi_apx]
    

time_Ipar = []
for darts in number_of_darts:
    time_temp = pi_approx_par(darts)
    time_temp.insert(0, darts)
    time_temp.append(time_temp[1]/darts)
    time_Ipar.append(time_temp)

time_Ipar


importing sqrt from numpy on engine(s)
importing uniform from random on engine(s)
importing time from time on engine(s)
Out[50]:
[[100, 0.02384805679321289, 3.08, 0.0002384805679321289],
 [1000, 0.024895906448364258, 3.144, 2.4895906448364259e-05],
 [10000, 0.12739801406860352, 3.1567999999999996, 1.2739801406860352e-05],
 [100000, 0.6547918319702148, 3.1418, 6.5479183197021485e-06]]

In [49]:
def pi_approx_stdlib(number_of_darts):
    if __name__ == '__main__':
        nCPU = multiprocessing.cpu_count()
        pool = multiprocessing.Pool(processes=nCPU)
        start_time = time()
        number_of_darts_per_thread = number_of_darts/nCPU
        argu_list = [number_of_darts_per_thread]* nCPU
        pi_apx = pool.map_async(pi_approx, argu_list).get()
        pi_apx = np.mean(pi_apx)
        end_time = time()
        print pi_apx
        execution_time = end_time-start_time
        pool.terminate()
        return [execution_time, pi_apx]
time_stdlib = []
for dart in number_of_darts:
    time_temp = pi_approx_stdlib(dart)
    time_temp.insert(0, dart)
    time_temp.append(time_temp[1]/dart)
    time_stdlib.append(time_temp)

time_stdlib


3.32
3.152
3.1292
3.13724
Out[49]:
[[100, 0.004316091537475586, 3.3200000000000003, 4.316091537475586e-05],
 [1000, 0.015408039093017578, 3.1520000000000001, 1.5408039093017579e-05],
 [10000, 0.06084895133972168, 3.1292, 6.0848951339721677e-06],
 [100000, 0.573638916015625, 3.1372399999999998, 5.7363891601562499e-06]]

In [30]:
def to_DataFrame(data_list):
    '''
        convert list to pandas DataFrame
        time is in second
        the unit of simu_rate is [# per us]
    
    '''
    data_df = pd.DataFrame(data_list, columns=['nDarts', 'time', 'pi_approx', 'simu_rate'])
    data_df.set_index('nDarts', inplace=True)
    data_df['simu_rate'] *= 1e6
    return data_df

In [34]:
f0, ax0 = plt.subplots()
ax0.loglog(time_serial[:,0], time_serial[:,1], label='serial')
ax0.loglog(time_Ipar[:,0], time_Ipar[:,1], label='Ipython')
ax0.loglog(time_stdlib[:,0],time_stdlib[:,1], label='StdLib')

ax0.legend()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-0705e17833aa> in <module>()
      1 f0, ax0 = plt.subplots()
----> 2 ax0.loglog(time_serial[:,0], time_serial[:,1], label='serial')
      3 ax0.loglog(time_Ipar[:,0], time_Ipar[:,1], label='Ipython')
      4 ax0.loglog(time_stdlib[:,0],time_stdlib[:,1], label='StdLib')
      5 

TypeError: list indices must be integers, not tuple

In [51]:
serial_df = to_DataFrame(time_serial)
Ipar_df = to_DataFrame(time_Ipar)
stdlib_df = to_DataFrame(time_stdlib)

In [52]:
serial_df


Out[52]:
time pi_approx simu_rate
nDarts
100 0.002270 3.2800 22.699833
1000 0.016114 3.2000 16.113997
10000 0.117643 3.1700 11.764312
100000 1.016217 3.1458 10.162170

In [53]:
Ipar_df


Out[53]:
time pi_approx simu_rate
nDarts
100 0.023848 3.0800 238.480568
1000 0.024896 3.1440 24.895906
10000 0.127398 3.1568 12.739801
100000 0.654792 3.1418 6.547918

In [35]:
number_of_darts


Out[35]:
array([   100,   1000,  10000, 100000])

In [36]:
darts


Out[36]:
100000

In [47]:
[darts]*3


Out[47]:
[100000, 100000, 100000]

In [43]:
a = darts.tolist()

In [42]:
darts


Out[42]:
100000

In [45]:
a*3


Out[45]:
300000

In [60]:
ts = Series(np.random.randn(1000), index=date_range('1/1/2000', periods=1000))

In [71]:
from pandas import Series, DataFrame

In [59]:
from pandas import date_range

In [62]:
ts = ts.cumsum()

In [64]:
ts.plot()


Out[64]:
<matplotlib.axes.AxesSubplot at 0x10a6b0710>

In [66]:
plt.figure(); ax = ts.plot(style='k--', label='Series'); plt.legend()


Out[66]:
<matplotlib.legend.Legend at 0x10c223d50>

In [70]:
ax.set_title('aa')


Out[70]:
<matplotlib.text.Text at 0x10c2356d0>

In [73]:
In [6]: df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))

In [75]:
df.head()


Out[75]:
A B C D
2000-01-01 -1.067597 -0.015046 0.265412 0.195163
2000-01-02 0.242401 -0.292326 -0.123619 -1.941828
2000-01-03 -0.793515 1.360787 -0.321307 1.563963
2000-01-04 0.403138 -0.757833 -1.722811 0.055350
2000-01-05 0.832704 0.658673 -2.537781 -0.023283

In [83]:
axA = df.A.plot()

In [79]:
import sys

In [84]:
axA.set_title('aa')
sys.stdout.flush()

In [86]:
Ipar_df
serial_df
stdlib_df


Out[86]:
time pi_approx simu_rate
nDarts
100 0.004316 3.32000 43.160915
1000 0.015408 3.15200 15.408039
10000 0.060849 3.12920 6.084895
100000 0.573639 3.13724 5.736389

In [7]:
import numpy as np
import pandas as pd
from numpy import sqrt
from random import uniform
from time import time
import multiprocessing
import matplotlib.pyplot as plt
#%matplotlib
number_of_darts = 10**np.arange(2,6)
number_of_darts.tolist()

def pi_approx_serial(number_of_darts):
    ''' approximate pi using serial method'''
    number_of_darts_in_circle = 0
    start_time = time()
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = np.sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = 4*number_of_darts_in_circle/float(number_of_darts)
    return [execution_time, pi_apx]
time_serial = []
for dart in number_of_darts:
    time_temp = pi_approx_serial(dart)
    time_temp.insert(0, dart)
    time_temp.append(dart/time_temp[1])
    time_serial.append(time_temp)


def pi_approx(number_of_darts):
    ''' approximate pi using MC method'''
    number_of_darts_in_circle = 0
    for n in xrange(number_of_darts):
        x = uniform(0,1)
        y = uniform(0,1)
        distance = sqrt((x-0.5)**2+(y-0.5)**2)
        if distance <= 0.5:
            number_of_darts_in_circle += 1
        else:
            pass
    return 4*number_of_darts_in_circle/float(number_of_darts)

from IPython import parallel
rc = parallel.Client()
dview = rc[:]
with dview.sync_imports():
    from numpy import sqrt
    from random import uniform
    from time import time

def pi_approx_par(number_of_darts):
    ''' approximate pi using parallel method '''
    number_of_darts_per_thread = number_of_darts/len(dview)
    start_time = time()
    d_result = dview.apply_async(pi_approx, number_of_darts_per_thread)
    result = d_result.get()
    end_time = time()
    execution_time = end_time - start_time
    pi_apx = np.mean(result)
    return [execution_time, pi_apx]
time_Ipar = []
for dart in number_of_darts:
    time_temp = pi_approx_par(dart)
    time_temp.insert(0, dart)
    time_temp.append(dart/time_temp[1])
    time_Ipar.append(time_temp)


def pi_approx_stdlib(number_of_darts):
    if __name__ == '__main__':
        nCPU = multiprocessing.cpu_count()
        pool = multiprocessing.Pool(processes=nCPU)
        start_time = time()
        number_of_darts_per_thread = number_of_darts/nCPU
        argu_list = [number_of_darts_per_thread] * nCPU
        pi_apx = pool.map_async(pi_approx, argu_list).get()
        pi_apx = np.mean(pi_apx)
        end_time = time()
        execution_time = end_time-start_time
        pool.terminate()
        return [execution_time, pi_apx]
time_stdlib = []
for dart in number_of_darts:
    time_temp = pi_approx_stdlib(dart)
    time_temp.insert(0, dart)
    time_temp.append(dart/time_temp[1])
    time_stdlib.append(time_temp)

def to_DataFrame(data_list):
    '''
        convert list to pandas DataFrame
        time is in second
        the unit of simu_rate is [# per us]
    '''
    data_df = pd.DataFrame(data_list, columns=['nDarts', 'time', 'pi_approx', 'simu_rate'])
    data_df.set_index('nDarts', inplace=True)
    return data_df

serial_df = to_DataFrame(time_serial)
Ipar_df = to_DataFrame(time_Ipar)
stdlib_df = to_DataFrame(time_stdlib)

fig, axes = plt.subplots()
serial_df.time.plot(style='r-', label='Simple')
serial_df.simu_rate.plot(secondary_y=True, style='r--')
Ipar_df.time.plot(style='g-', label='Ipcluster')
Ipar_df.simu_rate.plot(secondary_y=True, style='g--')
stdlib_df.time.plot(style='c-', label='Multiprocessing')
stdlib_df.simu_rate.plot(secondary_y=True, style='c--')
axes.set_xlabel('Darts Thrown')
axes.set_ylabel('Execution Time(seconds), solid line')
axes.set_xscale('log')
axes.set_yscale('log')
axes.right_ax.set_yscale('log')
#axes.right_ax.set_ylabel('Simulation Rate (darts/seconds), dashed line')
axes.legend()
axes.set_title('MacBook Pro with 2.4GHZ Intel Core 2 Duo', size='large')
plt.show()


importing sqrt from numpy on engine(s)
importing uniform from random on engine(s)
importing time from time on engine(s)

In [14]:
np.logspace(1,7,10)


Out[14]:
array([  1.00000000e+01,   4.64158883e+01,   2.15443469e+02,
         1.00000000e+03,   4.64158883e+03,   2.15443469e+04,
         1.00000000e+05,   4.64158883e+05,   2.15443469e+06,
         1.00000000e+07])

In [16]:
dar


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-109731c61bc3> in <module>()
----> 1 np.int([_])

TypeError: int() argument must be a string or a number, not 'list'

In [ ]: