#!/usr/bin/env python
import optparse
import os
import platform
import sys
import codecs
from gammaSimulation import *
import csv
from decimal import *
import numpy
from RandomIntVal import *
import pprint

def isNumeric(value):
	return str(value).replace('.','').strip().isdigit()

def fileExists(value):
    if os.path.isfile(os.path.expanduser(value.strip())):
        return os.path.abspath(os.path.expanduser(value.strip()))
    else:
        print "I can't find the file " + value
        sys.exit()

def isReturnFile(myfile):
	if os.path.abspath(os.path.expanduser(myfile.strip())) != False:
		return os.path.abspath(os.path.expanduser(myfile.strip()))
	else:
		print 'You can\'t save to that location'
		sys.exit()

def WriteFile(filename,criticalvalues, pvaluesList,data,herfCen=False):
	fieldList = ['NumberOfFirms','FirmSize','StDev','GammaMean','GammaMin','GammaMax','GammaStd','HerfindahlMean','HerfindahlMin','HerfindahlMax','HerfindahlStd','GValueMean','GValueMin','GValueMax','GValueStd','PValue']
	
	for x in range(len(criticalvalues)):
		fieldList.append("C" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHC" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHCL" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHC95" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHCL5" + str(criticalvalues[x]).replace('.','').strip())
		
	for x in range(len(pvaluesList)):
		fieldList.append("P" + str(pvaluesList[x]).replace('.','').strip())
		
	for x in range(len(criticalvalues)):
		fieldList.append("HC" + str(criticalvalues[x]).replace('.','').strip())

	for x in range(len(pvaluesList)):
		fieldList.append("HP" + str(pvaluesList[x]).replace('.','').strip())
	
	if herfCen == True:
		fieldList.append("SavedIterations")
		fieldList.append("TotalIterations")
	
	if os.path.isfile(filename) == False:
		mf = open(filename, 'wb')
		myfile = csv.writer(mf)
		myfile.writerow(fieldList)
		mf.close()
	
	myfile = open(filename,'ab+')
	WriteFile = csv.DictWriter(myfile,fieldList)
	WriteFile.writerow(data)
	myfile.close()
	print "Saving # of Firms: " + str(data['NumberOfFirms']) + ", Firm Size: " + str(data['FirmSize']) + ", StDev: " + str(data['StDev'])


