Python MIT 开放课件股市模拟不完整?

发布于 2024-09-15 12:24:13 字数 2078 浏览 4 评论 0原文

我刚刚从在线发布的麻省理工学院视频讲座中复制了这段代码:(Lec 23 | MIT 6.00 计算机科学与编程简介,2008 年秋季)。由于我必须从视频讲座中复制它,因此我不确定我是否获得了完整的程序。它不能按原样工作,我可以使用一些指导。

谢谢。

import pylab, random

class Stock(object):
    def __init__(self, price, distribution):
        self.price = price
        self.history = [price]
        self.distribution = distribution
        self.lastChange = 0

    def setPrice(self, price):
        self.price = price
        self.history.append(price)

    def getPrice(self):
        return self.price

    def makeMove(self, mktBias, mo):
        oldPrice = self.price
        baseMove = self.distribution() + mktBias
        self.price = self.price * (1.0 + baseMove)
        if mo:
            self.price = self.price + random.gauss(.5, .5)*self.lastChange
        if self.price < 0.01:
            self.price = 0.0
        self.history.append(self.price)
        self.lastChange = oldPrice - self.price

    def showHistory(self, figNum):
        pylab.figure(figNum)
        pylab.plot(self.history)
        pylab.title('Closing Price, Test  ' + str(figNum))
        pylab.xlabel('Day')
        pylab.ylabel('Price')


    def unitTestStock():
        def runSim(stks, fig, mo):
            for a in stks:
                for d in range(numDays):
                    s.makeMove(bias, mo)
                s.showHistory(fig)
                mean += s.getPrice()
            mean = mean/float(numStks)
            pylab.axhline(mean)
        numStks = 20
        numDays = 200
        stks1 = []
        stks2 = []
        bias = 0.0
        mo = False
        for i in range(numStks):
            volatility = random.uniform(0,0.2)
            d1 = lambda: random.uniform(-volatility, volatility)
            d2 = lambda: random.gauss(0.0, volatility/2.0)
            stks1.append(Stock(100.0, d1))
            stks2.append(Stock(100.0, d2))
        runSim(stks1, 1, mo)
        runSim(stks2, 2, mo)

    unitTestStock()
    pylab.show()
    assert False

class Market(object):
    def __init__(self):
        self.stks = []
        self.bias = 0.0

I just copied this code from the MIT video lecture that is posted online: (Lec 23 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008). Since I had to copy it from a video lecture, I'm not sure I got the complete program. It is not working as is, I could use some guidance.

Thanks.

import pylab, random

class Stock(object):
    def __init__(self, price, distribution):
        self.price = price
        self.history = [price]
        self.distribution = distribution
        self.lastChange = 0

    def setPrice(self, price):
        self.price = price
        self.history.append(price)

    def getPrice(self):
        return self.price

    def makeMove(self, mktBias, mo):
        oldPrice = self.price
        baseMove = self.distribution() + mktBias
        self.price = self.price * (1.0 + baseMove)
        if mo:
            self.price = self.price + random.gauss(.5, .5)*self.lastChange
        if self.price < 0.01:
            self.price = 0.0
        self.history.append(self.price)
        self.lastChange = oldPrice - self.price

    def showHistory(self, figNum):
        pylab.figure(figNum)
        pylab.plot(self.history)
        pylab.title('Closing Price, Test  ' + str(figNum))
        pylab.xlabel('Day')
        pylab.ylabel('Price')


    def unitTestStock():
        def runSim(stks, fig, mo):
            for a in stks:
                for d in range(numDays):
                    s.makeMove(bias, mo)
                s.showHistory(fig)
                mean += s.getPrice()
            mean = mean/float(numStks)
            pylab.axhline(mean)
        numStks = 20
        numDays = 200
        stks1 = []
        stks2 = []
        bias = 0.0
        mo = False
        for i in range(numStks):
            volatility = random.uniform(0,0.2)
            d1 = lambda: random.uniform(-volatility, volatility)
            d2 = lambda: random.gauss(0.0, volatility/2.0)
            stks1.append(Stock(100.0, d1))
            stks2.append(Stock(100.0, d2))
        runSim(stks1, 1, mo)
        runSim(stks2, 2, mo)

    unitTestStock()
    pylab.show()
    assert False

