1 / 34

4. A Stock price problem

4. A Stock price problem. Rocky K. C. Chang 13 November 2018. Objectives. To apply our problems-solving procedure to a finance problem To evaluate the time complexity of a computer algorithm To design a much more efficient algorithm than a brute force approach. Time complexity of algorithms.

ruthanng
Télécharger la présentation

4. A Stock price problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4. A Stock price problem Rocky K. C. Chang 13 November 2018

  2. Objectives • To apply our problems-solving procedure to a finance problem • To evaluate the time complexity of a computer algorithm • To design a much more efficient algorithm than a brute force approach

  3. Time complexity of algorithms • The correctness of an algorithm is obviously important. • The performance of an algorithm is equally, if not more than, the correctness. • (Time complexity) How much time does it take to solve a problem of size n? • E.g., Tower of Hanoi • (Space complexity) How much memory space does it take to solve a problem of size n? • Classification of algorithms: • Constant: O(1) (e.g., # operations = 10 regardless of n) • Between constant and linear: O(log n) • Linear: O(n) (e.g., # operations = 2n + 10) • Exponential: O(n3), O(2n), etc.

  4. A simple example • Consider the problem of computing 0! + 1! + 2! + …+ n!. • One implementation: • We use the number of multiplications as the measure for time complexity. • What is the time complexity of this algorithm? • Could we do better than this?

  5. Exercise 4.1 Search on Google: “yahoo finance quote your_stock_to_watch”. Choose historical data and specify the time period to watch. Download the data and view it using a text editor. What is the data format? View it using a spreadsheet program and plot the value of “close”.

  6. A problem • Find the up-period for a given day k in a stock’s price time series. • Up-period for day k: • The maximum number of consecutive days just before day k for which their prices are not higher than day k. • Backward checking until finding a higher price or reaching the first day. • In our case, we also include day k. That is, the up-period is at least equal to 1. • See examples in the next slide.

  7. Up-period is 26. Up-period is 1. Up-period is 9. Up-period is 18.

  8. Up-period is 8. Up-period is 1.

  9. The stock-price problem • Implement a function to compute the up-periods for all days. • Function signature: Function stockUp(stock) Input: stock is a list of stock prices on a range of days which starts from day 0. Output: a list of up-periods for the days.

  10. Exercise 4.2 Write the pseudocode for the stockUp() function.

  11. Exercise 4.3 Implement the pseudocode obtained in exercise 4.2. Use this test data: [7, 12, 9, 6, 3, 9, 10].

  12. Exercise 4.4 Modify the code from exercise 4.3 to return the up periods as well as the count of comparisons. Print DayPriceUp period# comparisons 1 7 1 ? 2 12 2 ? : :

  13. Exercise 4.5 The algorithm we have developed and implemented in exercises 4.2 and 4.3 is a brute-force method. Could we skip some comparisons if we somehow remember the previous results? Which comparisons in our toy example can we skip?

  14. Exercise 4.6 How do we exploit the observation made in exercise 4.5? What information should we retain?

  15. Exercise 4.7 How to use a stack to check the balance of parentheses?

  16. Exercise 4.8 Implement the improved method using a stack.

  17. Exercise 4.9 Extend the program in exercise 4.8 to include the count of comparisons.

  18. Exercise 4.10 Compute the number of comparisons saved by the stack-based method for [7,12,9,6,3,9,10] by hand. Try it also for [7,12,9,6,3,9,10,11] and [7,12,9,6,3,9,10,11,12] .

  19. Exercise 4.11 What are the worst-case time complexities of the brute-force algorithm and the improved algorithm?

  20. End

  21. End “Better a little with righteousness than much gain with injustice.” (Proverbs 16:8) “收入少而有公義,勝過收入多卻毫無正義。” (箴言 16:8)

More Related