Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:29:19+00:00 2026-05-16T10:29:19+00:00

I just copied this code from the MIT video lecture that is posted online:

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:29:20+00:00Added an answer on May 16, 2026 at 10:29 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have copied this code from the official example... I just replaced the api
//just copied this code from w3schools var person={fname:John,lname:Doe,age:25}; for (x in person) { document.write(person[x]
I've just been experimenting with MPI, and copied and ran this code, taken from
i have copied this code from the FB.init documentation : <script src=http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US type=text/javascript></script> ...
I have this code copied from one of questions from SO: public static String
This code snippet hand copied from a book I am reading: /* scmp: string
I copied this code from another post. I tried the example, however, I am
This code is copied directly from Scott Gu's blog (http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx): Hello @name, the year
I just copied over an Visual Studio 2010 source code from one SVN repository
I have just copied Key-Listener code from http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java . I was able to compalie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.