/**
* Copyright (C) 2013-2017 Vasilis Vryniotis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datumbox.framework.core.statistics.descriptivestatistics;
import com.datumbox.framework.common.dataobjects.AssociativeArray;
import com.datumbox.framework.common.dataobjects.AssociativeArray2D;
import com.datumbox.framework.common.dataobjects.FlatDataCollection;
import com.datumbox.framework.common.dataobjects.TypeInference;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* This class provides several methods to estimate the descriptive statistics
* of censored observations.
*
* @author Vasilis Vryniotis
*/
public class CensoredDescriptives {
/**
* Postfix character used in Censored observations.
*/
public static final String CENSORED_NUMBER_POSTFIX="+";
/**
* Calculates the survivalFunction by processing the flatDataCollection with the
censored internalData. The flatDataCollection contains numbers in string format. The
* Censored entries contain a + symbol at the end of the number.
*
* @param flatDataCollection
* @return
*/
public static AssociativeArray2D survivalFunction(FlatDataCollection flatDataCollection) {
AssociativeArray2D survivalFunction = new AssociativeArray2D(); //AssociativeArray2D is important to maintain the order of the first keys
Queue censoredData = new PriorityQueue<>();
Queue uncensoredData = new PriorityQueue<>();
int n = flatDataCollection.size();
if(n==0) {
throw new IllegalArgumentException("The provided collection can't be empty.");
}
for(Object value : flatDataCollection) {
String str = value.toString();
if(str.endsWith(CENSORED_NUMBER_POSTFIX)) {
//censored internalData encoded as 4.3+ or -4.3+
censoredData.add(Double.valueOf(str.substring(0,str.length()-CENSORED_NUMBER_POSTFIX.length()))); //remove the trailing char and convert it to double
}
else {
//uncensored internalData
uncensoredData.add(TypeInference.toDouble(value)); //convert it to double
}
}
Double currentCensored = null;
Double currentUncensored = null;
int i = 1;
double previousUncensoredValue = 1.0;
double varianceDenominator = 0.0;
do {
if(currentCensored==null) {
currentCensored=censoredData.poll();
}
if(currentUncensored==null) {
currentUncensored=uncensoredData.poll();
}
boolean isCensored = false;
String key;
if(currentUncensored == null) {
key=currentCensored.toString().concat(CENSORED_NUMBER_POSTFIX);
currentCensored = null;
isCensored = true;
}
else if(currentCensored == null) {
key=currentUncensored.toString();
currentUncensored = null;
}
else if(currentCensored0) {
varianceDenominator+=1.0/((n-i)*(n-i+1.0));
}
survivalFunction.put2d(key, "Sti", Sti);
survivalFunction.put2d(key, "varianceSti", Sti*Sti*varianceDenominator);
previousUncensoredValue=Sti;
}
++i;
}
while(currentCensored!=null || currentUncensored!=null ||
!censoredData.isEmpty() || !uncensoredData.isEmpty());
//censoredData=null;
//uncensoredData=null;
return survivalFunction;
}
/**
* Calculates median.
*
* @param survivalFunction
* @return
*/
public static double median(AssociativeArray2D survivalFunction) {
Double ApointTi = null;
Double BpointTi = null;
int n = survivalFunction.size();
if(n==0) {
throw new IllegalArgumentException("The provided collection can't be empty.");
}
for(Map.Entry
Ads help maintain this website.