Solutions for Homework sheet 5

Data Structures

(i) Numpy Arrays

a. Create a numpy array of length 5 with all elements set to 0.

In [1]:
import numpy as np
A=np.zeros(5)
print A
[ 0.  0.  0.  0.  0.]

b. Create a 2 dimensional numpy array of length 4 × 4 with all elements set to 1.

In [2]:
A=np.ones((4,4))
print A
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]

c1. Create a 2 dimensional numpy array of length 5 × 5 with all elements set to 10.

In [3]:
A=np.full((5,5),10)
print A
[[ 10.  10.  10.  10.  10.]
 [ 10.  10.  10.  10.  10.]
 [ 10.  10.  10.  10.  10.]
 [ 10.  10.  10.  10.  10.]
 [ 10.  10.  10.  10.  10.]]

c2. Create two different numpy arrays with elements [1, 3, 8] and with elements from 1 to 100 divided into 10 evenly spaced numbers.

In [4]:
A=np.asarray([1,3,8])
print A

B=np.linspace(1,100,10)
print B
[1 3 8]
[   1.   12.   23.   34.   45.   56.   67.   78.   89.  100.]

d. Create a 2 dimensional numpy array filled with random values. Find their datatypes.

In [5]:
A=np.random.rand(4,4)
A.dtype
Out[5]:
dtype('float64')

e. Let say we define an array X1 as follows:


import numpy as np
X1 = np.eye(2)

Find the dimension and type of array X1.

In [6]:
import numpy as np
X1 = np.eye(2)
print X1.shape
(2, 2)

f. Let say we define an array X1 as follows

import numpy as np
X1 = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

Print the first row and column of the array X1, also print the last element of 3rd row.

In [7]:
import numpy as np
X1 = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) 
print X1[0,:]
print X1[:,0]
print X1[2,3]
[1 2 3 4]
[1 5 9]
12

g. Let say we define an array X1 as follows

import numpy as np
X1 = np. array ([[10 ,12 ,13 ,14] , [5 ,6 ,7 ,8] , [19 ,10 ,11 ,12]])


Obtain a new array X2, which has same elements as X1 except elements smaller than 10 are set equal to 0.

In [8]:
import numpy as np
X1 = np.array ([[10 ,12 ,13 ,14] , [5 ,6 ,7 ,8] , [19 ,10 ,11 ,12]])
X2=X1
X2[X2<10]=0
print X2
[[10 12 13 14]
 [ 0  0  0  0]
 [19 10 11 12]]

h. Define two 2 × 3 numpy arrays a and b. The first row of a has elements [1, 2, 4] and the second row has the elements [4,5,6]. The first row of b has elements [2, 2, 2] and second row has elements [5, 5, 6]. First take the transpose of a and b then carry out an element wise multiplication on them.

In [9]:
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[2,2,2],[5,5,6]])
print a.T*b.T
[[ 2 20]
 [ 4 25]
 [ 6 36]]
(ii) Lists

a. Define a python list with elements [9,10,11,12] and find out the length of this list.

In [10]:
L=[9,10,11,12]
print len(L)        
4

b. Define a python list with elements [60.1, 50, 80, 10, 12.5] , sort this list in increasing order.

In [11]:
L=[60.1, 50, 80, 10, 12.5]
L.sort()
print L
[10, 12.5, 50, 60.1, 80]

Define a python list with elements [’Tom’,’John’,’Jack’,’Kate’], remove ’Tom’ from this list and add ’Dan’ to this list. Next write the resulting list in reverse order.

In [12]:
L=['Tom','John','Jack','Kate']
L.pop(0)
print L
L.append('Dan')
print L
['John', 'Jack', 'Kate']
['John', 'Jack', 'Kate', 'Dan']

d. Define a 3 × 3 matrix in form of a python list, all the elements of the first row should be 1, all the elements of the second row should be 2 and all the elements of the third row should be 3.

In [13]:
M=[[1, 2, 3, 4],[5, 6, 7, 8], [9, 10, 11, 12]]
print M
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
(iii) Dictionary

a. Create a python dictionary with keys ’Name’, ’Course’, and ’Year’ and associated values as ’Nancy’, ’Chem01’ and ’2017’.

b. Print the keys and values of the dictionary created above.

In [14]:
D = {'Name': 'Nancy', 'Course': 'Chem01', 'Year': 2017}
print D.keys()  
print D.values()
['Course', 'Name', 'Year']
['Chem01', 'Nancy', 2017]

Control Statements

a. Using if statement write a code in python which calculates square root of an integer if it is positive.

In [16]:
x=input('Input a positive integer')
if(x<0):
    print 'Negative Integer'
else: 
    print 'Square root of the given integer=', np.sqrt(x)
Input a positive integer2
Square root of the given integer= 1.41421356237

b. Using if else statement write a code in python which checks whether a given username and password is correct.

In [17]:
uppair_dict={'Username':'math50','Password':'123xyz'}

User=raw_input('Input username:')

import getpass
Pass=getpass.getpass('Input password:')

if(User==uppair_dict['Username'] and Pass==uppair_dict['Password']):
    print 'Congratulations, your username and password are correct'
else: 
    print 'Username/Password is not correct'
Input username:math50
Input password:········
Congratulations, your username and password are correct

c. Use for loop to produce the following output from python code.

