00001 package commandline.Distributions;
00002
00003 import java.util.ArrayList;
00004
00005 import junit.framework.Assert;
00006
00007 import commandline.CodingUtils.DataStructure;
00008 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00009 import commandline.NormalMeshAlgorithm.Configuration;
00010 import commandline.NormalMeshAlgorithm.SnijPixelStrategy;
00011 import commandline.debuggingCode.Debug;
00012 import commandline.factories.SnijPixelStrategyBuilder;
00013
00014 public class VerticalDistribution_Power_Law extends VerticalDistribution {
00020
00021 public final static int UNUSED = -1;
00022 public final static float PROB = 0.4f;
00023 public final static float LAMBDA = 0.05f;
00024 protected float k;
00025 protected float a;
00026 public VerticalDistribution_Power_Law(Configuration configuration, DataStructure breathFirstDatastructure) {
00027 super(configuration, breathFirstDatastructure);
00028 Histogram histogram=makeHistogram();
00029 this.k= this.getAbsValueOfKEstimate(histogram);
00030 this.p = getPEstimate(histogram);
00031 this.a=this.getAFrom(k);
00032 Debug.print("k= "+k);
00033 Debug.print("a= "+a);
00034 Debug.print("p= "+p);
00035 Assert.assertTrue(p>=0);
00036 Assert.assertTrue(a>=0);
00037 Assert.assertTrue(k>=0);
00038 }
00039
00040
00046 public VerticalDistribution_Power_Law(float k, float p) {
00047 this.p=p;
00048 this.k=k;
00049 this.a=this.getAFrom(k);
00050 }
00051
00052
00053 public float getSchatter() {
00054 return this.k;
00055 }
00059 public double getPMF_at(double v,float normalizingFactor,double width) {
00060 v = Math.abs(v);
00061 if (Debug.DEBUG) Assert.assertTrue(v >= 0);
00062 double p_v;
00063 if(v!=0){
00064 p_v = (1 - p) * a * Math.pow(v, -this.k);
00065 }else{
00066 p_v = p;
00067 }
00068 return p_v;
00069 }
00070
00071 protected float getPEstimate(Histogram histogram){
00072 int totaal_N_0=histogram.getN(0);
00073 float f_V_0=(float)(totaal_N_0)/ (float)(histogram.getPopulationCount());
00074
00075 p=f_V_0;
00076 return p;
00077 }
00078
00079 protected float getLambdaEstimator(Configuration configuration,DataStructure breathFirstDatastructure){
00080 System.err.println("getLambdaEstimator should not be invoced");
00081 System.exit(-1);
00082 return 0;
00083 }
00084
00088 private Histogram makeHistogram(){
00089 SnijPixelStrategy snij=SnijPixelStrategyBuilder.makeSnijPixelStrategy(configuration.getSnijPixelStrategyTYPE() , configuration);
00090 int verticalOffset;
00091 Histogram histogram=new Histogram(256);
00092 for (GenericEdge edge: this.breathFirstDatastructure.getParentCommitOrderedList()) {
00093 verticalOffset=edge.getVerticalOffset();
00094 if(edge.isFlat() && edge.getLengte()>2){
00095
00096 verticalOffset=snij.calculateVerticalOffset(snij.berekenMiddenHoogte(edge),commandline.ImageStuff.Image.getInstance().getPuntOpEnty(edge.getMidLoc()).getZ());
00097 }
00098 histogram.addToBin(verticalOffset);
00099 }
00100 return histogram;
00101 }
00102 protected float getAbsValueOfKEstimate(Histogram histogram){
00103
00104
00105
00106 ArrayList<Integer> notEmptyIndices= histogram.getNotEmptyBinsBetween(1, histogram.size()-1);
00107
00108
00112 float log_x_mean=0;
00113 float log_y_mean=0;
00114 for (Integer index : notEmptyIndices) {
00115 log_x_mean+= Math.log(index);
00116 log_y_mean+=Math.log(histogram.getN(index));
00117 }
00118 log_x_mean/=notEmptyIndices.size();
00119 log_y_mean/=notEmptyIndices.size();
00120
00121
00126 float var_log_x=0;
00127 float var_log_y=0;
00128 for (Integer index : notEmptyIndices) {
00129 var_log_x+= Math.pow(Math.log(index)-log_x_mean,2);
00130 var_log_y+= Math.pow(Math.log(histogram.getN(index))-log_y_mean,2);
00131 }
00132
00137 float k=(float)Math.sqrt(var_log_y)/(float)Math.sqrt(var_log_x);
00138
00139
00143 Cumulative cumulative=new Cumulative(histogram);
00144 float k_plus_one=(cumulative.size()*cumulative.getS_xy()-cumulative.getS_x()*cumulative.getS_y());
00145 k_plus_one/=(cumulative.size()*cumulative.getS_x_2()-Math.pow(cumulative.getS_x(),2));
00146
00147 k=k_plus_one-1;
00148 k=Math.abs(k);
00149
00150 return k;
00151 }
00152
00157 private float getAFrom(float k){
00158 float som=0;
00159 for(int index =1;index!=256;index++){
00160 som+=Math.pow(index,(-Math.abs(k)));
00161 }
00162 float a=(1-this.p)/som;
00163 return a;
00164 }
00165
00166
00167
00168 public NormalIndicesDistribution getNormalIndicesDistribution_1A() {
00169
00170 return new NormalIndicesDistribution_1A_Power_Law(this);
00171 }
00172
00173
00174
00175
00176 public NormalIndicesDistribution getNormalIndicesDistribution_1B() {
00177 return new NormalIndicesDistribution_1B_Power_Law();
00178 }
00179
00180
00181
00182
00183 public NormalIndicesDistribution getNormalIndicesDistribution_2A() {
00184 return new NormalIndicesDistribution_2A_Power_Law(this);
00185 }
00186
00187
00188
00189
00190 public NormalIndicesDistribution getNormalIndicesDistribution_2B() {
00191 return new NormalIndicesDistribution_2B_Power_Law();
00192 }
00193
00194 }
00195
00196
00197