Skip to content

Commit

Permalink
Fixed bug of invalid s.d; also migrated to use std:: instead of using…
Browse files Browse the repository at this point in the history
… namespace sts.

Note the comments are migrated to the header.
  • Loading branch information
84ds84d8s authored Apr 9, 2024
1 parent a122e4c commit b7c4aa2
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions random_price.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,37 @@
#include <iostream>
#include <random>

using namespace std;

double init_stock_price(int a){
//Stock prices cluster in 3 tiers in our world: about 5 hkd (a=1) 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).
//If an invalid input we default a=1.
std::random_device rd;
//std mt19937 Merssane Twister
std::random_device rd;
//Merssane Twister
std::mt19937 gen(rd());
//normal_distribution<double> distribution(5.0, 2.0);
if(a==2){
normal_distribution<double> distribution(50.0, 20.0);
std::normal_distribution<double> distribution(50.0, 20.0);
return distribution(gen);
}
}
else {
if(a==3){
normal_distribution<double> distribution(150.0, 50.0);
std::normal_distribution<double> distribution(150.0, 50.0);
return distribution(gen);
}
else{
normal_distribution<double> distribution(5.0, 2.0);
std::normal_distribution<double> distribution(5.0, 2.0);
return distribution(gen);
}
}
return 0.0;
}
double random_sd(float price){
//s.d. usually 1%-5% of initial stock price; modelling with normal(0.03*init_price, 0.02*init_price)
std::random_device rd;
std::mt19937 gen(rd());
normal_distribution<double> d(0.03*price, 0.02*price);
return d(gen);
std::normal_distribution<double> d(0.03*price, 0.02*price);
return abs(d(gen));

}
double random_stock(float &mean, float &sd,float &upper_limit, float &lower_limit){
//upper_limit and lower_limit are FRACTIONS of the mean so that it remains usable even for a rise
// or fall of large margin; unless changed manually. e.g. a 3% of the mean lower limit
//until mean price fall then lower_limit=0.03
std::random_device rd;
std::mt19937 gen(rd());
normal_distribution<double> distribution(mean, sd);
std::mt19937 gen(rd());
std::normal_distribution<double> distribution(mean, sd);
float price = distribution(gen);
// for mean > temp i.e. falling prices
if ((mean-price)>(lower_limit*mean)){
Expand All @@ -58,6 +48,5 @@ double random_stock(float &mean, float &sd,float &upper_limit, float &lower_limi
return price;
}
unsigned int random_integer(unsigned int max_integer) {
// Return a random integer from 0 to max - 1
return rand() % max_integer;
return rand() % max_integer;
}

0 comments on commit b7c4aa2

Please sign in to comment.