For any Assignment related queries, Call us at : -
+1 678 648 4277
You can mail us at :-
[email protected] or
reach us at :- https://www.pythonhomeworkhelp.com/
Quality Python Homework Help
PROBLEM
Here are some practice quiz questions (taken mostly from last year’s final).
1)Is each of the following True or False
1. In Python, the method sort of class list has a side effect.
2. When run on the same inputs, an exponential algorithm will always
take longer than a polynomial algorithm.
3. Newton’s method is based upon successive approximation.
4. Constructing a black box test suite involves looking at the control flow of
the code to be tested.
5. Python classes enforce data hiding.
2) The code below produces three plots. Match each of the plots on the next
page with the appropriate figure (Figure 1, Figure 2, or Figure 3).
import pylab y1 = []
y2 = []
y3 = []
for i in range(20): y1.append(300*i)
y2.append(i**3)
y2.append(i**3)
y3.append(2**i)
pylab.figure(1)
pylab.plot(y1)
pylab.figure(2)
pylab.plot(y2)
pylab.figure(3)
pylab.plot(y3)
pylab.semilogy()
3) Can the following specification be implemented? If not, explain why.
def f(x, e):
“““ x and e are floats
returns y, a float, such that x-e <= y*y <= x+e”””
4) Write a Python program that satisfies the following specification.
def f(L1, L2):
“““ L1 and L2 are lists of integers
returns the element-wise product of L1 and L2. If the lists are of
different length, it uses 1 for the missing coefficients.
E.g., if L1 = [2, 2, 3] and L2 = [7, 5, 6, 7]
it returns [14, 10, 18, 7] side effects: none ”””
The following questions all refer to the code you were asked to study in
preparation for this exam. They also assume that the following code is
used to test the program. (For your convenience, a copy of the posted
code is at the end of this quiz. Feel free to separate it from the rest of
the quiz.)
Warning: The code at the end of this is NOT the same as the code you
have been given to study. It may not be worth your time to study this
code carefully enough to answer all of the practice questions, but
these questions should give you the flavor of the kinds of questions
we might ask about the code supplied this term.
numDays = 500 bias =
0.11/200.0 numSectors = 1
sectorSize = 500
numTrials = 5
print 'Testing Model on', numDays, 'days' mean =
0.0
for i in range(numTrials):
pylab.figure(1)
mkt = generateMarket(numSectors,
sectorSize, bias)
endPrices = simMkt(mkt, numDays) mean +=
endPrices[-1]
title = 'Closing Prices With ' + str(numSectors) + ' Sectors'
plotValueOverTime(endPrices, title)
pylab.figure(2) plotDistributionAtEnd(mkt,
title)
meanClosing = mean/float(numTrials)
print 'Mean closing with', numSectors, 'sectors
is', meanClosing
1) If, in class Stock, the line
self.price = self.price * (1.0 + baseMove)
were replaced with the line
self.price = self.price * baseMove
which of the following best describes the expected mean value of the
market after 200 days:
a.Close to 111
b.Close to 100
c.Close to 50
d.Close to 0
e.About the same as with the original code
2)Consider running the above test code twice, once with numSectors = 1 and
once with numSectors = 20. Each will produce a Figure 1. Now consider
running a linear regression to fit a line to each of the curves produced in the
Figure 1’s for each test. Would you expect the average mean square error of
the fits for numSectors =
20 to be:
a.Smaller than the average mean square error of the fits for
numSectors = 1.
b.Larger than the average mean square error of the fits for numSectors
= 1.
c.About the same as the average mean square error of the fits for
numSectors = 1.
d.None of the above.
5.3) Characterize the algorithmic efficiency of generateMarket.
5.4) If in Stock.makeMove, the line
baseMove=bias+random.uniform(-self.volatility, self.volatility)
were replaced by the line
baseMove = bias
which of the following Figure 2’s is most likely to be produced by the test
code? (10 points)
B
A
C D
import pylab import random
# Global constant
TRADING_DAYS_PER_YEAR = 200
class Stock:
def init (self, ticker, volatility): self.volatility =
volatility self.ticker = ticker
self.price = None self.history = []
def setPrice(self, price): self.price =
price
self.history.append(price) def
getPrice(self):
return self.price def
getTicker(self):
return self.ticker def
makeMove(self, bias):
if self.price == 0.0: return
baseMove = bias + random.uniform(-self.volatility, self.volatility) self.price
= self.price * (1.0 + baseMove)
if self.price < 0.01: self.price = 0.0
self.history.append(self.price)
class Market:
def init (self): self.sectors = []
self.tickers = set()
def addSector(self, sect):
if sect.getName() in self.sectors: raise
ValueError('Duplicate sector')
for t in sect.getTickers(): if t in self.tickers:
raise ValueError('A ticker in sect already in market') else:
self.tickers.add(t)
self.sectors.append(sect) def
getSectors(self):
return self.sectors def
getStocks(self):
stocks = []
for s in self.sectors:
stocks = stocks + s.getStocks() return stocks
def moveAllStocks(self): vals = []
for s in self.sectors:
vals = vals + s.moveAllStocks() return vals
class Sector:
def init (self, sectorName, bias): self.tickers =
set() self.sectorName = sectorName
self.stocks = []
self.bias = bias self.origBias = bias
def addStock(self, stk):
if stk.getTicker() in self.tickers: raise
ValueError('Duplicate ticker')
self.tickers.add(stk.getTicker())
self.stocks.append(stk)
def getStocks(self): return
self.stocks
def getTickers(self): return
self.tickers
def setBias(self, newBias): self.bias =
newBias
def getBias(self): return self.bias
def getOrigBias(self): return
self.origBias
def sameSector(self, other):
return self.sectorName == other.sectorName def
getName(self):
return self.sectorName def
moveAllStocks(self):
vals = []
for stock in self.stocks:
stock.makeMove(self.bias)
vals.append(stock.getPrice())
return vals
def generateSector(sectorSize, sectorName, bias): sect =
Sector(sectorName, bias)
for i in range(sectorSize):
ticker = sectorName + 'Ticker ' + str(i) volatility =
random.uniform(0.01, 0.04) stk = Stock(ticker,
volatility) stk.setPrice(100)
try:
sect.addStock(stk) except
ValueError:
# Tickers are all different by construction, so this should never #
happen
print 'Error in generate stocks, should never reach here.' raise
AssertionError
return sect
def
generateMarket(numSectors,
sectorSize, bias): mkt =
Market()
for n in range(numSectors):
sect = generateSector(sectorSize, 'Sector ' +
str(n), bias) mkt.addSector(sect)
return mkt
def simMkt(mkt, numDays):
endPrices = []
for i in range(numDays):
if i%
(TRADING_DAYS_PER_YE
AR/4) == 0:
for s in mkt.getSectors():
newBias = s.getOrigBias() + random.gauss(0, 2*s.getOrigBias())
s.setBias(newBias)
vals = mkt.moveAllStocks() vals =
pylab.array(vals)
mean = vals.sum()/float(len(vals))
endPrices.append(mean)
return endPrices
def plotValueOverTime(endPrices, title):
pylab.plot(endPrices) pylab.title(title)
def plotDistributionAtEnd(mkt, title):
pylab.xlabel('Days')
prices = [] pylab.ylabel('Price')
for s in mkt.getStocks():
prices.append(s.getPrice())
pylab.hist(prices, bins = 20) pylab.title(title)
pylab.xlabel('Last Sale')
pylab.ylabel('Number of Securities')
Solution
import pylab, random
class Stock(object):
def __init__(self, price, distribution, vol):
self.price = price
self.history = [price]
self.distribution = distribution
self.vol = vol
self.lastChangeInfluence = 0.0
def setPrice(self, price):
self.price = price
self.history.append(price)
def getPrice(self):
return self.price
def makeMove(self, bias, mo):
oldPrice = self.price
baseMove = self.distribution(self.vol) + bias
self.price = self.price * (1.0 + baseMove)
self.price += mo*random.choice([0.0,
1.0])*self.lastChangeInfluence
self.history.append(self.price)
change = self.price - oldPrice
if change >= 0:
self.lastChangeInfluence = min(change, oldPrice*0.01)
else:
self.lastChangeInfluence = max(change, -oldPrice*0.01)
def showHistory(self, fig, test):
pylab.figure(fig)
pylab.plot(self.history)
pylab.title('Closing Prices, Test ' + test)
pylab.xlabel('Day')
pylab.ylabel('Price')
class SimpleMarket(object):
def __init__(self, numStks, volUB):
self.stks = []
self.bias = 0.0
for n in range(numStks):
volatility = random.uniform(0, volUB)
distribution = lambda vol: random.gauss(0.0, vol)
stk = Stock(100.0, distribution, volatility)
self.addStock(stk)
def addStock(self, stk):
self.stks.append(stk)
def setBias(self, bias):
self.bias = bias
def getBias(self):
return self.bias
def getStocks(self):
return self.stks[:]
def move(self, mo):
prices = []
for s in self.stks:
s.makeMove(self.bias, mo)
prices.append(s.getPrice())
return prices
class Market(SimpleMarket):
def __init__(self, numStks, volUB, dailyBiasRange):
SimpleMarket.__init__(self, numStks, volUB)
self.dailyBiasRange = dailyBiasRange
def move(self, mo):
prices = []
dailyBias = random.gauss(self.dailyBiasRange[0],
self.dailyBiasRange[1])
for s in self.stks:
s.makeMove(self.bias + dailyBias, mo)
prices.append(s.getPrice())
return prices
def simMkt(mkt, numDays, mo):
endPrices = []
for i in range(numDays):
vals = mkt.move(mo)
vals = pylab.array(vals)
mean = vals.sum()/float(len(vals))
endPrices.append(mean)
return endPrices
def plotAverageOverTime(endPrices, title):
pylab.plot(endPrices)
pylab.title(title)
pylab.xlabel('Days')
pylab.ylabel('Price')
def plotDistributionAtEnd(mkt, title, color):
prices = []
sumSoFar = 0
for s in mkt.getStocks():
prices.append(s.getPrice())
sumSoFar += s.getPrice()
mean = sumSoFar/float(len(prices))
prices.sort()
pylab.plot(prices, color)
pylab.axhline(mean, color = color)
pylab.title(title)
pylab.xlabel('Stock')
pylab.ylabel('Last Sale')
pylab.semilogy()
def runTrial(showHistory, test, p):
colors = ['b','g','r','c','m','y','k']
mkt = Market(p['numStocks'], p['volUB'], p['dailyBiasRange'])
mkt.setBias(p['bias'])
endPrices = simMkt(mkt, p['numDays'], p['mo'])
pylab.figure(1)
plotAverageOverTime(endPrices, 'Average Closing Prices')
pylab.figure(2)
plotDistributionAtEnd(mkt, 'Distribution of Prices', colors[test
%len(colors)])
if showHistory:
for s in mkt.getStocks():
s.showHistory(test+2, str(test))
def runTest(numTrials):
#Constants used in testing
numDaysPerYear = 200.0
params = {}
params['numDays'] = 200
params['numStocks'] = 500
params['bias'] = 0.1/numDaysPerYear
params['volUB'] #General
= 12.0/numDaysPerYear market
#Upper boundbias
on
volatility for a stock
params['mo'] = 1.1/numDaysPerYear #Momentum factor
params['dailyBiasRange'] = (0.0, 4.0/200.0)
for t in range(1, numTrials+1):
runTrial(True, t, params)
runTest(3)
pylab.show()