Python Homework Help
Python Homework Help
4. Constructing a black box test suite involves looking at the control flow of
the code to be tested.
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
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.
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
# 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
runTest(3)
pylab.show()