00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00045
#include "ScaledConditionalCDFSmoother.h"
00046
00047
00048
namespace PLearn {
00049
using namespace std;
00050
00051 ScaledConditionalCDFSmoother::ScaledConditionalCDFSmoother()
00052 :
ConditionalCDFSmoother(), preserve_relative_density(true)
00053 {
00054 }
00055
00056
PLEARN_IMPLEMENT_OBJECT(
ScaledConditionalCDFSmoother,
00057
"This smoothes a low-resolution histogram using as prior a high-resolution one.",
00058
"This class takes as 'prior_cdf' a detailed histogram (usually derived from\n"
00059
"an unconditional distribution) and uses it to smooth a given survival\n"
00060
"function and provide extra detail (high resolution).\n"
00061
"Two smoothing formula are provided, both of which guarantee that the smoothed\n"
00062
"survival function takes the same value as the raw one at or near original bin\n"
00063
"positions. In between the original bin positions, the smoothed survival\n"
00064
"is obtained by applying one of two possible formula, according to the\n"
00065
"preserve_relative_density option.\n");
00066
00067 void ScaledConditionalCDFSmoother::declareOptions(
OptionList& ol)
00068 {
00069
declareOption(ol,
"preserve_relative_density", &ScaledConditionalCDFSmoother::preserve_relative_density,
00070 OptionBase::buildoption,
00071
"If true then the following formula is used inside each of the large intervals (t_0,t_1):\n"
00072
" S(y_t) = S(y_{t_0})+(PS(y_t)-PS(y_{t_0}))(RS(y_{t_0})-RS(y_{t_1}))/(PS(y_{t_1})-PS(y_{t_0})\n"
00073
"where S(y_t) is the smoothed survival function at position y_t, PS(y_t) is the prior\n"
00074
"survival function at y_t, and RS(y_t) is the rough survival function (which is to be\n"
00075
"smoothed) at y_t. Note that RS is only known at the extremes of the interval, y_{t_0}\n"
00076
"and y_{t_1}. Note that this formula has the property that within the interval, the\n"
00077
"density is the prior density, scaled by the ratio of the total density in the interval\n"
00078
"for the target rough curve with respect to the prior curve\n"
00079
"If false, then the following formula is used instead, using the same notation:\n"
00080
" S(y_t) = PS(y_t)(RS(y_{t_0})/PS(y_{t_0}) + (y_t - y_{t_0})(RS(y_{t_1})-RS(y_{t_0}))/(PS(y_{t_1}) (t_1 - t_0)))\n"
00081
"What is the justification for this second formula?\n"
00082 );
00083
00084
00085
00086 inherited::declareOptions(ol);
00087 }
00088
00089 void ScaledConditionalCDFSmoother::build_()
00090 {
00091 }
00092
00093
00094 void ScaledConditionalCDFSmoother::build()
00095 {
00096 inherited::build();
00097
build_();
00098 }
00099
00100
00101 void ScaledConditionalCDFSmoother::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00102 {
00103 Object::makeDeepCopyFromShallowCopy(copies);
00104 }
00105
00106
00107
00108
00109
00110
00111 real ScaledConditionalCDFSmoother::smooth(
const Vec& source_function,
Vec& smoothed_function,
00112
Vec bin_positions,
Vec dest_bin_positions)
const
00113
{
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
if (!prior_cdf)
00124
PLERROR(
"in ScaledConditionalCDFSmoother::smooth you need to supply a prior_cdf");
00125
00126
if(bin_positions.
size() != source_function.
size()+1)
00127
PLERROR(
"in ScaledConditionalCDFSmoother::smooth you need to supply bin_positions");
00128
if(dest_bin_positions.
size() == 0)
00129
PLERROR(
"in ScaledConditionalCDFSmoother::smooth you need to supply dest_bin_positions");
00130 smoothed_function.
resize(dest_bin_positions.
size()-1);
00131
00132
00133
int j= 0;
00134
for(
int i= 0; i < source_function.
size(); ++i)
00135 {
00136
Vec v0(1), v1(1);
00137 v0[0]= bin_positions[i];
00138 v1[0]= bin_positions[i+1];
00139
00140
real prev_ratio= source_function[i]/prior_cdf->survival_fn(v0);
00141
real next_ratio;
00142
if(i == source_function.size()-1)
00143 next_ratio= 0.0;
00144
else
00145 next_ratio= source_function[i+1]/prior_cdf->survival_fn(v1);
00146
00147 cout << source_function[i] <<
'\t' << prev_ratio <<
'\t' << next_ratio <<
'\t' << v0[0] <<
'\t' << v1[0] <<
endl;
00148
real slope = !
preserve_relative_density? 0 :
00149 ((source_function[i+1]-source_function[i])/(prior_cdf->survival_fn(v1)-prior_cdf->survival_fn(v0)));
00150
real absisse = !
preserve_relative_density? 0 :
00151 (source_function[i] - slope * prior_cdf->survival_fn(v0));
00152
while(j < smoothed_function.
size() && dest_bin_positions[j+1] <= bin_positions[i+1])
00153 {
00154
Vec v(1);
00155 v[0]= dest_bin_positions[j];
00156
00157
00158
00159
00160
if (!
preserve_relative_density)
00161 smoothed_function[j]= prior_cdf->survival_fn(v) *
00162 (prev_ratio + (v[0]-v0[0])*(next_ratio-prev_ratio)/(v1[0]-v0[0]));
00163
else
00164 smoothed_function[j]= absisse + slope * prior_cdf->survival_fn(v);
00165 cout <<
'\t' << v[0] <<
'\t' << prior_cdf->survival_fn(v) <<
'\t' << smoothed_function[j] <<
endl;
00166 ++j;
00167 }
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
return 0.0;
00270 }
00271
00272 }