An important form of Zipf\'s law is the citation patterns found in scientific ar
ID: 3801505 • Letter: A
Question
An important form of Zipf's law is the citation patterns found in scientific articles. Rather than doing equations, let's make a simulation of a simplified version of this. (a) Start with a list containing the integers 1 and 2. (b) Choose one number randomly from the list and append it back to the list. You can use the sample() function from the random module to do this. (c) Append the next number (3) to the list. (d) Repeat these last two steps 1,000 times, incrementing the new number each time until you get a list of 2,002 integers. Now, count up how many times each integer appears in the list and plot a rank-value plot on a log-log scale. Explain how we know that the plot exhibits a power-law scaling, and approximate the scaling exponent.Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
public class ZipfLaw {
public static void main(String[] args) {
//Initializing list and map of integer
List<Integer> list=new ArrayList<Integer>();
Map<Integer,Integer> map=new TreeMap<Integer,Integer>();
//Intially adding two numbers in list 1 and 2
list.add(1);
list.add(2);
Random rand = new Random();
int num;
//Getting random number bounded with value 2002 and appending in list
for(int i=0;i<1000;i++){
num=rand.nextInt(2002);
list.add(num);
}
//Initializing set of integer so for the purpose of removing duplicates from list
Set<Integer> set=new HashSet<Integer>(list);
int number=0;
int frequency=0;
Iterator<Integer> it=set.iterator();
//calculating frequency of the number and putting in TreeMap so it can be auto sorted in ascending order
while(it.hasNext()){
number=it.next();
frequency=Collections.frequency(list, number);
map.put(number, frequency);
}
//in map key is random number and value as frequency of the number
//Displaying key value pair of random number as key and its frequency as value
System.out.println(map);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.