即使概率相等,我的数据仍呈下降趋势

My Data keeps trending downwards even though it's equal probabilities

所以我想就此寻求一些帮助。请注意,我是 python 的新手,所以我的以下代码不会是最干净的,我会很乐意并且会喜欢你的提示和建议如何让它变得更好,但是 我的主要问题是关于下面的代码。

如果你要 运行 它,你可以 运行 每“分钟”一年(这就是我一直在做的),并根据每分钟生成一个“值”关于概率。

但不知为什么,到最后它一直呈下降趋势,我也不知道为什么。我在工作时 运行 100 个文件,我只是检查了它们,然后再次(我已经测试了多次),大多数文件都呈下降趋势。

即我从 1000 开始,然后以更低的价格结束。这对我来说没有意义,因为价值上升、下降或保持不变的概率是相等的。

请有人能帮我一下。我希望它生成很多看起来完全不同并且行为完全不同的文件。

import random
import os
import time

def convert_time(total_run_time):
    return time.strftime('%H:%M:%S', time.gmtime(total_run_time))

def choice_years():
    f = open(f'./{folder}/Details.txt', 'a')
    f.write(f'You chose to run for: {range_years} Year(s)\n')
    f.write(f'You will generate data for: {str(iteration_total)} iterations\n')
    f.close()
    print(f'You will generate {str(iteration_total)} entries of data.')
def choice_months():
    f = open(f'./{folder}/Details.txt', 'a')
    f.write(f'You chose to run for: {range_months} Month(s)\n')
    f.write(f'You will generate data for: {str(iteration_total)} iterations\n')
    f.close()
    print(f'You will generate {str(iteration_total)} entries of data.')
def choice_days():
    f = open(f'./{folder}/Details.txt', 'a')
    f.write(f'You chose to run for: {range_days} Day(s)\n')
    f.write(f'You will generate data for: {str(iteration_total)} iterations\n')
    f.close()
    print(f'You will generate {str(iteration_total)} entries of data.')
def choice_hours():
    f = open(f'./{folder}/Details.txt', 'a')
    f.write(f'You chose to run for: {range_hours} Hour(s)\n')
    f.write(f'You will generate data for: {str(iteration_total)} iterations\n')
    f.close()
    print(f'You will generate {str(iteration_total)} entries of data.')

# Name_Number of Files_Time Geration_Number of Y/M/D/H_Every how many minutes 
folder = 'Dummy_Data_4.csv'
os.mkdir(folder)

FILE_INPUT = input('Please choose the name of the .txt file you want to write to:\n')
FILE_NUMBER = int(input('\nHow many files do you want to generate?:\n'))
f = open(f'./{folder}/Details.txt', 'w')
f.write(f'You chose to generate {str(FILE_NUMBER)} file(s)\n')
f.close()

iteration = input('\nEvery how many minutes do you want data?:\n')
f = open(f'./{folder}/Details.txt', 'a')
f.write(f'You chose to generate data every: {iteration} minute(s)\n')
f.close()

if iteration.isdigit():
    time_duration = input('\nDo you want to run it for Years, Months, Days, or Hours?:\n1. Years\n2. Months\n3. Days\n4. Hours\n')
    if time_duration == '1' or time_duration == 'Years' or time_duration == '1. Years' or time_duration == 'Y' or time_duration == 'years' or time_duration == '1. years' or time_duration == 'y':
        print('\nYou chose Years') 
        range_years = input('How many Years? Please input an integer:\n')
        iteration_total = int((int(range_years) * 525600)/int(iteration))
        choice_years()
    elif time_duration == '2' or time_duration == 'Months' or time_duration == '2. Months' or time_duration == 'M' or time_duration == 'months' or time_duration == '2. months' or time_duration == 'm':
        print('\nYou chose Months')
        range_months = input('How many Months? Please input an integer (Note: The average number of days for a month will be taken as 30):\n')
        iteration_total = int((int(range_months) * 43200)/int(iteration))
        choice_months()
    elif time_duration == '3' or time_duration == 'Days' or time_duration == '3. Days' or time_duration == 'D' or time_duration == 'days' or time_duration == '3. days' or time_duration == 'd':
        print('\nYou chose Days')
        range_days = input('How many Days? Please input an integer:\n')
        iteration_total = int((int(range_days) * 1440)/int(iteration))
        choice_days()
    elif time_duration == '4' or time_duration == 'Hours' or time_duration == '4. Hours' or time_duration == 'H' or time_duration == 'hours' or time_duration == '4. hours' or time_duration == 'h':
        print('\nYou chose Hours')
        range_hours = input('How many Hours? Please input an integer:\n')
        iteration_total = int((int(range_hours) * 60)/int(iteration))
        choice_hours()
    else:
        print('Your choice is invalid, goodbye')
        exit()
else:
    print('Your input was incorrect, goodbye')
    exit()

starting_price = float(input('\nWhat is your starting price?:\n'))
new_price = float('%.2f' % starting_price)
print('Your starting price is ' + 'R%.2f' % starting_price)
f = open(f'./{folder}/Details.txt', 'a')
f.write('Your starting price is: ' + 'R%.2f' % starting_price + '\n' + '\n')
f.close()

