hic3defdr.util.lowess module¶
-
hic3defdr.util.lowess.lowess_fit(x, y, logx=False, logy=False, left_boundary=None, right_boundary=None, frac=0.3, delta=0.01)[source]¶ Opinionated convenience wrapper for lowess smoothing.
Parameters: - y (x,) – The x and y values to fit, respectively.
- logy (logx,) – Pass True to perform the fit on the scale of
log(x)and/orlog(y), respectively. - right_boundary (left_boundary,) – Allows specifying boundaries for the fit, in the original
xspace. If a float is passed, the returned fit will return the farthest left or farthest right lowess-estimatedy_hat(from the original fitting set) for all points which are left or right of the specified left or right boundary point, respectively. Pass None to use linear extrapolation for these points instead. - frac (float) – The lowess smoothing fraction to use.
- delta (float) – Distance (on the scale of
xorlog(x)) within which to use linear interpolation when constructing the initial fit, expressed as a fraction of the range ofxorlog(x).
Returns: This function takes in
xvalues on the originalxscale and returns estimatedyvalues on the originalyscale (regardless of what is passed forlogxandlogy). This function will still return sane estimates foryeven at points not in the original fitting set by performing linear interpolation in the space the fit was performed in.Return type: function
Notes
No filtering of input values is performed; clients are expected to handle this if desired. NaN values should not break the function, but
xpoints with zero values passed whenlogxis True are expected to break the function.The default value of the
deltaparameter is set to be non-zero, matching the behavior of lowess smoothing in R and improving performance.Linear interpolation between x-values in the original fitting set is used to provide a familiar functional interface to the fitted function.
Boundary conditions on the fitted function are exposed via
left_boundaryandright_boundary, mostly as a convenience for points wherex == 0when fitting was performed on the scale oflog(x).When
left_boundaryorright_boundaryare None (this is the default) the fitted function will be linearly extrapolated for points beyond the lowest and highest x-values inx.
-
hic3defdr.util.lowess.weighted_lowess_fit(x, y, logx=False, logy=False, left_boundary=None, right_boundary=None, frac=None, auto_frac_factor=15.0, delta=0.01, w=20, power=0.25, interpolate_before_increase=True)[source]¶ Performs lowess fitting as in
lowess_fit(), but weighting the data points automatically according to the precision in theyvalues as estimated by a rolling window sample variance.Points are weighted proportionally to a specified power
powerof their precision by adding duplicated points to the dataset. This should approximate the effects of a true weighted lowess fit, with the caveat that the weights are rounded a bit.Weighting the data points according to this rolling window sample variance is probably only a good idea if the marginal distribution of
xvalues is uniform.Parameters: - y (x,) – The x and y values to fit, respectively.
- logy (logx,) – Pass True to perform the fit on the scale of
log(x)and/orlog(y), respectively. - right_boundary (left_boundary,) – Allows specifying boundaries for the fit, in the original
xspace. If a float is passed, the returned fit will return the farthest left or farthest right lowess-estimatedy_hat(from the original fitting set) for all points which are left or right of the specified left or right boundary point, respectively. Pass None to use linear extrapolation for these points instead. - frac (float, optional) – The lowess smoothing fraction to use. Pass None to use the default:
auto_frac_factordivided by the product of the average of the unscaled weights and the largest scaled weight. - auto_frac_factor (float) – When
fracis None, this factor scales the automatically determined fraction parameter. - delta (float) – Distance (on the scale of
xorlog(x)) within which to use linear interpolation when constructing the initial fit, expressed as a fraction of the range ofxorlog(x). - w (int) – The size of the rolling window to use when estimating the precision of the y values.
- power (float) – Precisions will be taken to this power to obtain unscaled weights.
- interpolate_before_increase (bool) – Hacky flag introduced to handle quirk of Hi-C dispersion vs distance relationships in which dispersion is elevated at extremely short distances. When True, this function will identify a group of points with the lowest x-values across which the y-value is monotonically decreasing. These points will be included in the variance estimation, but will be excluded from lowess fitting. Linear interpolation will be used at these x-values instead, since it is hard to convince lowess to follow a sharp change in the trend that is only supported by 3-4 data points out of 200-500 total data points, even with our best attempts at weighting. Pass False to perform a simple weighted lowess fit with no linear interpolation.
Returns: This function takes in
xvalues on the originalxscale and returns estimatedyvalues on the originalyscale (regardless of what is passed forlogxandlogy). This function will still return sane estimates foryeven at points not in the original fitting set by performing linear interpolation in the space the fit was performed in.Return type: function