# quant-econ Solutions: Object Oriented Programming

## Exercise 1

``````

In :

class ECDF:

def __init__(self, observations):
self.observations = observations

def __call__(self, x):
counter = 0.0
for obs in self.observations:
if obs <= x:
counter += 1
return counter / len(self.observations)

``````
``````

In :

# == test == #

from random import uniform
samples = [uniform(0, 1) for i in range(10)]
F = ECDF(samples)

print F(0.5)  # Evaluate ecdf at x = 0.5

F.observations = [uniform(0, 1) for i in range(1000)]

print F(0.5)

``````
``````

0.2
0.519

``````

## Exercise 2

``````

In :

class Polynomial:

def __init__(self, coefficients):
"""
Creates an instance of the Polynomial class representing

p(x) = a_0 x^0 + ... + a_N x^N,

where a_i = coefficients[i].
"""
self.coefficients = coefficients

def __call__(self, x):
"Evaluate the polynomial at x."
y = 0
for i, a in enumerate(self.coefficients):
y += a * x**i
return y

def differentiate(self):
"Reset self.coefficients to those of p' instead of p."
new_coefficients = []
for i, a in enumerate(self.coefficients):
new_coefficients.append(i * a)
# Remove the first element, which is zero
del new_coefficients
# And reset coefficients data to new values
self.coefficients = new_coefficients

``````
``````

In [ ]:

``````