i = 1
j = 1
kick_off_time = time.time()
while j <= FILE_NUMBER:
    NAME_WITHOUT_EXTENSION = FILE_INPUT + '_' + str(j)
    CSV_FILE_WRITE = FILE_INPUT + '_' + str(j) + '.csv'
    print(CSV_FILE_WRITE)
    i = 1
    j += 1
    new_price = float('%.2f' % starting_price)
    start_time = time.time()
    while i <= iteration_total:
        up_or_down = ['up', 'down', 'none']
        up_or_down_decision = random.choices(up_or_down, weights=(33,33,33), k=1)
        if up_or_down_decision[0] == 'up':
            change_list = [0.001, 0.002, 0.00005, 0.01, 0.003]
            change_decision = random.choice(change_list)
            added_price = new_price * change_decision
            new_price = new_price + added_price
            formatted_new_price = float('%.2f' % new_price)
            f = open(f'./{folder}/' + CSV_FILE_WRITE, 'a')
            f.write(f'{str(i)},{str(formatted_new_price)} \n')
            f.close()
            i += 1
        elif up_or_down_decision[0] == 'down':
            change_list = [0.001, 0.002, 0.00005, 0.01, 0.003]
            change_decision = random.choice(change_list)
            subtracted_price = new_price * change_decision
            new_price = new_price - subtracted_price
            formatted_new_price = float('%.2f' % new_price)
            f = open(f'./{folder}/' + CSV_FILE_WRITE, 'a')
            f.write(f'{str(i)},{str(formatted_new_price)} \n')
            f.close()
            i += 1
        elif up_or_down_decision[0] == 'none':
            formatted_new_price = float('%.2f' % new_price)
            end_time = time.time()
            iteration_time = float('%.2f' % (end_time - start_time))
            f = open(f'./{folder}/' + CSV_FILE_WRITE, 'a')
            f.write(f'{str(i)},{str(formatted_new_price)} \n')
            f.close()
            i += 1
    end_time = time.time()
    iteration_time = '%.5f' % float(end_time - start_time)
    f = open(f'./{folder}/Details.txt', 'a')
    f.write(f'{CSV_FILE_WRITE}: {iteration_time} seconds \n')
    f.close()
termination_time = time.time()
total_run_time = termination_time - kick_off_time
f = open(f'./{folder}/Details.txt', 'a')
f.write(f'Total Run Time: {convert_time(total_run_time)} seconds')
f.close()

您从之前的总和中添加或减去百分比。每个百分比都可以,加减都可以。

想想这里发生了什么。您首先将 1% 加到 1000,然后从结果中减去 1%,并一遍又一遍地重复几次。你的最终结果将小于 1000。你自己试试:

def tryit():
    print("Ten rounds, substracting first")
    price = 1000
    for x in range(10):
        if x%2 == 0: 
            price -= price*0.01
        else:
            price += price*0.01
        print(price)
    
    print("Ten rounds, adding first")
    price = 1000
    for x in range(10):
        if x%2 == 0: 
            price += price*0.01
        else:
            price -= price*0.01
        print(price)
        
        
tryit()

即使您一遍又一遍地加减相同的百分比,您的最终价格仍低于 1000。

这是为什么?

  • 第一轮:1000 * 0.01 -> 加10 -> 新价格= 1010
  • 第二轮:1010 * 0.01 -> 减去10.1 -> 新价= 999.90

如果我们从减法开始,我们以同样的效果结束:

  • 第一轮:1000 * 0.01 -> 减10 -> 新价= 990
  • 第二轮:990 * 0.01 -> 加9.9 -> 新价= 999.9

即使您在那里添加一些随机元素,也会出现这种行为。

那么如何保持这种平衡呢?不要加减,要乘除。

def thisworks():
    print("Ten rounds, substracting first")
    price = 1000
    for x in range(10):
        if x%2 == 0: 
            price = price*1.01
        else:
            price = price/1.01
        print(price)
    print("Ten rounds, adding first")
    price = 1000
    for x in range(10):
        if x%2 == 0: 
            price = price/1.01
        else:
            price = price*1.01
        print(price)
        
        
thisworks()

并将其放入您的代码中(只是其中的一部分):

 while i <= iteration_total:
        up_or_down = ['up', 'down', 'none']
        up_or_down_decision = random.choices(up_or_down, weights=(33,33,33), k=1)
        if up_or_down_decision[0] == 'up':
            change_list = [0.001, 0.002, 0.00005, 0.01, 0.003]
            change_decision = random.choice(change_list)
            # XXX Change here
            # added_price = new_price * change_decision
            # new_price = new_price + added_price
            new_price = new_price * (1+change_decision)
            formatted_new_price = float('%.2f' % new_price)
            f = open(f'./{folder}/' + CSV_FILE_WRITE, 'a')
            f.write(f'{str(i)},{str(formatted_new_price)} \n')
            f.close()
            i += 1
        elif up_or_down_decision[0] == 'down':
            change_list = [0.001, 0.002, 0.00005, 0.01, 0.003]
            change_decision = random.choice(change_list)
            # XXX Change here
            # subtracted_price = new_price * change_decision
            # new_price = new_price - subtracted_price
            new_price = new_price / (1+change_decision)
            formatted_new_price = float('%.2f' % new_price)
            f = open(f'./{folder}/' + CSV_FILE_WRITE, 'a')
            f.write(f'{str(i)},{str(formatted_new_price)} \n')
            f.close()
            i += 1