Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First working demo of random price generation #10

Merged
merged 14 commits into from
Apr 9, 2024
49 changes: 46 additions & 3 deletions random_price.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
// code related to randomness
#include <cstdlib>
#include "random_price.h"
using namespace std;
#include <cstdlib>
#include <iostream>
#include <random>

double init_stock_price(int a) {
std::random_device rd;
// Merssane Twister
std::mt19937 gen(rd());
if (a == 2) {
std::normal_distribution<double> distribution(50.0, 20.0);
return distribution(gen);
}
else {
if (a == 3) {
std::normal_distribution<double> distribution(150.0, 50.0);
return distribution(gen);
}
else {
std::normal_distribution<double> distribution(5.0, 2.0);
return distribution(gen);
}
}
return 0.00;
}
eric15342335 marked this conversation as resolved.
Show resolved Hide resolved
double random_sd(float price) {
std::random_device rd;
std::mt19937 gen(rd());
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) {
std::random_device rd;
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)) {
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;
84ds84d8s marked this conversation as resolved.
Show resolved Hide resolved
84ds84d8s marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 27 additions & 0 deletions random_price.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
#ifndef RANDOM_PRICE_H
#define RANDOM_PRICE_H

/**
#ifndef
#define are to prevent double include the header
*/
/**
* Initial Stock Price Generator
* if a=2 mean 50 sd 20; if a=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