def RunSimulation(rState, numberoffirmsList,firmsizeList,sdevList,trancheList,criticalvaluesList,loopsc,destination, twister, roundval, normaldist, pvaluesList, cMS, herfCen = False, herfLow=-1, herfHigh=-1):
	for x in range(len(numberoffirmsList)):
		for y in range(len(firmsizeList)):
			for z in range(len(sdevList)):
				resultDic = {}
				if float(sdevList[z]) == float(0) or float(firmsizeList[y]) == float(0):
					stsend = 0
				else:
					stsend = float(firmsizeList[y]*float(sdevList[z]))
				cGS = gammaSimulation(rState, firmsizeList[y], stsend, int(numberoffirmsList[x]), trancheList, criticalvaluesList, loopsc, twister, roundval, normaldist, pvaluesList, cMS, herfCen, herfLow, herfHigh)
				gamma = cGS.getGamma()
				herfindahl = cGS.getHerfindahl()
				gValue = cGS.getGValue()
				del cGS
				resultDic['NumberOfFirms'] = numberoffirmsList[x]
				resultDic['FirmSize'] = firmsizeList[y]
				resultDic['StDev'] = sdevList[z]
				
				resultDic['GammaMean'] = gamma['mean']
				resultDic['GammaMin'] = gamma['min']
				resultDic['GammaMax'] = gamma['max']
				resultDic['GammaStd'] = gamma['std']
				
				
				resultDic['HerfindahlMean'] = herfindahl['mean']
				resultDic['HerfindahlMin'] = herfindahl['min']
				resultDic['HerfindahlMax'] = herfindahl['max']
				resultDic['HerfindahlStd'] = herfindahl['std']
				
				resultDic['GValueMean'] = gValue['mean']
				resultDic['GValueMin'] = gValue['min']
				resultDic['GValueMax'] = gValue['max']
				resultDic['GValueStd'] = gValue['std']
				
				
				resultDic['PValue'] = gamma['pvalue']
				
				
				
				lcv = gamma['criticalValues']
				
				for cv in range(len(criticalvaluesList)):
					key = "C" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['hCritical']
				
					
				for cv in range(len(criticalvaluesList)):
					key = "GHC" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
					
				lcv = gamma['hCriticalLow']
					
				for cv in range(len(criticalvaluesList)):
					key = "GHCL" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['hCriticals']
				
					
				for cv in range(len(criticalvaluesList)):
					key = "GHC95" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
					
				lcv = gamma['hCriticalLows']
					
				for cv in range(len(criticalvaluesList)):
					key = "GHCL5" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				

				lcv = herfindahl['criticalValues']
				
				for cv in range(len(criticalvaluesList)):
					key = "HC" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['pValues']

				for cv in range(len(pvaluesList)):
					key = "P" + str(pvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = herfindahl['pValues']
				for cv in range(len(pvaluesList)):
					key = "HP" + str(pvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]	
				
				if herfCen == True:
					resultDic['SavedIterations'] = gValue['loops']
					resultDic['TotalIterations'] = gValue['totalloops']
				
				WriteFile(destination,criticalvaluesList, pvaluesList,resultDic,herfCen)
				
 
def loadFile(value):
	empty_data = []
	with open(value.strip(), 'rU') as f:
		read_data = f.readlines()
	
	for x in range(len(read_data)):
		if isNumeric(read_data[x].strip()):
			empty_data.append(float(read_data[x].strip()))
			
	return empty_data

def main():
	desc = 'Tool to simulate EG statistic and Herfindahl values'
	p = optparse.OptionParser(description=desc)
	p.add_option('--tranchefile', '-t', dest="tranche", help="File containing geographic tranches", default='', metavar='"<File Path>"')
	p.add_option('--criticalvalues', '-c', dest="criticalvalues", help="File containing critical values to test", default='', metavar='"<File Path>"')
	p.add_option('--pvalues',dest="pvalues", help="File containing p values to test", default='', metavar='"<File Path>"')
	p.add_option('--firmsize', '-f', dest="firmsize", help="File containing firm size (head count)", default='', metavar='"<File Path>"')
	p.add_option('--sdev', '-s', dest="sdev", help="File containing the standard deviations to test", default='', metavar='"<File Path>"')
	p.add_option('--numberoffirms', '-n', dest="numberoffirms", help="File containing the number of firms (in an industry) to test", default='', metavar='"<File Path>"')
	p.add_option('--iterations', '-i', type="int", dest="iterations", help="Number of iterations to run for each simulation", default=1000)
	p.add_option('--destination', '-d', dest="destination", help="Main csv file to save simulation(s) output", default='', metavar='"<File Path>"')
	p.add_option("--twister", action="store_true", dest="twister", default=False, help="Use mersenne twister for random number generation instead of fortuna")
	p.add_option("--roundfirmsize", action="store_true", dest="roundval", default=False, help="Round firm size to closest integer")
	p.add_option("--roundfirmsizedown", action="store_true", dest="roundvaldown", default=False, help="Round firm size down to closest integer")
	p.add_option("--roundfirmsizeup", action="store_true", dest="roundvalup", default=False, help="Round firm size up to closest integer")
	p.add_option("--roundfirmsizetenths", action="store_true", dest="roundvaltenths", default=False, help="Round firm size to nearest tenth")
	p.add_option("--seed", type="int", dest="seed", default=1012810, help="Seed the random generator with a specified value")
	p.add_option("--normal", action="store_true", dest="normaldist", default=False, help="Normal distributed firm sizes instead of log normal")
	p.add_option("--maurel", action="store_true", dest="cMS", default=False, help="Use Maurel and Sedillot (1999)'s value of G instead of EG")
	p.add_option("--HerfCensuredLow", type="float", dest="HerfCensuredLow", default=-1.0, help="Toss any simulated result where the Herfindahl is below this value")
	p.add_option("--HerfCensuredHigh", type="float", dest="HerfCensuredHigh", default=-1.0, help="Toss any simulated result where the Herfindahl is above this value")
	
	(options, arguments) = p.parse_args();
	herfRange = False
	if options.cMS == True:
		cMS = True
	else:
		cMS = False
	
	
	if options.HerfCensuredLow>=0 and options.HerfCensuredHigh>options.HerfCensuredLow:
		herfRange = True
	if (options.HerfCensuredLow>-1 or options.HerfCensuredLow>-1) and herfRange==False:
		print 'Sorry, you must specify a censored range such that the low Herfindahl value is less than the high Herfindahl value.  We highly suggest you run a uncensored simulation to find out where the Herfindahl values are likely to be.'
		sys.exit()
		
	if len(options.destination)>0:
		destination = isReturnFile(options.destination.strip())
	else:
		print 'You must specify a destination file'
		sys.exit()
	
	if int(options.iterations)<=0 or int(options.seed)<0:
		print 'You must specify a positive value for both iterations and seeding the random number generator'
		sys.exit()
	
	pvalues = []
	if len(options.pvalues)>0:
		pvaluesfile = fileExists(options.pvalues)
		pvalues = loadFile(pvaluesfile)
		
	
	if len(options.tranche)>0 and len(options.criticalvalues)>0 and len(options.firmsize)>0 and len(options.numberoffirms)>0 and len(options.sdev)>0:
		tranchefile = fileExists(options.tranche)
		criticalvaluesfile = fileExists(options.criticalvalues)
		firmsizefile = fileExists(options.firmsize)
		numberoffirmsfile = fileExists(options.numberoffirms)
		sdevfile = fileExists(options.sdev)
		
		trancheList = loadFile(tranchefile)
		criticalvaluesList = loadFile(criticalvaluesfile)
		firmsizeList = loadFile(firmsizefile)
		numberoffirmsList = loadFile(numberoffirmsfile)
		sdevList = loadFile(sdevfile)
		roundval = False
		
		if options.roundvaldown == True:
			roundval = 'floor'
		elif options.roundvalup == True:
			roundval = 'ceil'
		elif options.roundvaltenths == True:
			roundval = 'tenths'
		elif options.roundval == True:
			roundval = True
		
		rState = RandomIntVal(int(options.seed))
		RunSimulation(rState,numberoffirmsList,firmsizeList,sdevList,trancheList,criticalvaluesList,int(options.iterations),destination,options.twister,roundval,options.normaldist,pvalues, cMS, herfRange, options.HerfCensuredLow, options.HerfCensuredHigh)
		del rState
	else:
		print 'You must specify files for tranche, critical values, firm size, number of firms, standard deviation'
		sys.exit()	

if __name__ == '__main__':
    main()
a6869b0a55e6b9196469c5510d0881918252e5b3
#!/usr/bin/env python
import optparse
import os
import platform
import sys
import codecs
from gammaSimulation import *
import csv
from decimal import *
import numpy
from RandomIntVal import *
import pprint

def isNumeric(value):
	return str(value).replace('.','').strip().isdigit()

def fileExists(value):
    if os.path.isfile(os.path.expanduser(value.strip())):
        return os.path.abspath(os.path.expanduser(value.strip()))
    else:
        print "I can't find the file " + value
        sys.exit()

def isReturnFile(myfile):
	if os.path.abspath(os.path.expanduser(myfile.strip())) != False:
		return os.path.abspath(os.path.expanduser(myfile.strip()))
	else:
		print 'You can\'t save to that location'
		sys.exit()

def WriteFile(filename,criticalvalues, pvaluesList,data,herfCen=False):
	fieldList = ['NumberOfFirms','FirmSize','StDev','GammaMean','GammaMin','GammaMax','GammaStd','HerfindahlMean','HerfindahlMin','HerfindahlMax','HerfindahlStd','GValueMean','GValueMin','GValueMax','GValueStd','PValue']
	
	for x in range(len(criticalvalues)):
		fieldList.append("C" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHC" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHCL" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHC95" + str(criticalvalues[x]).replace('.','').strip())
	
	for x in range(len(criticalvalues)):
		fieldList.append("GHCL5" + str(criticalvalues[x]).replace('.','').strip())
		
	for x in range(len(pvaluesList)):
		fieldList.append("P" + str(pvaluesList[x]).replace('.','').strip())
		
	for x in range(len(criticalvalues)):
		fieldList.append("HC" + str(criticalvalues[x]).replace('.','').strip())

	for x in range(len(pvaluesList)):
		fieldList.append("HP" + str(pvaluesList[x]).replace('.','').strip())
	
	if herfCen == True:
		fieldList.append("SavedIterations")
		fieldList.append("TotalIterations")
	
	if os.path.isfile(filename) == False:
		mf = open(filename, 'wb')
		myfile = csv.writer(mf)
		myfile.writerow(fieldList)
		mf.close()
	
	myfile = open(filename,'ab+')
	WriteFile = csv.DictWriter(myfile,fieldList)
	WriteFile.writerow(data)
	myfile.close()
	print "Saving # of Firms: " + str(data['NumberOfFirms']) + ", Firm Size: " + str(data['FirmSize']) + ", StDev: " + str(data['StDev'])


def RunSimulation(rState, numberoffirmsList,firmsizeList,sdevList,trancheList,criticalvaluesList,loopsc,destination, twister, roundval, normaldist, pvaluesList, cMS, herfCen = False, herfLow=-1, herfHigh=-1):
	for x in range(len(numberoffirmsList)):
		for y in range(len(firmsizeList)):
			for z in range(len(sdevList)):
				resultDic = {}
				if float(sdevList[z]) == float(0) or float(firmsizeList[y]) == float(0):
					stsend = 0
				else:
					stsend = float(firmsizeList[y]*float(sdevList[z]))
				cGS = gammaSimulation(rState, firmsizeList[y], stsend, int(numberoffirmsList[x]), trancheList, criticalvaluesList, loopsc, twister, roundval, normaldist, pvaluesList, cMS, herfCen, herfLow, herfHigh)
				gamma = cGS.getGamma()
				herfindahl = cGS.getHerfindahl()
				gValue = cGS.getGValue()
				del cGS
				resultDic['NumberOfFirms'] = numberoffirmsList[x]
				resultDic['FirmSize'] = firmsizeList[y]
				resultDic['StDev'] = sdevList[z]
				
				resultDic['GammaMean'] = gamma['mean']
				resultDic['GammaMin'] = gamma['min']
				resultDic['GammaMax'] = gamma['max']
				resultDic['GammaStd'] = gamma['std']
				
				
				resultDic['HerfindahlMean'] = herfindahl['mean']
				resultDic['HerfindahlMin'] = herfindahl['min']
				resultDic['HerfindahlMax'] = herfindahl['max']
				resultDic['HerfindahlStd'] = herfindahl['std']
				
				resultDic['GValueMean'] = gValue['mean']
				resultDic['GValueMin'] = gValue['min']
				resultDic['GValueMax'] = gValue['max']
				resultDic['GValueStd'] = gValue['std']
				
				
				resultDic['PValue'] = gamma['pvalue']
				
				
				
				lcv = gamma['criticalValues']
				
				for cv in range(len(criticalvaluesList)):
					key = "C" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['hCritical']
				
					
				for cv in range(len(criticalvaluesList)):
					key = "GHC" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
					
				lcv = gamma['hCriticalLow']
					
				for cv in range(len(criticalvaluesList)):
					key = "GHCL" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['hCriticals']
				
					
				for cv in range(len(criticalvaluesList)):
					key = "GHC95" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
					
				lcv = gamma['hCriticalLows']
					
				for cv in range(len(criticalvaluesList)):
					key = "GHCL5" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				

				lcv = herfindahl['criticalValues']
				
				for cv in range(len(criticalvaluesList)):
					key = "HC" + str(criticalvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = gamma['pValues']

				for cv in range(len(pvaluesList)):
					key = "P" + str(pvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]
				
				lcv = herfindahl['pValues']
				for cv in range(len(pvaluesList)):
					key = "HP" + str(pvaluesList[cv]).replace('.','').strip()
					resultDic[key] = lcv[cv]	
				
				if herfCen == True:
					resultDic['SavedIterations'] = gValue['loops']
					resultDic['TotalIterations'] = gValue['totalloops']
				
				WriteFile(destination,criticalvaluesList, pvaluesList,resultDic,herfCen)
				
 
def loadFile(value):
	empty_data = []
	with open(value.strip(), 'rU') as f:
		read_data = f.readlines()
	
	for x in range(len(read_data)):
		if isNumeric(read_data[x].strip()):
			empty_data.append(float(read_data[x].strip()))
			
	return empty_data

def main():
	desc = 'Tool to simulate EG statistic and Herfindahl values'
	p = optparse.OptionParser(description=desc)
	p.add_option('--tranchefile', '-t', dest="tranche", help="File containing geographic tranches", default='', metavar='"<File Path>"')
	p.add_option('--criticalvalues', '-c', dest="criticalvalues", help="File containing critical values to test", default='', metavar='"<File Path>"')
	p.add_option('--pvalues',dest="pvalues", help="File containing p values to test", default='', metavar='"<File Path>"')
	p.add_option('--firmsize', '-f', dest="firmsize", help="File containing firm size (head count)", default='', metavar='"<File Path>"')
	p.add_option('--sdev', '-s', dest="sdev", help="File containing the standard deviations to test", default='', metavar='"<File Path>"')
	p.add_option('--numberoffirms', '-n', dest="numberoffirms", help="File containing the number of firms (in an industry) to test", default='', metavar='"<File Path>"')
	p.add_option('--iterations', '-i', type="int", dest="iterations", help="Number of iterations to run for each simulation", default=1000)
	p.add_option('--destination', '-d', dest="destination", help="Main csv file to save simulation(s) output", default='', metavar='"<File Path>"')
	p.add_option("--twister", action="store_true", dest="twister", default=False, help="Use mersenne twister for random number generation instead of fortuna")
	p.add_option("--roundfirmsize", action="store_true", dest="roundval", default=False, help="Round firm size to closest integer")
	p.add_option("--roundfirmsizedown", action="store_true", dest="roundvaldown", default=False, help="Round firm size down to closest integer")
	p.add_option("--roundfirmsizeup", action="store_true", dest="roundvalup", default=False, help="Round firm size up to closest integer")
	p.add_option("--roundfirmsizetenths", action="store_true", dest="roundvaltenths", default=False, help="Round firm size to nearest tenth")
	p.add_option("--seed", type="int", dest="seed", default=1012810, help="Seed the random generator with a specified value")
	p.add_option("--normal", action="store_true", dest="normaldist", default=False, help="Normal distributed firm sizes instead of log normal")
	p.add_option("--maurel", action="store_true", dest="cMS", default=False, help="Use Maurel and Sedillot (1999)'s value of G instead of EG")
	p.add_option("--HerfCensuredLow", type="float", dest="HerfCensuredLow", default=-1.0, help="Toss any simulated result where the Herfindahl is below this value")
	p.add_option("--HerfCensuredHigh", type="float", dest="HerfCensuredHigh", default=-1.0, help="Toss any simulated result where the Herfindahl is above this value")
	
	(options, arguments) = p.parse_args();
	herfRange = False
	if options.cMS == True:
		cMS = True
	else:
		cMS = False
	
	
	if options.HerfCensuredLow>=0 and options.HerfCensuredHigh>options.HerfCensuredLow:
		herfRange = True
	if (options.HerfCensuredLow>-1 or options.HerfCensuredLow>-1) and herfRange==False:
		print 'Sorry, you must specify a censored range such that the low Herfindahl value is less than the high Herfindahl value.  We highly suggest you run a uncensored simulation to find out where the Herfindahl values are likely to be.'
		sys.exit()
		
	if len(options.destination)>0:
		destination = isReturnFile(options.destination.strip())
	else:
		print 'You must specify a destination file'
		sys.exit()
	
	if int(options.iterations)<=0 or int(options.seed)<0:
		print 'You must specify a positive value for both iterations and seeding the random number generator'
		sys.exit()
	
	pvalues = []
	if len(options.pvalues)>0:
		pvaluesfile = fileExists(options.pvalues)
		pvalues = loadFile(pvaluesfile)
		
	
	if len(options.tranche)>0 and len(options.criticalvalues)>0 and len(options.firmsize)>0 and len(options.numberoffirms)>0 and len(options.sdev)>0:
		tranchefile = fileExists(options.tranche)
		criticalvaluesfile = fileExists(options.criticalvalues)
		firmsizefile = fileExists(options.firmsize)
		numberoffirmsfile = fileExists(options.numberoffirms)
		sdevfile = fileExists(options.sdev)
		
		trancheList = loadFile(tranchefile)
		criticalvaluesList = loadFile(criticalvaluesfile)
		firmsizeList = loadFile(firmsizefile)
		numberoffirmsList = loadFile(numberoffirmsfile)
		sdevList = loadFile(sdevfile)
		roundval = False
		
		if options.roundvaldown == True:
			roundval = 'floor'
		elif options.roundvalup == True:
			roundval = 'ceil'
		elif options.roundvaltenths == True:
			roundval = 'tenths'
		elif options.roundval == True:
			roundval = True
		
		rState = RandomIntVal(int(options.seed))
		RunSimulation(rState,numberoffirmsList,firmsizeList,sdevList,trancheList,criticalvaluesList,int(options.iterations),destination,options.twister,roundval,options.normaldist,pvalues, cMS, herfRange, options.HerfCensuredLow, options.HerfCensuredHigh)
		del rState
	else:
		print 'You must specify files for tranche, critical values, firm size, number of firms, standard deviation'
		sys.exit()	

if __name__ == '__main__':
    main()