Posts: 279
Threads: 107
Joined: Aug 2019
Hi all!
Here's some code:
price_chg = np.arange(-10,11,2)
stock_prices = [100]
for i in range(10001):
if (stock_prices[-1] + np.random.choice(price_chg)) < 0:
stock_prices.append(0)
continue
else:
stock_prices.append(stock_prices[-1] + np.random.choice(price_chg)) Why is the if block failing to keep negative integers out of the list?
Posts: 582
Threads: 1
Joined: Aug 2019
(Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list? Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
stock_prices = [100]
for i in range(10001):
random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
if random_price < 0:
stock_prices.append(0)
else:
stock_prices.append(random_price)
Posts: 279
Threads: 107
Joined: Aug 2019
Aug-23-2021, 02:59 PM
(This post was last modified: Aug-23-2021, 03:00 PM by Mark17.)
(Aug-23-2021, 02:44 PM)ibreeden Wrote: (Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list? Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
stock_prices = [100]
for i in range(10001):
random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
if random_price < 0:
stock_prices.append(0)
else:
stock_prices.append(random_price)
When will the "stupid mistakes" end? Rhetorical question. :)
Thank you!
Posts: 6,823
Threads: 20
Joined: Feb 2020
max() may be a better choice.
import random
price = 10
stock_prices = [price]
for _ in range(11):
price = max(0, price + random.randrange(-10, 11, 2))
stock_prices.append(price)
print(stock_prices)
Posts: 45
Threads: 0
Joined: Aug 2021
Aug-24-2021, 04:38 AM
(This post was last modified: Aug-24-2021, 04:38 AM by naughtyCat.)
One line code
import random
stock_prices = [10] + [max(0, 10+random.randrange(-10, 11, 2)) for _ in range(11)]
Posts: 6,823
Threads: 20
Joined: Feb 2020
Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.
Posts: 45
Threads: 0
Joined: Aug 2021
(Aug-24-2021, 08:31 AM)deanhystad Wrote: Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.
two line
import random
price = 10
stock_prices = [price] + [max(0, price+random.randrange(-10, 11, 2)) for _ in range(11)]
Posts: 6,823
Threads: 20
Joined: Feb 2020
Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.
Posts: 45
Threads: 0
Joined: Aug 2021
(Aug-24-2021, 03:32 PM)deanhystad Wrote: Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.
ya, you are right, I am learned, thanks
|