class Market(object):
    def __init__(self):
        self.stks = []
        self.bias = 0.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2024-09-22 12:24:13

除了错误输入变量 s 和缺少平均分配之外,您还存在缩进问题。

目前,您当前已将 unitTestStock() 定义为 Stock 类的属性。这不是您想要的,尤其是 unitTestStock 没有 self 参数。要解决您的问题,请合并上述更改,然后缩进函数 unitTestStock() 的整个主体及其后面的 3 行。

代码应该如下所示:

class Stock(object):
    <...>

    def showHistory(self, figNum):
        pylab.figure(figNum)
        pylab.plot(self.history)
        pylab.title('Closing Price, Test  ' + str(figNum))
        pylab.xlabel('Day')
        pylab.ylabel('Price')

def unitTestStock():
    def runSim(stks, fig, mo):
        mean = 0.0
        for s in stks:
            for d in range(numDays):
                s.makeMove(bias, mo)
            s.showHistory(fig)
            mean += s.getPrice()
        mean = mean/float(numStks)
        pylab.axhline(mean)
    numStks = 20
    numDays = 200
    stks1 = []
    stks2 = []
    bias = 0.0
    mo = False
    for i in range(numStks):
        volatility = random.uniform(0,0.2)
        d1 = lambda: random.uniform(-volatility, volatility)
        d2 = lambda: random.gauss(0.0, volatility/2.0)
        stks1.append(Stock(100.0, d1))
        stks2.append(Stock(100.0, d2))
    runSim(stks1, 1, mo)
    runSim(stks2, 2, mo)

unitTestStock()
pylab.show()
assert False

In addition to mistyping the variable s and missing the mean assignment, you also have an indentation problem.

As it stands, you've currently defined unitTestStock() as an attribute of the Stock class. This is not what you want, especially as unitTestStock has no self parameter. To fix your problem, incorporate the above changes, and then dedent the entire body of the function unitTestStock() and the 3 lines following it.

The code should look like this:

class Stock(object):
    <...>

    def showHistory(self, figNum):
        pylab.figure(figNum)
        pylab.plot(self.history)
        pylab.title('Closing Price, Test  ' + str(figNum))
        pylab.xlabel('Day')
        pylab.ylabel('Price')

def unitTestStock():
    def runSim(stks, fig, mo):
        mean = 0.0
        for s in stks:
            for d in range(numDays):
                s.makeMove(bias, mo)
            s.showHistory(fig)
            mean += s.getPrice()
        mean = mean/float(numStks)
        pylab.axhline(mean)
    numStks = 20
    numDays = 200
    stks1 = []
    stks2 = []
    bias = 0.0
    mo = False
    for i in range(numStks):
        volatility = random.uniform(0,0.2)
        d1 = lambda: random.uniform(-volatility, volatility)
        d2 = lambda: random.gauss(0.0, volatility/2.0)
        stks1.append(Stock(100.0, d1))
        stks2.append(Stock(100.0, d2))
    runSim(stks1, 1, mo)
    runSim(stks2, 2, mo)

unitTestStock()
pylab.show()
assert False
秋意浓 2024-09-22 12:24:13

您似乎缺少 mean = 0.0 并且需要将 a 更改为 s

def runSim(stks, fig, mo):
    mean = 0.0
    for s in stks:
        for d in range(numDays):
            s.makeMove(bias, mo)
        s.showHistory(fig)
        mean += s.getPrice()
    mean = mean/float(numStks)
    pylab.axhline(mean)

PS。我认为大部分代码都在 此 pdf,可以在 此页面

You seem to be missing mean = 0.0 and need to change an a to an s:

def runSim(stks, fig, mo):
    mean = 0.0
    for s in stks:
        for d in range(numDays):
            s.makeMove(bias, mo)
        s.showHistory(fig)
        mean += s.getPrice()
    mean = mean/float(numStks)
    pylab.axhline(mean)

PS. I think most of this code is in this pdf, which can be found on this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文