Budujemy model Stiglitz-Honig-Cohen’a Part 2
-
Upload
hayley-ryan -
Category
Documents
-
view
32 -
download
1
description
Transcript of Budujemy model Stiglitz-Honig-Cohen’a Part 2
Budujemy model Stiglitz-Honig-Cohen’a
Part 2
Joanna Tyrowicz
Tomasz Michalak
26.10.2009
www.ua.ac.be/tomasz.michalak
Kilka użytecznych funckji w MATLABie (1) Save i load
(a) Aby zapisać zmienną lub zestaw zmiennych w pliku piszemy:
save NAZWA_PLIKU zmienna1 zmienna2 ... zmiennaN
(b) Aby dodać dodatkowe zmienne do NAZWA_PLIKU piszemy:
save NAZWA_PLIKU zmiennaX zmiennaY ... zmiennaZ –append
(c) Aby zapisać wszystkie zmienne jakie znajdują się w pamięci:
save NAZWA_PLIKU
(d) Aby odczytać zmienną lub zestaw zmiennych z pliku piszemy:
load NAZWA_PLIKU zmienna1 zmienna2 ... zmiennaNlubload NAZWA_PLIKU
Kilka użytecznych funckji w MATLABie (2)
Mnożenie wektorów i macierzy element razy element:
v1 = [1 2 3];
v2 = [3 4 5];
v1.*v2 = [3 8 15]
Wyświetlanie napisów w Command Window:
disp(‘Napis’);
i = 10;
disp([‘zysk = ‘ num2str(i)]);
Kilka użytecznych funckji w MATLABie (3)
Sumowanie elementów wektora:
sum(v)
sum([5 6 7]) = 18; Znajdowanie maximum i minimum
max([5 6 7]) = 7;
min([5 6 7]) = 5; Przeniesienie linii która jest za długa:
...
ETAPY TWORZENIA SYMULACJI MAS
1. Zrozumienie modelu teoretycznego
2. Zaprogramowania modelu, ktory będzie ewoluował w czasie:
(a) Inicjalizacja głównych zmiennych (ilość agentów, ich zdolności,
parametry funckji użyteczności, ilość okresów, tzn tyle ile chcemy aby
model ewoluował itp).
(b) Główna pętla modelu
(c) Funkcje pozwalające na analizę wyników
3. Kalibracja/estymacja i empiryczna walidacja modelu
Pętla Główna
Główna pętla jest miejscem w softwarze, w którym zaczynają się schody.
Niezwykle trudno jest napisać całą procedurę głównej pętli za jednym zamachem tak aby wszystko od razu funkcjonowało.
Najlepszą strategią jest pisanie software po kawałku tworząc funkcje raportujące i porównujące raporty.
Zaprogramujemy:• Produkcje i konsumpcje;• handel;
Proste
Mniej Proste
Postać Pętli Głównej
for period = 1 : number_of_periods
%Consumption and Production
...
%Trading
...
end
Nagrywanie danych wejściowych do pętliPoczątek programuInicjalizacja zmiennych%uploadujemy dane wejscioweload dane noa GOLDS FOODS RESERVE UFUN FOODI GOLDI; %We will need matrix BIDS to store bids for all agents during a dayBIDS = zeros(noa,1);%Matrix showing who wants to buy%1. one if buy; 2. price 3. volume 4. index of agentBUY = zeros(noa,4);%Matrix showing who wants to sellSELL = zeros(noa,4);BUYcumul = zeros(noa,1); %skumulowany popytSELLcumul = zeros(noa,1); %skumulowana sprzedażPt_1 = 1; %Ustalona cena poczatkowa na rynkufor period = 1 : number_of_periods
....end
rynekB01.m
Raportowanie tekstowe w Command Window (1)
....%MAIN STATISTICS PRICEoT = zeros(1,number_of_periods);PRICEoT(1) = Pt_1; disp(' ');disp('Initial endowments:');disp(['Gold: ' num2str(sum(GOLDI))]);disp(['Food: ' num2str(sum(FOODI))]);for period = 1 : number_of_periods
....end
Raportowanie tekstowe w Command Window (2)
% -------------------------------------------------------------------% Multi-Agent Simulations of Steiglitz, Honig and% Cohen Model by X Y% -------------------------------------------------------------------clc;clear;close all;comments = 1;...for period = 1 : number_of_periods if comments disp(' '); disp(['Period ' num2str(period) ' ---------------------']); endend
rynekC01.m
Konsumpcja i produkcja:%Production and consumption--------------------------------------------%Production Loop%Variables to gather basic statisticsgold_produced = 0;food_produced = 0; for agent = 1 : noa %Agents consume every day one unit of food if FOODI(agent) >= 1 FOODI(agent) = FOODI(agent) - 1; end %Every agent makes a decision to produce if GOLDS(agent) > Pt_1*FOODS(agent) %agent wants to produce gold GOLDI(agent) = GOLDI(agent) + GOLDS(agent); gold_produced = gold_produced + GOLDS(agent); %statistics else FOODI(agent) = FOODI(agent) + FOODS(agent); %agent wants to prod food food_produced = food_produced + FOODS(agent); %statistics endend
Dodajemy komentarz po produkcji
for period = 1 : 1 %number_of_periods
%Production and consumption
....
if comments
disp('Production:');
disp(['Gold: ' num2str(gold_produced)]);
disp(['Food: ' num2str(food_produced)]);
end
end
rynekD01.m
Macierze BUY i SELL for agent = 1 : noa %we check what the bid would be BIDS(agent) = utility_function(agent,Pt,GOLDI(agent),RESERVE(agent),... UFUN(agent,1),UFUN(agent,2),UFUN(agent,3),FOODI(agent)); %we check whether an agent wants to sell or buy if FOODI(agent) < RESERVE(agent) BUY(agent,1) = 1; BUY(agent,2) = BIDS(agent); BUY(agent,3) = RESERVE(agent) - FOODI(agent); BUY(agent,4) = agent; else SELL(agent,1) = 1; SELL(agent,2) = BIDS(agent); SELL(agent,3) = -(RESERVE(agent) - FOODI(agent)); SELL(agent,4) = agent; end end
rynekE01.m
Handel – raportowanie początkowe
%TRADING********************************************************
%before we trade let's check how much money and food we have
if comments
disp('Before trade:');
disp(['Gold: ' num2str(sum(GOLDI))]);
disp(['Food: ' num2str(sum(FOODI))]);
end
%we also want to see food reserve on the graph
if reserve_balance_figure
figure_foodibar = figure;
bar(FOODI-RESERVE);
end
Dodać na sam początek
wartośc 0 lub 1
Demand and Supply Volumes SELLsorted = sortrows(SELL,2); BUYsorted = sortrows(BUY,2); SELLcumul = zeros(noa,1); BUYcumul = zeros(noa,1); SELLcumul(1) = SELLsorted(1,3); BUYcumul(noa) = BUYsorted(noa,3); for i = noa-1 : -1 : 1 BUYcumul(i) = BUYcumul(i+1)+BUYsorted(i,3); end for i = 2 : noa SELLcumul(i) = SELLcumul(i-1)+SELLsorted(i,3);end if demand_suply_figure figure plot(SELLsorted(:,2),SELLcumul) hold on plot(BUYsorted(:,2),BUYcumul) end
rynekF01.m
Demand and Supply Volumes
0 0.5 1 1.5 2 2.5 30
1000
2000
3000
4000
5000
6000
7000
8000
Demand Volume
Supply Volume
E
Eq. Price
Eq
. A
mo
un
t
Znajdowanie Eq. Price (total_buy < total_sell) (1)
total_buy = sum(BUY(:,3)); total_sell = sum(SELL(:,3)); special_case = 0; if total_buy < total_sell %find minimum price agents want to buy for equilibrium_price = max(BUY(:,2)); equilibrium_amount = total_buy; for i = 1 : noa if BUY(i,2) > 0 & BUY(i,2) < equilibrium_price equilibrium_price = BUY(i,2); end end
. if equilibrium_price == 0 special_case = 1; else ....
else....end
teraz jesteśmy tutaj
i przechodzimy do kolejnego slajdu
Znajdowanie Eq. Price (total_buy < total_sell) (2)
for i = 1 : noa if SELLcumul(i) > equilibrium_amount break; end end equilibrium_sell_index = i; %let's clear the market now sold = 0; bought = 0; for i = 1 : noa if BUYsorted(i,2) >= equilibrium_price agent = BUYsorted(i,4); bought = bought + BUY(agent,3); %increase inventory FOODI(agent) = FOODI(agent) + BUY(agent,3); %now transfer money from the buyers GOLDI(agent) = GOLDI(agent) - BUY(agent,3)*equilibrium_price; end end
Znajdowanie Eq. Price (total_buy < total_sell) (3)k = 1;while SELLsorted(k,4) == 0 k = k + 1;endfor i = k : equilibrium_sell_index-1 agent = SELLsorted(i,4); %let's sell stock of the agent sold = sold + SELL(agent,3); %decrease reserve FOODI(agent) = FOODI(agent) - SELL(agent,3); %now transfer money to the seller GOLDI(agent) = GOLDI(agent) + SELL(agent,3)*equilibrium_price; endif sold < bought agent = SELLsorted(equilibrium_sell_index,4); FOODI(agent) = FOODI(agent) -(bought-sold); GOLDI(agent) = GOLDI(agent)+(bought-sold)*equilibrium_price; sold = bought;else equilibrium_sell_index = equilibrium_sell_index-1;end