Lesson 17: Monte Carlo Simulation

============

One win result

==================

Win

============

One lose result

====================

Lose

==============

Code

=====================

#! usr/bin/env python2.7
import random
p=0
money=100
for p in range(0,1000):
p+=1
i=random.randint(2,12)
if i<8:
money-=1
print ‘At roll No.’,p,’you =lose=.Left money’,money
else:
money+=1
print ‘At roll No.’,p,’you =win=! Left money’,money
if money==0:
print ‘You lose all money at roll No.’,p
break
print ‘=============================’
print ‘After 1000 rall,money=’,money
print ‘=============================’

Lesson 16: Multiple Regression with Python

1063593118

======================

Code

============================

#! usr/bin/env python2.7

# ———–Read x and y————
x1=[]
x2=[]
x3=[]
y=[]

import csv
with open(‘price.csv’) as csvfile:
reader=csv.reader(csvfile)
for row in reader:
xy=row
x1.append(int(xy[0]))
x2.append(int(xy[1]))
x3.append(int(xy[2]))
y.append(int(xy[3]))
x=[x1,x2,x3]

print ‘=======================================’
print ‘Define X1=Bedrooms X2=Bathrooms X3=SqFt’
print ‘=======================================’
print

#————-Calculate Multiple Regression Result————
import numpy as np
import statsmodels.api as sm

def reg_m(y, x):
x=np.array(x).T
X=sm.add_constant(x)
results=sm.OLS(endog=y,exog=X).fit()
return results

print reg_m(y,x).summary()

Lesson 15:Python Basics, GDP-Fertility Regression and Scatter Plots

I deal 3 csv in the one program.
lesson15.1 lesson15.2

=============================================

Code

=============================================

#!/usr/bin/env python2.7

area=[‘Americas.csv’,’Asia Pacific.csv’,’EMEA.csv’]
i=0
import csv
import scipy.stats as stats
for i in range(0,3):
print
print ‘***Data from’,area[i],’***’
print
ID=[]
GDP=[]
FertRate=[]
nn=0
with open(area[i],’rb’) as datafile:
reader=csv.reader(datafile)
for row in reader:
nn+=1
temp=row
ID.append(int(temp[0]))
GDP.append(float(temp[1]))
FertRate.append(float(temp[2]))
print ‘——————————‘
print ‘ID=’,ID
print ‘——————————‘
print ‘GDP=’,GDP
print ‘——————————‘
print ‘Fertility Rate=’,FertRate
print ‘——————————‘
slope,intercept,rvalue,pvalue,stderr=stats.linregress(GDP,FertRate)
print ‘@@For’,area[i],’@@’
print ‘Slope=’,slope
print ‘Intercept=’,intercept
print ‘R squar=’,rvalue*rvalue
print ‘N=’,nn
print
print ‘####################################################################################’
print ‘####################################################################################’

Lesson 14:Writing Python Programs

Result

12.2

12.4

12.6

12.8

=================================================================================

Code

=================================================================================

12.2

——————————————————————————————-

#!/usr/bin/env python2.7
x=[]
y=[]
file=’12.2.csv’

import csv
with open(file,’rb’) as cf:
reader=csv.reader(cf)
for row in reader:
xy=row
x.append(int(xy[0]))
y.append(int(xy[1]))
print ‘Read from’,file
print ‘x=’,x
print ‘y=’,y

import scipy.stats as s

slope,intercept,rvalue,pvalue,stderr=s.linregress(x,y)
print
print ‘—-lineregress———-‘
print
print ‘Y =’,slope,’* X +’,intercept
print
print ‘————————-‘
print
print ‘== Produced by Jiajian Wu ==’

————————————————————————————-

12.4

————————————————————————————–

#!/usr/bin/env python2.7
x=[]
y=[]
file=’12.4.csv’

import csv
with open(file,’rb’) as cf:
reader=csv.reader(cf)
for row in reader:
xy=row
x.append(int(xy[0]))
y.append(int(xy[1]))
print ‘Read from’,file
print ‘x=’,x
print ‘y=’,y

import scipy.stats as s

slope,intercept,rvalue,pvalue,stderr=s.linregress(x,y)
print
print ‘—-lineregress———-‘
print
print ‘Y =’,slope,’* X +’,intercept
print
print ‘————————-‘
print
print ‘== Produced by Jiajian Wu ==’

————————————————————————————-

12.6

————————————————————————————–

#!/usr/bin/env python2.7
x=[]
y=[]
file=’12.6.csv’

import csv
with open(file,’rb’) as cf:
reader=csv.reader(cf)
for row in reader:
xy=row
x.append(int(xy[0]))
y.append(int(xy[1]))
print ‘Read from’,file
print ‘x=’,x
print ‘y=’,y

import scipy.stats as s

slope,intercept,rvalue,pvalue,stderr=s.linregress(x,y)
print
print ‘—-lineregress———-‘
print
print ‘Y =’,slope,’* X +’,intercept
print
print ‘————————-‘
print
print ‘== Produced by Jiajian Wu ==’

————————————————————————————-

12.8

————————————————————————————–

#!/usr/bin/env python2.7
x=[]
y=[]
file=’12.8.csv’

import csv
with open(file,’rb’) as cf:
reader=csv.reader(cf)
for row in reader:
xy=row
x.append(float(xy[0]))
y.append(float(xy[1]))
print ‘Read from’,file
print ‘x=’,x
print ‘y=’,y

import scipy.stats as s

slope,intercept,rvalue,pvalue,stderr=s.linregress(x,y)
print
print ‘—-lineregress———-‘
print
print ‘Y =’,slope,’* X’,intercept
print
print ‘————————-‘
print
print ‘== Produced by Jiajian Wu ==’

=============================================================

CSV

=============================================================

12.2

—————————————————————————–

158,349
296,510
87,301
110,322
436,550

—————————————————————————–

12.4

—————————————————————————–

1425,277
273,100
915,120
1687,259
234,40
142,25
259,57
258,31
894,141

—————————————————————————

12.6

—————————————————————————

12,17
21,15
28,22
8,19
20,24

—————————————————————————

12.8

—————————————————————————

12.5,148
3.7,55
21.6,338
60,994
37.6,541
6.1,89
16.8,126
41.2,379