@@ -50,6 +50,7 @@ struct MATHS_EXPORT SChangeDescription
5050 enum EDescription
5151 {
5252 E_LevelShift,
53+ E_LinearScale,
5354 E_TimeShift
5455 };
5556
@@ -88,7 +89,7 @@ class MATHS_EXPORT CUnivariateTimeSeriesChangeDetector
8889 public:
8990 CUnivariateTimeSeriesChangeDetector (const TDecompositionPtr &trendModel,
9091 const TPriorPtr &residualModel,
91- core_t ::TTime minimumTimeToDetect = 6 * core::constants::HOUR,
92+ core_t ::TTime minimumTimeToDetect = 12 * core::constants::HOUR,
9293 core_t ::TTime maximumTimeToDetect = core::constants::DAY,
9394 double minimumDeltaBicToDetect = 14.0 );
9495
@@ -103,6 +104,13 @@ class MATHS_EXPORT CUnivariateTimeSeriesChangeDetector
103104 // ! if there has been.
104105 TOptionalChangeDescription change ();
105106
107+ // ! The function used to decide whether to accept a change.
108+ // ! A change is accepted at a value of 1.0 for this function.
109+ // !
110+ // ! \param[out] change Filled in with the index of the change
111+ // ! the most likely change.
112+ double decisionFunction (std::size_t &change) const ;
113+
106114 // ! Add \p samples to the change detector.
107115 void addSamples (const TWeightStyleVec &weightStyles,
108116 const TTimeDoublePr1Vec &samples,
@@ -123,7 +131,7 @@ class MATHS_EXPORT CUnivariateTimeSeriesChangeDetector
123131 private:
124132 using TChangeModel = time_series_change_detector_detail::CUnivariateChangeModel;
125133 using TChangeModelPtr = boost::shared_ptr<TChangeModel>;
126- using TChangeModelPtr4Vec = core::CSmallVector<TChangeModelPtr, 4 >;
134+ using TChangeModelPtr5Vec = core::CSmallVector<TChangeModelPtr, 5 >;
127135 using TMinMaxAccumulator = CBasicStatistics::CMinMax<core_t ::TTime>;
128136
129137 private:
@@ -147,7 +155,7 @@ class MATHS_EXPORT CUnivariateTimeSeriesChangeDetector
147155 double m_CurrentEvidenceOfChange;
148156
149157 // ! The change models.
150- TChangeModelPtr4Vec m_ChangeModels;
158+ TChangeModelPtr5Vec m_ChangeModels;
151159};
152160
153161namespace time_series_change_detector_detail
@@ -158,6 +166,7 @@ namespace time_series_change_detector_detail
158166class MATHS_EXPORT CUnivariateChangeModel : private core::CNonCopyable
159167{
160168 public:
169+ using TDouble1Vec = core::CSmallVector<double , 1 >;
161170 using TDouble4Vec = core::CSmallVector<double , 4 >;
162171 using TDouble4Vec1Vec = core::CSmallVector<TDouble4Vec, 1 >;
163172 using TTimeDoublePr = std::pair<core_t ::TTime, double >;
@@ -189,10 +198,10 @@ class MATHS_EXPORT CUnivariateChangeModel : private core::CNonCopyable
189198 virtual TOptionalChangeDescription change () const = 0;
190199
191200 // ! Update the change model with \p samples.
192- virtual void addSamples (std::size_t count,
193- const TWeightStyleVec & weightStyles,
201+ virtual void addSamples (const std::size_t count,
202+ TWeightStyleVec weightStyles,
194203 const TTimeDoublePr1Vec &samples,
195- const TDouble4Vec1Vec & weights) = 0;
204+ TDouble4Vec1Vec weights) = 0;
196205
197206 // ! Debug the memory used by this object.
198207 void debugMemoryUsage (core::CMemoryUsage::TMemoryUsagePtr mem) const ;
@@ -206,29 +215,28 @@ class MATHS_EXPORT CUnivariateChangeModel : private core::CNonCopyable
206215 // ! Get a checksum for this object.
207216 virtual uint64_t checksum (uint64_t seed) const = 0;
208217
209- protected:
210- // ! The sample count to initialize a change model.
211- static const std::size_t COUNT_TO_INITIALIZE{5u };
212-
213218 protected:
214219 // ! Restore the residual model reading state from \p traverser.
215220 bool restoreResidualModel (const SDistributionRestoreParams ¶ms,
216221 core::CStateRestoreTraverser &traverser);
217222
218223 // ! Get the log-likelihood.
219224 double logLikelihood () const ;
220- // ! Update the data log-likelihood with \p logLikelihood.
221- void addLogLikelihood (double logLikelihood);
222225
223226 // ! Get the expected log-likelihood.
224227 double expectedLogLikelihood () const ;
225- // ! Update the expected data log-likelihood with \p logLikelihood.
226- void addExpectedLogLikelihood (double logLikelihood);
228+
229+ // ! Update the log-likelihood with \p samples.
230+ void updateLogLikelihood (const TWeightStyleVec &weightStyles,
231+ const TDouble1Vec &samples,
232+ const TDouble4Vec1Vec &weights);
233+
234+ // ! Update the expected log-likelihoods.
235+ void updateExpectedLogLikelihood (const TWeightStyleVec &weightStyles,
236+ const TDouble4Vec1Vec &weights);
227237
228238 // ! Get the time series trend model.
229239 const CTimeSeriesDecompositionInterface &trendModel () const ;
230- // ! Get the time series trend model.
231- CTimeSeriesDecompositionInterface &trendModel ();
232240
233241 // ! Get the time series residual model.
234242 const CPrior &residualModel () const ;
@@ -275,10 +283,10 @@ class MATHS_EXPORT CUnivariateNoChangeModel final : public CUnivariateChangeMode
275283 virtual TOptionalChangeDescription change () const ;
276284
277285 // ! Get the log likelihood of \p samples.
278- virtual void addSamples (std::size_t count,
279- const TWeightStyleVec & weightStyles,
286+ virtual void addSamples (const std::size_t count,
287+ TWeightStyleVec weightStyles,
280288 const TTimeDoublePr1Vec &samples,
281- const TDouble4Vec1Vec & weights);
289+ TDouble4Vec1Vec weights);
282290
283291 // ! Get the static size of this object.
284292 virtual std::size_t staticSize () const ;
@@ -312,10 +320,10 @@ class MATHS_EXPORT CUnivariateLevelShiftModel final : public CUnivariateChangeMo
312320 virtual TOptionalChangeDescription change () const ;
313321
314322 // ! Update with \p samples.
315- virtual void addSamples (std::size_t count,
316- const TWeightStyleVec & weightStyles,
323+ virtual void addSamples (const std::size_t count,
324+ TWeightStyleVec weightStyles,
317325 const TTimeDoublePr1Vec &samples,
318- const TDouble4Vec1Vec & weights);
326+ TDouble4Vec1Vec weights);
319327
320328 // ! Get the static size of this object.
321329 virtual std::size_t staticSize () const ;
@@ -324,7 +332,6 @@ class MATHS_EXPORT CUnivariateLevelShiftModel final : public CUnivariateChangeMo
324332 virtual uint64_t checksum (uint64_t seed) const ;
325333
326334 private:
327- using TDoubleVec = std::vector<double >;
328335 using TMeanAccumulator = CBasicStatistics::SSampleMean<double >::TAccumulator;
329336
330337 private:
@@ -338,6 +345,56 @@ class MATHS_EXPORT CUnivariateLevelShiftModel final : public CUnivariateChangeMo
338345 double m_SampleCount;
339346};
340347
348+ // ! \brief Captures the likelihood of the data given an arbitrary
349+ // ! linear scaling.
350+ class MATHS_EXPORT CUnivariateLinearScaleModel final : public CUnivariateChangeModel
351+ {
352+ public:
353+ CUnivariateLinearScaleModel (const TDecompositionPtr &trendModel,
354+ const TPriorPtr &residualModel);
355+
356+ // ! Initialize by reading state from \p traverser.
357+ virtual bool acceptRestoreTraverser (const SModelRestoreParams ¶ms,
358+ core::CStateRestoreTraverser &traverser);
359+
360+ // ! Persist state by passing information to \p inserter.
361+ virtual void acceptPersistInserter (core::CStatePersistInserter &inserter) const ;
362+
363+ // ! The BIC of applying the level shift.
364+ virtual double bic () const ;
365+
366+ // ! The expected BIC of applying the change.
367+ virtual double expectedBic () const ;
368+
369+ // ! Get a description of the level shift.
370+ virtual TOptionalChangeDescription change () const ;
371+
372+ // ! Update with \p samples.
373+ virtual void addSamples (const std::size_t count,
374+ TWeightStyleVec weightStyles,
375+ const TTimeDoublePr1Vec &samples,
376+ TDouble4Vec1Vec weights);
377+
378+ // ! Get the static size of this object.
379+ virtual std::size_t staticSize () const ;
380+
381+ // ! Get a checksum for this object.
382+ virtual uint64_t checksum (uint64_t seed) const ;
383+
384+ private:
385+ using TMeanAccumulator = CBasicStatistics::SSampleMean<double >::TAccumulator;
386+
387+ private:
388+ // ! The optimal shift.
389+ TMeanAccumulator m_Scale;
390+
391+ // ! The mode of the initial residual distribution model.
392+ double m_ResidualModelMode;
393+
394+ // ! The number of samples added so far.
395+ double m_SampleCount;
396+ };
397+
341398// ! \brief Captures the likelihood of the data given a specified
342399// ! time shift.
343400class MATHS_EXPORT CUnivariateTimeShiftModel final : public CUnivariateChangeModel
@@ -364,10 +421,10 @@ class MATHS_EXPORT CUnivariateTimeShiftModel final : public CUnivariateChangeMod
364421 virtual TOptionalChangeDescription change () const ;
365422
366423 // ! Update with \p samples.
367- virtual void addSamples (std::size_t count,
368- const TWeightStyleVec & weightStyles,
424+ virtual void addSamples (const std::size_t count,
425+ TWeightStyleVec weightStyles,
369426 const TTimeDoublePr1Vec &samples,
370- const TDouble4Vec1Vec & weights);
427+ TDouble4Vec1Vec weights);
371428
372429 // ! Get the static size of this object.
373430 virtual std::size_t staticSize () const ;
0 commit comments