5, 10, 15, 25, Loop finished

In [18]:
for x in range(5,25,5):
    print x,',',
print 'Loop finished'
5 , 10 , 15 , 20 , Loop finished

d. Use a for loop to print all the elements from the list [’Jack’,’Jill’,’Jamie’,’John’].

In [19]:
L= ['Jack','Jill','Jamie','John']
for name in L:
    print name,
Jack Jill Jamie John

e. Use a for loop to add all the elements from the numpy array [4, 1.9, 3.1, 7, 2.6, 5.3, 3, 4]

In [20]:
L=[4, 1.9, 3.1, 7, 2.6, 5.3, 3, 4]
s=0
for x in L:
   s=s+x
print s
    
30.9

f. Use while statement to write a code which produces the following output

Your guess for the given integer : 100 Wrong! Lower than 100.

Your guess for the given integer : 45 Wrong! Higher than 45.

Your guess for the given integer : 50 Correct! you guessed it.

The while loop is over.

Done

In [21]:
running=True
X0=50

while(running):
    X=input('Guess  the integer')
    if(X>X0):
        print 'Your guess for the given integer :', X, 'Wrong! Lower than', X  
    elif(X<X0):
         print 'Your guess for the given integer :', X, 'Wrong! Higher than', X
    elif(X==X0):
        print 'Your guess for the given integer :', X, 'Correct! Higher than', X
        running=False
        print 'The while loop is over'
print 'Done'
Guess  the integer100
Your guess for the given integer : 100 Wrong! Lower than 100
Guess  the integer45
Your guess for the given integer : 45 Wrong! Higher than 45
Guess  the integer50
Your guess for the given integer : 50 Correct! Higher than 50
The while loop is over
Done

g. Use numpy arrays and for loops to solve the following problem from the course:

Consider a normal error regression model: $Y_i=\beta_0+\beta_1X_i+ \epsilon_i $. Where $\epsilon_i$ are independent and distributed as $N(0,10)$. $\beta_0=0.10$, $\beta_1=2.15$, $i=1,...,5$ and in these $5$ trials $X_i$ takes the values $\{5,10,15,20,25\}$. You are given that for every trial $i$ there are $k$ different observations of $Y_i$. Plot a scatter plot for $k=10$ between $X$ and $Y$.

In [22]:
X0=np.arange(5,30,5)
k=10
Y1=np.zeros(5*k)
X1=np.zeros(5*k)

labels=[];

l=0
for i in range(0,5):
    for j in range(0,k): 
        epsilon_1=np.random.normal(0,1,1)
        Y1[l] =0.10+2.15*X0[i]+epsilon_1[0]
        X1[l]=X0[i]
        l=l+1
     
    
import matplotlib.pylab as plt 
%matplotlib inline 

plt.figure(figsize=(15,7)) 
plt.subplot(1,2,1)
plt.scatter(X1,Y1,c='r',s=80)
plt.xlabel(r'$X$',fontsize=25)
plt.ylabel(r'$Y$',fontsize=25)    
plt.xlim(3,28);  
plt.ylim(4,60)
Out[22]:
(4, 60)
3. Basic Statistics

(i) Let random variable $X$ assumes outcomes $x_1,.....,x_n$. If the probability of outcomes is given by the probability function ${f(x_i)= P(X=x_i)}$ then what will be the expected value of $X$ i.e., $E\{X\}$? What will be the variance $\sigma^2\{ X\}$? If $a$ and $c$ are constants then what will be $E\{a+cX\}$ and $\sigma^2\{a+cX\}$?

$$E\{X\}={\sum_{i=1}^{n}x_if(x_i) }$$
$$ \sigma^2\{X\}= E\{(X-E\{X\})^2\}$$
$$E\{a+cX\}=a+cE\{X\} $$
$$\sigma^2\{a+cX\}=c^2\sigma^2\{X\} $$

(ii) The 90th percentile of a normal distribution is how many standard deviations above the mean?

In [23]:
import scipy.stats as st
format(st.norm.ppf(0.9),'.4f')
Out[23]:
'1.2816'

The 90th percentile is 1.28 standard deviations above the mean.

(iii) State the properties of independent and identically distributed (i.i.d.) random variables.

Independent identically distributed random variables are mutually independent (any given pair of variables has a covariance of zero and a coefficient of correlation of zero), randomly selected, and each have the same probability distribution function.

(iv) Write down the expression for the probability density function (pdf) of a normal random variable X.

The density function for a normal random varaible X

$$f(X)=\frac{1}{\sigma\sqrt{2\pi}}\exp\bigg[ \frac{1}{2} \bigg( \frac{X-\mu}{\sigma} \bigg)^2 \bigg] $$

for $$- \infty < X< \infty$$

Where $\mu$ is the mean and $\sigma^2$ is the variance

(v) Explain cumulative distribution function (cdf) with the help of a mathematical expression and sketch.

  • The cumulative distribution function (cdf) of a discrete random variable $X$ is defined as:
$$F(x)=P(X\leq x) =\sum_{t\leq x} xf(t) $$
  • The cumulative distribution function (cdf) $F(x)$ for a continuous random variable $X$ with probability density function $f$ is defined by:
$$F(x)=P(X\leq x) = \int_{-\infty}^{x} f(t)dt$$

for $- \infty < x< \infty$.