联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2024-04-01 11:56

Data Science for Finance

Problem Set 2

Exercise 1:

Rewrite Problem set 1 Exercise 4 using the if statement.

Exercise 2:

Assuming you are designing a trading system for a stock brokerage, one important rule to consider is the tick size. Tick size is the smallest price increment in which the prices are quoted. We assume the tick size is $0.01 (this is consistent with the actual tick size for most U.S. stocks).

a)  Write an algorithm that asks the user to enter the current stock price. Then the algorithm displays the 10 quotable prices that precede the current stock  price and the 10 quotable prices that follow the current stock price. Please use for loop to write the algorithm.

b)  Re-write the previous algorithm using while loop instead of for loop.

Exercise 3:

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the  number  itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number. Write an algorithm to print all perfect numbers from 1 to 100. Hint: You need to use the % operator to get the remainder after division (e.g. 7%3 = 1 since 7=2*3+1).

Exercise 4:

This exercise will compare various ways of accessing a collection. Assuming you are designing a website that gives users stock price information. Yahoo finance is a good example. To do this, you need to maintain a list of stock ID and corresponding  prices. When the user needs to access a particular stock, the website needs to quickly output its stock price information.  In the following questions, you are going to simulate this process using different methods and make comparison.

a)  Create a list of 10000 stock ID and corresponding prices using a loop. Stock ID starts from 1 and ends in 10000. For each stock, generate a random stock price between 1 and 500. The final list should look like [(1, 30.32), (2, 10.51), (3, 42.88), (4, 44.60), ….]. The first tuple in the list has the first stock ID 1 and stock price 30.32, the second tuple has the second stock ID 2 and stock price 10.51, etc.

To generate random number uniformly distributed within an interval [a,b], you need to first use the syntax import random to import the random number generator module. Then you can use the function random.uniform(a, b) to generate the random number uniformly distributed within an interval [a,b].

b)  Use the random.shuffle function to shuffle the list in a random order. After the shuffling,  the list should be in a random order. For example, the first element could be stock 300 instead of stock 1.

c)  Use a for loop to go through the list and find the stock price for stock 5000. And record the time it takes for the computer to find the stock price. To record the time, you need to first use the syntax import time to import the Python time module. To record time, you can first use function time.time()to get the time before running the search and then use time.time()again to get the time after running the search. The running time of the search is the difference between two times.

d)  Convert the previously generated list into a dictionary. Use the stock ID as the key and the stock price as the value for the dictionary.

e)  Use the dictionary indexing method to find the stock price for stock 5000. Also record the time it takes for the computer to find the stock price.

f)   Run the program several times. Compare the running time of the search for these two methods. What do you find?






版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp