#python convergence_loss_times_origion.py.txt 1000 20 30 10 10 5# 1000 times permutation, 20pair data, 20 polarization,30 lossing proportion, 10 mixing  proportion, 5 times per combination (must more than 0);
import sys
import scipy.stats as st
import numpy as np
import random
str1=int(sys.argv[1])
str2=int(sys.argv[2])-1
str3u=(100+float(sys.argv[3]))/100
str3d=(100-float(sys.argv[3]))/100
str4=int(sys.argv[4])
str5=int(sys.argv[5])
str6=int(sys.argv[6])+1
#100 times permutation
for iii in range (0,str1):
 pall=[]
 xbarall=[]
 xmall=[]
 palls=[]
 xbaralls=[]
 xmalls=[]
 result=[]
###
 freall=[]
 frealls=[]
 combineall=[]
 combinealls=[]

 combineall_c=[]
 combineall_cs=[]
 combineall_c_l=[]
 combineall_c_ls=[]

#5 times per combination,generate first population
 p0=0
 combine0=[]

###
 fres=[]
 combines = []
 combines_c = []
 combines_c_l = []


 xbar0=xm0=0
 createVar = locals()
 for x in range(1,str6):
  createVar["p04"+str(x)]=1
  createVar["p05"+str(x)]=1
  createVar["p06"+str(x)]=1
  createVar["four0"+str(x)]=[]
  createVar["five0"+str(x)]=[]
  createVar["six0"+str(x)]=[]
  
  for num0 in range(1,5):
   f0=random.random()
   createVar["p04"+str(x)]=createVar["p04"+str(x)]*f0
   createVar["four0"+str(x)].append (f0)
####
   fres.append(str(f0)) 
  p0=p0+createVar["p04"+str(x)]
  for num0 in range(1,6):
   f0=random.random()
   createVar["p05"+str(x)]=createVar["p05"+str(x)]*f0
   createVar["five0"+str(x)].append (f0)
####
   fres.append(str(f0))
  p0=p0+createVar["p05"+str(x)]
  for num0 in range(1,7):
   f0=random.random()
   createVar["p06"+str(x)]=createVar["p06"+str(x)]*f0
   createVar["six0"+str(x)].append (f0)
####
   fres.append(str(f0))
  p0=p0+createVar["p06"+str(x)]

  for i in range(0,4):
   for j in range(0,4):
    for k in range(0,4):
     if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
      fc=createVar["four0"+str(x)][i]*createVar["four0"+str(x)][j]*createVar["four0"+str(x)][k]
      combine0.append (fc)

  for i in range(0,5):
   for j in range(0,5):
    for k in range(0,5):
     if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
      fc=createVar["five0"+str(x)][i]*createVar["five0"+str(x)][j]*createVar["five0"+str(x)][k]
      combine0.append (fc)

  for i in range(0,6):
   for j in range(0,6):
    for k in range(0,6):
     if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
      fc=createVar["six0"+str(x)][i]*createVar["six0"+str(x)][j]*createVar["six0"+str(x)][k]
      combine0.append (fc)

 pd=p0*0.01
 pall.append(p0)
 palls.append(str(p0))
 left=int(len(combine0)*(100-str4)/100)
 mix=int(len(combine0)*str5/100)


 combine0_index=range(0,len(combine0),1)
 random.shuffle(combine0_index)
 combine_sample0_index=combine0_index[0:left]
 sort_combine_sample0_index = sorted(combine_sample0_index,reverse=False)

 combine_sample0=[]
 for index in sort_combine_sample0_index:
  combine_sample0.append(combine0[index])

 mixlist0=[]
 for xx in range(0,mix):
  mixlist0.append(random.uniform(pd,p0))
 combine_sample_mix0=combine_sample0 + mixlist0
###

 for xx in combine0:
  combines_c.append(str(xx))
 for yy in combine_sample0:
  combines_c_l.append(str(yy))

 for zz in combine_sample_mix0:
  combines.append(str(zz))


 xbar0=np.mean(combine_sample_mix0)
 xm0=np.median(combine_sample_mix0)
 xbarall.append(xbar0)
 xmall.append(xm0)
 xbaralls.append(str(xbar0))
 xmalls.append(str(xm0))

###
 aa = ','
 frelist=aa.join(fres)
 freall.append(str(frelist))
###
 aa = ','
 combinelist=aa.join(combines)
 combineall.append(str(combinelist))

 combinelist_c=aa.join(combines_c)
 combineall_c.append(str(combinelist_c))
 combinelist_c_l=aa.join(combines_c_l)
 combineall_c_l.append(str(combinelist_c_l))

#5 times per combination,generate other 19 populations
 for ii in range (0,str2):
  combine=[]
  p=0
###
  fres=[]
  combines = []
  combines_c = []
  combines_c_l = []

  xbar=xm=0
  for x in range(1,str6):
   createVar["p4"+str(x)]=1
   createVar["p5"+str(x)]=1
   createVar["p6"+str(x)]=1
   createVar["four"+str(x)]=[]
   createVar["five"+str(x)]=[]
   createVar["six"+str(x)]=[]
   for num in range(0,4):
    f=createVar["four0"+str(x)][num]*random.uniform(str3d,str3u)
    createVar["p4"+str(x)]=createVar["p4"+str(x)]*f
    createVar["four"+str(x)].append (f)
####
    fres.append(str(f)) 
   p=p+createVar["p4"+str(x)]
   for num in range(0,5):
    f=createVar["five0"+str(x)][num]*random.uniform(str3d,str3u)
    createVar["p5"+str(x)]=createVar["p5"+str(x)]*f
    createVar["five"+str(x)].append (f)
####
    fres.append(str(f)) 
   p=p+createVar["p5"+str(x)]
   for num in range(0,6):
    f=createVar["six0"+str(x)][num]*random.uniform(str3d,str3u)
    createVar["p6"+str(x)]=createVar["p6"+str(x)]*f
    createVar["six"+str(x)].append (f)
####
    fres.append(str(f)) 
   p=p+createVar["p6"+str(x)]

   for i in range(0,4):
    for j in range(0,4):
     for k in range(0,4):
      if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
       fcc=createVar["four"+str(x)][i]*createVar["four"+str(x)][j]*createVar["four"+str(x)][k]
       combine.append (fcc)
   for i in range(0,5):
    for j in range(0,5):
     for k in range(0,5):
      if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
       fcc=createVar["five"+str(x)][i]*createVar["five"+str(x)][j]*createVar["five"+str(x)][k]
       combine.append (fcc)
   for i in range(0,6):
    for j in range(0,6):
     for k in range(0,6):
      if ((i!=j)and(i<j)and(j!=k)and(j<k)and(k!=i)):
       fcc=createVar["six"+str(x)][i]*createVar["six"+str(x)][j]*createVar["six"+str(x)][k]
       combine.append (fcc)


  combine_sample=[]
  for index in sort_combine_sample0_index:
   combine_sample.append(combine[index])

  mixlist=[]
  for xx in range(0,mix):
   mixlist.append(random.uniform(pd,p0))
  combine_sample_mix=combine_sample + mixlist
  xbar=np.mean(combine_sample_mix)
  xm=np.median(combine_sample_mix)
###
  for xxx in combine:
   combines_c.append(str(xxx))
  for yyy in combine_sample:
   combines_c_l.append(str(yyy))

  for zzz in combine_sample_mix:
   combines.append(str(zzz))

  xbarall.append(xbar)
  xmall.append(xm)
  xbaralls.append(str(xbar))
  xmalls.append(str(xm))
  pall.append(p)
  palls.append(str(p))
###
  aa = ','
  frelist=aa.join(fres)
  freall.append(str(frelist))
###
  combinelist=aa.join(combines)
  combineall.append(str(combinelist))

  combinelist_c=aa.join(combines_c)
  combineall_c.append(str(combinelist_c))

  combinelist_c_l=aa.join(combines_c_l)
  combineall_c_l.append(str(combinelist_c_l))


#  combinelen=combine_samplelen=combine_sample_mixlen=0
#  combinelen=len(combine)
#  combine_samplelen=len(combine_sample)
#  combine_sample_mixlen=len(combine_sample_mix)
#  print (combinelen,combine_samplelen,combine_sample_mixlen)

 slope, intercept, r_value, p_value, slope_std_error = st.linregress(xbarall, pall)
 result.append(str(slope))
 result.append(str(intercept))
 result.append(str(r_value**2))
 result.append(str(slope_std_error))
 result.append(str(p_value))

 slope, intercept, r_value, p_value, slope_std_error = st.linregress(xmall, pall)
 result.append(str(slope))
 result.append(str(intercept))
 result.append(str(r_value**2))
 result.append(str(slope_std_error))
 result.append(str(p_value))
 a = ','
 resultlist=a.join(result)
 resultn=resultlist + "\n"
 file = open("./result.txt", "a")
 file.write(resultn)
 file.close()

#generate origional data of the first, tenth, and 100th permutation
 if ((iii==0) or (iii==9) or (iii==99)  or (iii==999)):
  f = open("./{0}.txt".format(iii), "w") 
  aa = ','
  xbaralllist=aa.join(xbaralls)
  xbaralln=xbaralllist + "\n"
  xmalllist=aa.join(xmalls)
  xmalln=xmalllist + "\n"
  palllist=aa.join(palls)
  palln=palllist + "\n"
  f.write(str(xbaralln))
  f.write(str(xmalln))
  f.write(str(palln))
  f.close()
###
  ff = open("./{0}.origion.txt".format(iii), "a") 
  aa = '\n'
  frealllist=aa.join(freall)
  frealllistn=frealllist + "\n"
  ff.write(str(frealllistn))

  combinealllist_c=aa.join(combineall_c)
  combinealllist_cn=combinealllist_c + "\n"
  ff.write(str(combinealllist_cn))

  combinealllist_c_l=aa.join(combineall_c_l)
  combinealllist_c_ln=combinealllist_c_l + "\n"
  ff.write(str(combinealllist_c_ln))

  combinealllist=aa.join(combineall)
  combinealllistn=combinealllist + "\n"
  ff.write(str(combinealllistn))
  ff.close()




