diff --git a/random_price.cpp b/random_price.cpp index c55aeed6..2c2b987e 100644 --- a/random_price.cpp +++ b/random_price.cpp @@ -1,9 +1,44 @@ // code related to randomness -#include #include "random_price.h" -using namespace std; +#include +#include +#include +double init_stock_price(int price_profile) { + std::random_device rd; + std::mt19937 gen(rd()); + std::normal_distribution distribution(5.0, 2.0); + if (price_profile == 2) { + distribution.param(std::normal_distribution::param_type(50.0, 20.0)); + } + if (price_profile == 3) { + distribution.param(std::normal_distribution::param_type(150.0, 50.0)); + } + return distribution(gen); +} +double random_sd(float price) { + std::random_device rd; + std::mt19937 gen(rd()); + std::normal_distribution d(0.03 * price, 0.02 * price); + return abs(d(gen)); +} +double random_stock(float & mean, float & sd, float & upper_limit, + float & lower_limit) { + std::random_device rd; + std::mt19937 gen(rd()); + std::normal_distribution distribution(mean, sd); + float price = distribution(gen); + // for mean > temp i.e. falling prices + if ((mean - price) > (lower_limit * mean)) { + mean = price; + } + // rising prices + if ((price - mean) > (upper_limit * mean)) { + mean = price; + } + sd = random_sd(price); + return price; +} unsigned int random_integer(unsigned int max_integer) { - // Return a random integer from 0 to max - 1 return rand() % max_integer; } diff --git a/random_price.h b/random_price.h index 3068a0ea..0e36505e 100644 --- a/random_price.h +++ b/random_price.h @@ -1,5 +1,33 @@ #ifndef RANDOM_PRICE_H #define RANDOM_PRICE_H +/** +#ifndef +#define are to prevent double include the header +*/ + +/** + * Initial Stock Price Generator + * if price_profile=2 mean 50 sd 20; if price_profile=3 mean 150 sd 50; else mean 5 sd 2; + *Stock prices cluster in 3 tiers in our world: about 5 hkd (a=ELSE) variating about 2 hkd; , 50 hkd(a=2) variating about 20 hkd, + *and 150 hkd(a=3)variating about 50 hkd (based on a very little sample observation of real world). + */ +double init_stock_price(int a); +/** + * Stock Standard Deviation Generator (for both initial and after initial) + * s.d. usually 1%-5% of initial stock price; + * modelling with absolute value of norm dist mean 0.03*init_price; s.d. 0.02*init_price (yes, s.d. of s.d.) + */ +double random_sd(float init_price); +/** + * Random Stock Price Generator (non-initialising) + * requires mean, s.d., upper limit increase threshold (FRACTION of mean) and lower limit decrease threshold (FRACTION of mean) + * e.g. both 0.03; then the mean changes to exactly the new price if the value differ from mean by 3% + */ +double random_stock(float & mean, float & sd, float & upper_limit, float & lower_limit); +/** + * Random integer function in choosing stock type + * Return a random integer from 0 to max - 1 + */ unsigned int random_integer(unsigned int max_integer); #endif