-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuction.java
More file actions
108 lines (76 loc) · 2.29 KB
/
Auction.java
File metadata and controls
108 lines (76 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package bid;
public class Auction {
public static double maxBidder(double[] valuations) {
double privateVal = 0;
int winner = 0;
for(int i=0; i<valuations.length; i++) {
if(valuations[i] >= privateVal) {
privateVal = valuations[i];
winner = i;
}
}
System.out.println("Max bidder is " + winner + " of " + privateVal);
return privateVal;
}
public static double secondHighest(double privateVal, double[] valuations) {
double max = privateVal;
double secondMax = 0;
int loc = 0;
for(int i=0; i<valuations.length; i++) {
if(valuations[i] > secondMax && valuations[i] < max) {
secondMax = valuations[i];
loc = i;
}
}
System.out.println("Second highest (and revenue) is " + loc + " of " + secondMax);
return secondMax;
}
public static double[] randPrivateVal() {
// each index represents one bidder
double[] bids = new double[5];
// each bidder's private valuation generated
// independently over [0,10]
for(int i=0; i<bids.length; i++) {
double valuation = roundDec(Math.random()*11);
bids[i] = valuation;
}
return bids;
}
public static double bidderSurplus(double privateVal, double secondHighest) {
double bidSurplus = privateVal - secondHighest;
bidSurplus = roundDec(bidSurplus);
System.out.println("Bid surplus " + bidSurplus);
return bidSurplus;
}
public static void toString(double[] bids) {
String str = "[";
for(int i=0; i<bids.length-1; i++) {
str += bids[i] + ", ";
}
str += bids[bids.length-1] + "]";
System.out.println(str);
}
public static double roundDec(double num) {
num = (double) Math.round(num*100)/100;
return num;
}
public static void main(String[] args) {
double bidderSurplus = 0;
double revenue = 0;
for(int i=0; i<500; i++) {
System.out.println("Simulation " + i);
double[] test = randPrivateVal();
toString(test);
double findMax = maxBidder(test);
double secondH = secondHighest(findMax, test);
double surplus = bidderSurplus(findMax, secondH);
bidderSurplus += surplus;
revenue += secondH;
System.out.println("");
}
bidderSurplus = roundDec((bidderSurplus/500));
revenue = roundDec((revenue/500));
System.out.println("Avgerage total bidder surplus " + bidderSurplus);
System.out.println("Average total seller revenue " + revenue);
}
}