diff --git a/Agent.java b/Agent.java deleted file mode 100644 index 7215437..0000000 --- a/Agent.java +++ /dev/null @@ -1,99 +0,0 @@ -class Agent { //don't worry about this, java's really cool - //and uses classes for everything, - //very close to von neumann set theory - - String philosophy; //initial variables - char party; - - - public Agent(String phi, char orientation){ - - philosophy = phi; //assign philosophy vector, here a string to make it easy on me - party = orientation; //assign whether or not agent is of a positive or antipositive mind - - } - - public static double K(String phi1, String phi2) { - int count = 0; - if (phi1.length() == phi2.length()){ //if vectors same length, check for similarity - for(int i = 0; i +#include +#include +#include +#include +#include +#include +#include + +class agent; + +#include "trader.h" +#include "financial_intermediary.h" using namespace std; //In this program, we will model a financial market -class agent { //creates agent capable of possessing and trading assets - double wealth; - public: - agent(double x) : wealth(x){} //so far just has one public variable denoting total value of all assets - //double networth() { return wealth;} - double networth = wealth; //return wealth -}; +//PODs (Possibly fold into class for additional functionality?) + +/** + * Exchange wealth between two agents (know as the lender and lendee) + * @param lender - the agent giving the wealth + * @param lendee - the agent recieving the wealth + */ +void borrow_lend(agent* lender, agent* lendee); + +/** + * Better Random number generator + * @param fMin Minimum nmuber on the range of possible randomly generated numbers. + * @param fMax Maximum number on the range of possible randomly generated numbers. + * @return A random number on the range of (fMin, fMax). + */ +double dRand(double fMin, double fMax) +{ + std::default_random_engine generator; + std::uniform_real_distributon distribution(fMin, fMax); + return distribution(generator); +} -class financial_intermediary { //creates an agent capable of possessing and trading assets, will also serve as a storage node - double wealth; //with more abilities down the line - public: - financial_intermediary(double x) : wealth(x){} - double networth = wealth; -}; +int main(){ + cout.precision(3); //set precision on output values....might need to retool this -void deposit(agent& A, financial_intermediary& B, double amount){ //function allowing an agent to deposit cash - double agent_wealth_init = A.networth; - double fin_wealth_init = B.networth; + srand(time(NULL)); //generate new sequence of random variables for each loop - A.networth = agent_wealth_init - amount; - B.networth = fin_wealth_init + amount; + int M, N; //number of financial intermediaries, number of nodes, respectively -} + cout << "Enter number of financial intermediaries: "; + cin >> M; -void withdraw(agent& A, financial_intermediary& B, double amount){ //function allowing an agent to deposit cash - double agent_wealth_init = A.networth; - double fin_wealth_init = B.networth; + vector fin_int_vec {}; //placeholder for all financial intermediaries generated below - A.networth = agent_wealth_init + amount; - B.networth = fin_wealth_init - amount; + cout << "Enter number of agents: "; + cin >> N; -} + vector trader_vec {}; //placeholder for all traders generated below + + for (int i = 0; i < M; i++){ + agent * bank = new financial_intermediary(i); //create new financial intermediary + fin_int_vec.push_back(bank); //add financial intermediary to storage vector + } -int main(){ //tests for me + for (int i = 0; i < N; i++){ + agent * node = new trader(i); //create new trader + node->createPortfolio(); //create trader portfolio + trader_vec.push_back(node); //add trader to storage vector + } - agent spender(3.0); - financial_intermediary bank(30000.0); + for (int i = 0; i < N; i++){ + fin_int_vec[0]->createAccount(trader_vec[i]); //create account for all nodes interacting with particular financial intermediary + } //leaving as separate loop so these values can change - cout << spender.networth << '\n'; - cout << spender.networth + 20 << '\n'; - cout << bank.networth << '\n'; + for (int i = 0; i < fin_int_vec[0]->accounts.size(); i++){ //loop through accounts - deposit(spender, bank, 1); + cout << "Account pre-deposit " << fin_int_vec[0]->accounts[i].ID << ' ' << fin_int_vec[0]->accounts[i].value << '\n'; //display account information before deposit + + cout << "Portfolio pre-deposit" << '\n'; - cout << spender.networth << '\n'; - cout << bank.networth << '\n'; + for (int j = 0; j < trader_vec[i]->assets.size(); j++){ //loop through portfolio of account at hand + cout << trader_vec[i]->portfolio[j].type << ' ' //print out portfolio attribues + << trader_vec[i]->portfolio[j].ID << ' ' + << trader_vec[i]->portfolio[j].title << ' ' + << trader_vec[i]->portfolio[j].value << '\n'; + } + + trader_vec[i]->deposit_cash(fin_int_vec[0], utils::dRand(0, trader_vec[i]->portfolio[0].value)); //deposit cash into account - withdraw(spender, bank, 1); + cout << "Account post-deposit " << fin_int_vec[0]->accounts[i].ID << ' ' << fin_int_vec[0]->accounts[i].value << '\n'; //display account information after deposit - cout << spender.networth << '\n'; - cout << bank.networth << '\n'; + cout << "Portfolio post-deposit" << '\n'; + for (int j = 0; j < trader_vec[i]->assets.size(); j++){ //loop through portfolio of account at hand + cout << trader_vec[i]->portfolio[j].type << ' ' //print out portfolio attributes + << trader_vec[i]->portfolio[j].ID << ' ' + << trader_vec[i]->portfolio[j].title << ' ' + << trader_vec[i]->portfolio[j].value << '\n'; + } + trader_vec[i]->withdraw_cash(fin_int_vec[0], dRand(0, trader_vec[i]->portfolio[0].value)); //withdraw cash from account + cout << "Account post-withdrawl " << fin_int_vec[0]->accounts[i].ID << ' ' << fin_int_vec[0]->accounts[i].value << '\n'; //display account information after withdrawl + + cout << "Portfolio post-withdrawl" << '\n'; + for (int j = 0; j < trader_vec[i]->assets.size(); j++){ //loop through portfolio of account at hand + cout << trader_vec[i]->portfolio[j].type << ' ' //print out portfolio attributes + << trader_vec[i]->portfolio[j].ID << ' ' + << trader_vec[i]->portfolio[j].title << ' ' + << trader_vec[i]->portfolio[j].value << '\n'; + } + + cout << '\n'; + + } + + for (int i = 0; i < M; i++){ + delete fin_int_vec[i]; //delete all objects + } + + for (int i = 0; i < N; i++){ + delete trader_vec[i]; //delete all objects + } + return 0; -} \ No newline at end of file +} diff --git a/Grand_Canonical_Adj_Mat.R b/Grand_Canonical_Adj_Mat.R deleted file mode 100644 index 8a2d8c9..0000000 --- a/Grand_Canonical_Adj_Mat.R +++ /dev/null @@ -1,242 +0,0 @@ -library(sna) -library(network) -library(rgl) -library(networkDynamic) - -#preset number of type-nodes -V <- 100 -W1 <- 7 -W2 <- 7 - -#random number of type-nodes -#V <- sample(0:10, 1) -#W1 <- sample(0:10, 1) -#W2 <- sample(0:10, 1) - -#node vector -n <- c(0, V, W1, W2) - -#node colors -colors <- 1:(length(n)-1) -Col <- rep(0, times=sum(n)) -names(colors) <- c("red", "cyan", "green") - - -#node weights -#w <- replicate - -#initialize GCA -#g <- rep(0, times=(sum(n))^2) -g <- replicate(sum(n)^2, sample(0:1, 1), simplify="array") -G <- matrix(g, nrow=sum(n), ncol=sum(n)) - -#create like-components -for (i in 1:(length(n)-1)){ - a <- rep(0, times=n[i+1]^2) - #a <- replicate(n[i+1]^2, sample(0:1, 1), simplify="array") - A <- matrix(a, nrow=n[i+1], ncol=n[i+1]) - for(p in 1:n[i+1]){ - for (q in 1:n[i+1]){ - A[p,q] -> A[q,p] - A[p,p] = 0 - } - } - b <- sum(n[1:i])+1 - c <- sum(n[1:(i+1)]) - count <- length(b:c) - G[b:c, b:c] <- A[1:count, 1:count] - col<- rep(names(colors)[i], times=count) - #print(col) - Col[b:c] <- col[1:count] - #print(Col) -} - -#ensure inhibition of higher access -G[1:V, (V+W1+1):sum(n)] <- 0 - -#prohibit resource binding -G[(V+1):sum(n), (V+1):sum(n)] <- 0 - -#limit binding -for(i in 1:V){ - G[i, (V+1):(V+W1)] = sample(0:1, 1) - while(sum(G[i, (V+1):(V+W1)]) > 1){ - G[i, sample((V+1):(V+W1), 1)] <- 0 - } -} - -#prioritize binding -for (i in 1:V){ - for (j in (V+1):(V+W1)){ - if (G[i,j]== 1){ - G[i, (V+W1+1):sum(n)]=sample(0:1, 1) - } - while(sum(G[i, (V+W1+1):(sum(n))]) > 1){ - G[i, sample((V+W1+1):sum(n), 1)] <- 0 - } - } -} -#print(G) - -#allow for binding of agents if specific conditions are met -vec <- numeric() - -for(i in 1:V){ - for (j in (V+W1+1):(sum(n))){ - if(G[i, j]==1){ - vec <- c(vec, i) - } - } -} -print(vec) -lvec <- length(vec) -uvec <- unique(vec) -for(i in 1:lvec){ - sveci <- sample(i, 1) - for (j in 1:lvec){ - svecj <- sample(j, 1) - G[uvec[sveci], uvec[svecj]] <- 1 - } -} -#print(uvec) -#print(sample(vec, 1)) -#G[uvec[svec], 1), uvec[svec],1] <- 1 -diag(G)=0 - -if (length(uvec) == 1){ - G[1:V, 1:V] = 0 -} - -#print(G[1:V, 1:V]) - - -#make GCA undirected -for (i in 1:sum(n)){ - for (j in 1:sum(n)){ - G[j, i] <- G[i,j] - } -} - -print(G[1:V, 1:V]) -#print(G) - -#create network -net <- network(G, directed=FALSE) -#net %v% "weights" <- w - -#plot network -gplot(net, vertex.col=Col) -#gplot3d(net, vertex.col=Col) - -#agent adjacency matrix -agents <- G[1:V, 1:V] -#print(agents) - -#create society -soc <- network(agents, directed = FALSE) -#wagents <- w[(sum(n)*(1:V))+(1:V)] -#soc %v% "weights" <- wagents - -#plot society -gplot(soc, vertex.col=Col[1:V]) - -#eliminate dead nodes -zeros <- rep(0, times=V) - -for(i in V:1){ - #print(agents) - test <- agents[i,]==zeros - #print(test) - if(all(test)==1){ - agents <- agents[-i, -i] - } -} - -V2 <- sqrt(length(agents)) - -n2 <- c(0, V2, W1, W2) -#print(G) -#print(V2) -#print(agents) -'for(i in 1:V-1){ - if (sum(G[i, (i+1):V])>0){ - G[i, (V+1):sum(n)] <- G[i, (V+1):sum(n)] - } -}' - -G[1:V2, 1:V2] <- agents[1:V2, 1:V2] -print(uvec) -print(G) -'for(i in 1:V2){ - #if (G[i,(i+1): ] - print(G[i,]) - G[i, (V2+1):sum(n2)] <- G[uvec[i], (V2+1):sum(n2)] -}' -#print(G) -for(i in 1:sum(n2)){ - for(j in 1:sum(n2)){ - G[j, i] <- G[i,j] - } -} -#print(G) -#print(G) -#print(G[1:V2, 1:V2]) -#print(G[1:V, 1:V]) -#print(agents2) -#G[1:V2, 1:V2] <- agents -#print(agents) -#node vector - -for (i in V:(V2+1)){ - #print(G[1:i, 1:i]) - G <- G[-i, -i] -} -print(G[1:V2, 1:V2]) -#print(G) -#node colors -colors2 <- 1:(length(n2)-1) -Col2 <- rep(0, times=sum(n2)) -names(colors2) <- c("red", "cyan", "green") - -for (i in 1:(length(n2)-1)){ - a2 <- rep(0, times=n2[i+1]^2) - #a <- replicate(n[i+1]^2, sample(0:1, 1), simplify="array") - A2 <- matrix(a2, nrow=n2[i+1], ncol=n2[i+1]) - for(p in 1:n2[i+1]){ - for (q in 1:n2[i+1]){ - A2[p,q] -> A2[q,p] - A2[p,p] = 0 - } - } - b2 <- sum(n2[1:i])+1 - c2 <- sum(n2[1:(i+1)]) - count2 <- length(b2:c2) - #print(count2) - #G[b2:c2, b2:c2] <- A2[1:count2, 1:count2] - col2<- rep(names(colors2)[i], times=count2) - #print(col2) - Col2[b2:c2] <- col2[1:count2] - #print(Col2) -} -#print(Col2) - -#print(G[1:V, 1:V]) -for (i in V:(V2+1)){ - #print(G[1:i, 1:i]) - G <- G[-i, -i] -} -#print(G[1:V2, 1:V2]) -#create society -socdos <- network(agents, directed = FALSE) -#wagents <- w[(sum(n)*(1:V))+(1:V)] -#socdos %v% "weights" <- wagents - -#plot society -gplot(socdos, vertex.col=Col[1:V2]) -#gplot3d(socdos, vertex.col=Col[1:V]) -#print(agents) - -net2 <- network(G, directed = FALSE) -#net2 %v% "weights" <- w - -gplot(net2, vertex.col=Col2) \ No newline at end of file diff --git a/README.md b/README.md index 211a682..85ee5c6 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# Networks-Project +# Network Projects + +A project between a couple of dudes to simulate various networks + + +## Financial Mahkets + +An endevour to simulate financial markets. + diff --git a/agent.h b/agent.h new file mode 100644 index 0000000..d3d7581 --- /dev/null +++ b/agent.h @@ -0,0 +1,60 @@ +#include +#include +#include "utils.h"; +//////////// +//agent.h // +//////////// +/** + * creates agent capable of possessing and trading assets + */ + +struct account { + string type; + long int ID; //ID associated with holder + double value; //value associated with asset + int period; //track how long asset has been stored +}; + +struct claim { + //name of asset...not really necessary to include but could come in handy + string title; + //claim type + string type; + //ID associated with holder + long int ID; + //value associated with asset + double value; + //for bonds, determines repayment period; for stocks, does not apply + int period; +} money, stock, bond; + +class agent { + private: + long int ID; + double wealth; + public: + agent(long int id, double w) : ID(id), wealth(w){} + agent(long int id) : ID(id){} + long int getID() {return ID;} + + //array of asset types + vector assets{"money", "securities", "bonds"}; + + //array same length assets containing info about each asset + claim portfolio[3]; + + //create a portfolio of assets + virtual void createPortfolio(){} + + //function that will return the total value of all assets held by the agent + virtual double calculateNetworth() {return wealth;} + + //function allowing an agent to deposit cash + virtual void depositCash(agent* B, double amount){} + + //function allowing an agent to withdraw cash + virtual void withdrawCash(agent* B, double amount){} + + //create an account for an agent at hand + virtual void createAccount(agent* A) {} +}; \ No newline at end of file diff --git a/Economics_Template.pdf b/docs/Economics_Template.pdf similarity index 100% rename from Economics_Template.pdf rename to docs/Economics_Template.pdf diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..edb4694 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,3 @@ +# Financial Markets Documents + +A place to store documents unrelated to the actual function of the project. diff --git a/financial_intermediary.h b/financial_intermediary.h new file mode 100644 index 0000000..0fd22c7 --- /dev/null +++ b/financial_intermediary.h @@ -0,0 +1,36 @@ +#include "agent.h" +/////////////////////////// +//financial_intermediary // +/////////////////////////// +/** + * creates an agent capable of possessing and trading assets, will also serve as a storage node + */ +class financial_intermediary: public agent { + public: + financial_intermediary(int id) : agent(id) {} + vector accounts {}; + /** + * Create new account to add to total accounts of financial intermediary + * @param newAccount the acconut to be created. + */ + void createAccount(agent* newAccount) { + account temp; + temp.ID = A->getID(); + temp.value = 0; + accounts.push_back(temp); + } + /** + * calculate the networth of the finacial_intermediary + * @return [description] + */ + double calculateNetworth() { + wealth = 0; + //in this case a financial intermediary + for (int i = 0; i < assets.size(); i++){ + if (getID == accounts[i].ID){ + wealth += accounts[i].value; + } + } + return wealth; + } +}; \ No newline at end of file diff --git a/trader.h b/trader.h new file mode 100644 index 0000000..8ff39c0 --- /dev/null +++ b/trader.h @@ -0,0 +1,68 @@ +#include "agent.h" +////////////// +// trader.h // +////////////// +/** + * + */ + +class trader: public agent { + public: + trader(int id) : agent(id) {} + void createPortfolio() { + for (int j = 0; j < assets.size(); j++){ //assign values to the portfolio + portfolio[j].type = "node"; + portfolio[j].ID = ID; + portfolio[j].title = assets[j]; + portfolio[j].value = utils::dRand(0.0,1.0) % 100; + wealth += + } + } + /** + * return total value of all assets held by trader + * @return total networth. + */ + double calculateNetworth() { //return total value of all assets held by trader + wealth = 0; + for (int i = 0; i < assets.size(); i++){ + if (getID == portfolio[i].ID){ + wealth += portfolio[i].value; + } + } + return wealth; + } + //function allowing an agent to deposit cash + void depositCash(agent* B, double amount){ + double agent_cash_init = portfolio[0].value; //keep track of initial values + double fin_cash_init = B->accounts[getID].value; + + if (agent_cash_init < amount) { //if the agent cannot deposit desired amount + cout << "Insufficient funds" << '\n'; //restrict it from going into the negative + portfolio[0].value = agent_cash_init; + B->accounts[getID].value = fin_cash_init; + } + else { + portfolio[0].value = agent_cash_init - amount; //subtract amount deposited from agent wealth + B->accounts[getID].value = fin_cash_init + amount; //add deposited amount to agent's account + } + + } + + //function allowing an agent to withdraw cash + void withdrawCash(agent* B, double amount){ + double agent_wealth_init = portfolio[0].value; //keep track of initial values + double fin_wealth_init = B->accounts[getID].value; + + if (fin_wealth_init <= amount) { //if the agent wants to withdraw more than it has stored + cout << "Insufficient funds" << '\n'; //restrict it from going into the negative + portfolio[0].value = agent_wealth_init; + B->accounts[getID].value = fin_wealth_init; + } + else{ + portfolio[0].value = agent_wealth_init + amount; //add amount withdrawn to agent wealth + B->accounts[getID].value = fin_wealth_init - amount; //subtract withdrawn amount from agent's account + } + + } + +}; \ No newline at end of file