POP77001 Computer Programming for Social Scientists
Python programs can be decomposed into modules, statements, expressions, and objects, as follows:
\[ \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon} \]
object.attribute
Find the first occurrence of attribute by looking in
object, then in all classes above it.
Extra
from datetime import date
class Tamagotchi:
"""Class modelling a simple Tamagotchi toy"""
def __init__(self, name, birthdate = date.today()):
"""Creates a new Tamagotchi and gives it a name"""
self.name = name
self.birthdate = birthdate
self.food = 0
def __str__(self):
"""Returns a string representation of Tamagotchi"""
return (self.name + ' - ' +
'Age: ' + str(self.get_age().days) + ' days ' +
'Food: ' + str(self.food))
def get_age(self):
"""Get Tamagotchi's name in days"""
return date.today() - self.birthdate
def feed(self):
"""Give Tamagotchi some food"""
self.food += 1
def play(self):
"""Play with Tamagotchi"""
self.food -= 1Tamagotchiname - name given as stringbirthdate - birth date expressed as datetime.datefood - food level expressed as integer__init__() - constructor, called when an object of this class is created.__str__() - called when an object of this class is printed (with print() or str())get_age() - retrieve age expressed as datetime.timedeltafeed() - increment food level by 1play() - decrement food level by 1Tamagotchi is a class.Kuchipatchi, Mimitchi, Sebiretchi are instances of the class Tamagotchi.Tamagotchi.str is a class and 'watermelon' is an object of type str.object.method() as opposed to function(object).__).__init__() - defines object instantiation;__str__() - defines how an object is printed out;__add__() - overloads the + operator
__sub__() for -, __mul__() for *, etc.__eq__() - overloads the == operator
__lt__() for <, __ge__() for >=, etc.__len__() - returns the length of the object (is called by len() function)__iter__() - returns an iterator (used in loops)Extra
selfdef __init__(self, name):
self.name = name
sorted() function can sort objects of different types.__lt__() (less than) method.class Tamagotchi:
"""Class modelling a simple Tamagotchi toy"""
def __init__(self, name, birthdate = date.today()):
"""Creates a new Tamagotchi and gives it a name"""
self.name = name
self.birthdate = birthdate
self.food = 0
def __lt__(self, other):
"""Returns True if self's name precedes other's name alphabetically"""
return self.name < other.name
def __str__(self):
"""Returns a string representation of Tamagotchi"""
return (self.name + ' - ' +
'Age: ' + str(self.get_age().days) + ' days ' +
'Food: ' + str(self.food))
def get_age(self):
"""Get Tamagotchi's name in days"""
return date.today() - self.birthdate
def feed(self):
"""Give Tamagotchi some food"""
self.food += 1
def play(self):
"""Play with Tamagotchi"""
self.food -= 1object.class StatisticalTest:
"""Base class for Statistical tests"""
def __init__(self, x, y = None, test_name = None):
"""
Initialize the StatisticalTest.
Parameters:
- x: The first sample for the test.
- y: The second sample for tests that compare two samples (default is None).
- test_name: The name of the test as string (default is None).
"""
self.x = x
self.y = y
self.test_name = test_name
self.test_statistic = None
self.p_value = None
def __str__(self):
"""Return a string representation of the statistical test."""
return f'{self.test_name}\n' \
f'Test statistic: {self.test_statistic}\n' \
f'P-value: {self.p_value}'
def test(self):
"""
Conduct the statistical test.
This method performs the necessary calculations
to obtain the test statistic and p-value based
on the provided samples.
Important: This method must be implemented in subclasses.
"""
raise NotImplementedErrorHelp on method test in module __main__:
test() method of __main__.StatisticalTest instance
Conduct the statistical test.
This method performs the necessary calculations
to obtain the test statistic and p-value based
on the provided samples.
Important: This method must be implemented in subclasses.
When defining the subclass TTest we override the __init__() method.
import numpy as np
from scipy.stats import t
class TTest(StatisticalTest):
def __init__(self, x, y = None, test_name = None):
super().__init__(x, y, test_name)
def _prepare(self, x):
"""
Prepare the sample for the t-test.
"""
# Ensure x is NumPy array
x = np.array(x)
mean_x = np.mean(x)
# Use ddof = 1 for sample variance in Python
var_x = np.var(x, ddof = 1)
n_x = len(x)
se_x = np.sqrt(var_x / n_x)
return x, mean_x, var_x, n_x, se_xclass TTest_2samp(TTest):
def __init__(self, x, y):
super().__init__(x, y, 'Welch Two Sample t-test')
def test(self):
"""
Conduct two-sample t-test on the provided samples.
"""
if self.y is None:
raise ValueError("Two samples are required for an independent t-test.")
x, mean_x, var_x, n_x, se_x = self._prepare(self.x)
y, mean_y, var_y, n_y, se_y = self._prepare(self.y)
se = np.sqrt(se_x ** 2 + se_y ** 2)
# Calculate the t-statistic
t_stat = (mean_x - mean_y) / se
# Calculate the degrees of freedom
df = se ** 4 / (se_x ** 4 / (n_x - 1) + se_y ** 4 / (n_y - 1))
# Calculate the p-value
p_val = 2 * (1 - t.cdf(abs(t_stat), df))
# Set the test statistic and p-value attributes
self.test_statistic = t_stat
self.p_value = p_val