1 / 22

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

hayley-ryan
Download Presentation

Budujemy model Stiglitz-Honig-Cohen’a Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Budujemy model Stiglitz-Honig-Cohen’aPart 2 Joanna Tyrowicz Tomasz Michalak 26.10.2009 www.ua.ac.be/tomasz.michalak

  2. Kilka użytecznych funckji w MATLABie (1) • Save i load (a) Aby zapisać zmienną lub zestaw zmiennych w pliku piszemy: saveNAZWA_PLIKU zmienna1 zmienna2 ... zmiennaN (b) Aby dodać dodatkowe zmienne do NAZWA_PLIKU piszemy: saveNAZWA_PLIKU zmiennaX zmiennaY ... zmiennaZ –append (c) Aby zapisać wszystkie zmienne jakie znajdują się w pamięci: saveNAZWA_PLIKU (d) Aby odczytać zmienną lub zestaw zmiennych z pliku piszemy: loadNAZWA_PLIKU zmienna1 zmienna2 ... zmiennaN lub loadNAZWA_PLIKU

  3. 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)]);

  4. 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: ...

  5. ETAPY TWORZENIA SYMULACJI MAS • Zrozumienie modelu teoretycznego • Zaprogramowania modelu, ktory będzie ewoluował w czasie: • 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). • Główna pętla modelu • Funkcje pozwalające na analizę wyników • Kalibracja/estymacja i empiryczna walidacja modelu

  6. ETAPY TWORZENIA SYMULACJI MAS Ostatnio zajmowaliśmy się tym Dzis zajmiemy sie tym

  7. 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 

  8. Postać Pętli Głównej forperiod = 1 : number_of_periods %Consumption and Production ... %Trading ... end

  9. Nagrywanie danych wejściowych do pętli Początek programu Inicjalizacja zmiennych %uploadujemy dane wejsciowe load dane noa GOLDS FOODS RESERVE UFUN FOODI GOLDI; %We will need matrix BIDS to store bids for all agents during a day BIDS = zeros(noa,1); %Matrix showing who wants to buy %1. one if buy; 2. price 3. volume 4. index of agent BUY = zeros(noa,4); %Matrix showing who wants to sell SELL = zeros(noa,4); BUYcumul = zeros(noa,1); %skumulowany popyt SELLcumul = zeros(noa,1); %skumulowana sprzedaż Pt_1 = 1; %Ustalona cena poczatkowa na rynku forperiod = 1 : number_of_periods .... end rynekB01.m

  10. 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))]); forperiod = 1 : number_of_periods .... end

  11. Raportowanie tekstowe w Command Window (2) % ------------------------------------------------------------------- % Multi-Agent Simulations of Steiglitz, Honig and % Cohen Model by X Y % ------------------------------------------------------------------- clc; clear; close all; comments = 1; ... forperiod = 1 : number_of_periods if comments disp(' '); disp(['Period ' num2str(period) ' ---------------------']); end end rynekC01.m

  12. Konsumpcja i produkcja: %Production and consumption-------------------------------------------- %Production Loop %Variables to gather basic statistics gold_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 end end

  13. Dodajemy komentarz po produkcji forperiod = 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

  14. Aukcja do zaprogramowania

  15. 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

  16. 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

  17. Niezbalansowanie Żywności

  18. 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

  19. Demand and Supply Volumes E Demand Volume Eq. Amount Supply Volume Eq. Price

  20. 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

  21. 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

  22. Znajdowanie Eq. Price (total_buy < total_sell) (3) k = 1; while SELLsorted(k,4) == 0 k = k + 1; end for 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; end if 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

More Related