public override void Recompute() { IsValid = false; tsOutput = null; mvtsOutput = null; if (SkipDates == null) { SkipDates = new List <DateTime>(); } var mvts = GetInput(0) as MVTimeSeries; var ts = GetInput(0) as Data.TimeSeries; // make sure we have at least 2 data points in the time series to be sampled var tp = GetInputType(-1); if (tp != InputType.UnivariateTS && tp != InputType.MultivariateTS) { return; } if (tp == InputType.MultivariateTS) { // split it up var individuals = mvts.ExtractList(); var nc = individuals.Count; var sampled = new List <Data.TimeSeries>(nc); IsValid = true; for (var i = 0; i < nc; ++i) { var res = DoUnivariateSampling(individuals[i]); if (res.Count > 0) { sampled.Add(res); } else { IsValid = false; } } if (IsValid) { mvtsOutput = new MVTimeSeries(sampled, false) { Title = mvts.Title } } ; } if (tp == InputType.UnivariateTS) { tsOutput = DoUnivariateSampling(ts); IsValid = tsOutput.Count > 0; } }
public override void Recompute() { IsValid = false; var tp = GetInputType(-1); if (tp == InputType.MultivariateTS) { var mts = GetInput(0) as MVTimeSeries; if (mts == null) { return; } mvIntegral = new MVTimeSeries(mts.Dimension) { Title = "MV" }; for (var i = 0; i < mts.Dimension; ++i) { mvIntegral.SubTitle[i] = mts.SubTitle[i]; } integral = null; var sum = new double[mts.Dimension]; for (var t = 0; t < mts.Count; ++t) { for (var j = 0; j < mts.Dimension; ++j) { if (!double.IsNaN(mts[t][j])) { sum[j] += mts[t][j]; } } var temp = new double[mts.Dimension]; Array.Copy(sum, temp, mts.Dimension); mvIntegral.Add(mts.TimeStamp(t), temp, false); } } if (tp == InputType.UnivariateTS) { var ts = GetInput(0) as Data.TimeSeries; if (ts == null) { return; } integral = new Data.TimeSeries { Title = ts.Title }; mvIntegral = null; double sum = 0; for (var t = 0; t < ts.Count; ++t) { sum += ts[t]; integral.Add(ts.TimeStamp(t), sum, false); } } IsValid = true; }
//public override Icon GetIcon() //{ // return null; //} public override void Recompute() { CheckParameters(); var list1 = new List <Data.TimeSeries>(); IsValid = true; // otherwise we can bind them together var lists = new List <List <Data.TimeSeries> >(); for (var i = 0; i < NumInputs(); ++i) { var ts = GetInput(i); var mts = ts as MVTimeSeries; if (mts != null) { lists.Add(mts.ExtractList()); } else { var uts = ts as Data.TimeSeries; if (uts != null) { var lts = new List <Data.TimeSeries>(); lts.Add(uts); lists.Add(lts); } else { IsValid = false; } } } if (IsValid) { foreach (var lts in lists) { foreach (var ts in lts) { list1.Add(ts); } } mvtsOut = new MVTimeSeries(list1, SampleMissing); mvtsOut.Title = "MV"; } }
public override object GetOutput(int socket) { if (socket == 0) { var subList = new List <TimeSeries> { outputs[0], outputs[1], outputs[2] }; var bundle = new MVTimeSeries(subList, false); bundle.Title = "Bollinger"; return(bundle); } if (socket == 1) { return(outputs[3]); } throw new SocketException(); }
public override void Recompute() { IsValid = false; if (GetInputType(-1) != InputType.UnivariateTS) { return; } var series = GetInput(0) as Data.TimeSeries; combined = new MVTimeSeries(4); combined.Title = "Bars"; combined.SubTitle = new string[4] { "Open", "High", "Low", "Close" }; var accumulated = Vector <double> .Build.Dense(4); accumulated[0] = accumulated[1] = accumulated[2] = accumulated[3] = series[0]; var initialTime = PhaseAdjust(series.TimeStamp(0)); // initial time for current bar for (var t = 0; t < series.Count; ++t) { var intoBar = series.TimeStamp(t) - initialTime; if (intoBar >= Period) { // dump the previous bar and reset things combined.Add(initialTime + Period, accumulated.ToArray(), false); accumulated = Vector <double> .Build.Dense(4); var tx = series[t]; accumulated[0] = tx; accumulated[1] = tx; accumulated[2] = tx; accumulated[3] = tx; initialTime = PhaseAdjust(series.TimeStamp(t)); } accumulated[3] = series[t]; accumulated[1] = Math.Max(accumulated[1], series[t]); accumulated[2] = Math.Min(accumulated[2], series[t]); } IsValid = true; }
//public override Icon GetIcon() //{ // return null; //} public override void Recompute() { IsValid = false; var ts = GetInput(0) as Data.TimeSeries; var mts = GetInput(0) as MVTimeSeries; if (ts == null && mts == null) { return; // not valid } if (ts != null) { // merge univariate TS var result = new Data.TimeSeries(); for (var ii = 0; ii < NumberOfInputs; ++ii) { var pts = GetInput(ii) as Data.TimeSeries; if (pts != null) { result.Add(pts, true); } } outputResult = result; IsValid = true; } else if (mts != null) { // merge univariate TS var result = new MVTimeSeries(mts.Dimension); result.SubTitle = mts.SubTitle; for (var ii = 0; ii < NumberOfInputs; ++ii) { var pts = GetInput(ii) as MVTimeSeries; if (pts != null) { result.Add(pts, true); } } outputResult = result; IsValid = true; } }
//public abstract Icon GetIcon(); public virtual object GetOutput(int socket) { if (outputs == null) { return(null); } if (outputs.Count == 0) { return(null); } if (ShouldBundleOutputs && outputs.Count > 1) // then bundle them together { if (socket != 0) { throw new SocketException(); } if (outputs == null) { return(null); } if (outputs.Count == 1) { return(outputs[0]); } var mvts = new MVTimeSeries(outputs, false); if (multivariateOutputPrefix != null) { mvts.Title = multivariateOutputPrefix; } return(mvts); } // otherwise just return them as they are if (outputs == null) { return(null); } if (socket < NumOutputs()) { return(outputs[socket]); } throw new SocketException(); }
protected override bool CheckDataValidity(object data, StringBuilder failMessage) { mvts = data as MVTimeSeries; if (mvts == null) { return(false); } if (mvts.Dimension != dimension) { return(false); } if (!CanHandleNaNs()) { if (mvts.NaNCount() > 0) { if (failMessage != null) { failMessage.AppendLine("Cannot use this model with data with NaNs."); } return(false); } } return(true); }
public override void Recompute() { IsValid = false; tsOutput = null; mvtsOutput = null; // make sure we have at least 2 data points in the time series to be sampled var tp = GetInputType(0); if (tp != InputType.UnivariateTS && tp != InputType.MultivariateTS) { return; } var rp = GetInputType(1); if (rp != InputType.UnivariateTS && rp != InputType.MultivariateTS) { return; } // first get the reference times var times = new List <DateTime>(1000); var input1 = GetInput(1); var mvref = input1 as MVTimeSeries; var uref = input1 as TimeSeries; if (rp == InputType.UnivariateTS) { if (uref == null) { return; } for (int t = 0; t < uref.Count; ++t) { times.Add(uref.TimeStamp(t)); } } else if (rp == InputType.MultivariateTS) { if (mvref == null) { return; } for (int t = 0; t < mvref.Count; ++t) { times.Add(mvref.TimeStamp(t)); } } else { throw new ApplicationException("Invalid input type; this should not happen!"); } int refDim = uref != null ? 1 : mvref.Dimension; // then sample to get the output if (tp == InputType.UnivariateTS) { var uts = GetInput(0) as TimeSeries; if (uts == null) { return; } // split it up if (!IncludeReferenceInOutput) { tsOutput = new TimeSeries(); for (int t = 0; t < times.Count; ++t) { tsOutput.Add(times[t], uts.ValueAtTime(times[t]), true); } } else { mvtsOutput = new MVTimeSeries(1 + refDim); for (int t = 0; t < times.Count; ++t) { var val = new double[1 + refDim]; val[0] = uts.ValueAtTime(times[t]); for (int i = 1; i <= refDim; ++i) { val[i] = mvref != null ? mvref[t][i - 1] : uref[t]; } mvtsOutput.Add(times[t], val, true); } } IsValid = true; } if (tp == InputType.MultivariateTS) { var mts = GetInput(0) as MVTimeSeries; if (mts == null) { return; } // split it up if (!IncludeReferenceInOutput) { mvtsOutput = new MVTimeSeries(); for (int t = 0; t < times.Count; ++t) { mvtsOutput.Add(times[t], mts.ValueAtTime(times[t]), true); } } else { int thisDim = mts.Dimension; mvtsOutput = new MVTimeSeries(thisDim + refDim); for (int t = 0; t < times.Count; ++t) { var val = new double[thisDim + refDim]; var dv = mts.ValueAtTime(times[t]); for (int i = 0; i < thisDim; ++i) { val[i] = dv[i]; } for (int i = 0; i < refDim; ++i) { val[i + thisDim] = mvref != null ? mvref[t][i] : uref[t]; } mvtsOutput.Add(times[t], val, true); } } IsValid = true; } }
public override double LogLikelihood(Vector <double> parameter, double penaltyFactor, bool fillOutputs) { if (mvts == null) { return(double.NaN); } Vector <double> paramBak = Parameters; if (parameter != null) { Parameters = parameter; } var mvn = new MVNormalDistribution { Mu = mu, Sigma = Sigma }; double logLike = 0.0; for (int t = 0; t < mvts.Count; ++t) { Vector <double> pred = OneStepPredictor(t); Vector <double> resid = Vector <double> .Build.DenseOfArray(mvts[t]) - pred; logLike += mvn.LogProbabilityDensity(resid); } if (fillOutputs) { GoodnessOfFit = logLike; oneStepPredictors = new MVTimeSeries(dimension) { Title = mvts.Title }; oneStepPredictorsAtAvail = new MVTimeSeries(dimension) { Title = mvts.Title }; var rs = new MVTimeSeries(dimension) { Title = mvts.Title }; if (mvts.SubTitle != null) { oneStepPredictors.SubTitle = new string[dimension]; oneStepPredictorsAtAvail.SubTitle = new string[dimension]; rs.SubTitle = new string[dimension]; for (int i = 0; i < dimension; ++i) { if (mvts.SubTitle[i] != null) { oneStepPredictors.SubTitle[i] = mvts.SubTitle[i] + "[Pred]"; oneStepPredictorsAtAvail.SubTitle[i] = mvts.SubTitle[i] + "[Pred.AA]"; rs.SubTitle[i] = mvts.SubTitle[i] + "[Resid]"; } } } for (int t = 0; t < mvts.Count; ++t) { Vector <double> pred = OneStepPredictor(t); Vector <double> resid = Vector <double> .Build.DenseOfArray(mvts[t]) - pred; rs.Add(mvts.TimeStamp(t), mvn.Standardize(resid).ToArray(), false); oneStepPredictors.Add(mvts.TimeStamp(t), pred.ToArray(), false); if (t > 0) { oneStepPredictorsAtAvail.Add(mvts.TimeStamp(t - 1), pred.ToArray(), false); } } oneStepPredictorsAtAvail.Add(mvts.TimeStamp(mvts.Count - 1), OneStepPredictor(mvts.Count).ToArray(), false); Residuals = rs; } if (parameter != null) { Parameters = paramBak; } return(logLike); }
public override void Recompute() { IsValid = false; if (GetInputType(-1) != InputType.MultivariateTS) { return; } var series = GetInputBundle(); if (series.Count != 4 && series.Count != 5) { return; // something wrong!, needs open,high,low,close (and optional volume) } var open = series[0]; var high = series[1]; var low = series[2]; var close = series[3]; TimeSeries volume = series.Count == 5 ? series[4] : null; combined = new MVTimeSeries(series.Count); var accumulated = Vector <double> .Build.Dense(series.Count); accumulated[0] = open[0]; accumulated[1] = high[0]; accumulated[2] = low[0]; // low accumulated[3] = close[0]; if (series.Count == 5) { accumulated[4] = 0; } DateTime initialTime = PhaseAdjust(open.TimeStamp(0)); // initial time for current bar for (int t = 0; t < open.Count; ++t) { TimeSpan intoBar = open.TimeStamp(t) - initialTime; if (intoBar >= Period) { // dump the previous bar and reset things combined.Add(initialTime + Period, accumulated.ToArray(), false); accumulated = Vector <double> .Build.Dense(series.Count); accumulated[0] = open[t]; accumulated[1] = high[t]; accumulated[2] = low[t]; accumulated[3] = close[t]; if (volume != null) { accumulated[4] = 0; } initialTime = PhaseAdjust(open.TimeStamp(t)); } accumulated[3] = close[t]; accumulated[1] = Math.Max(accumulated[1], high[t]); accumulated[2] = Math.Min(accumulated[2], low[t]); if (volume != null) { accumulated[4] += volume[t]; } } IsValid = true; }
public override void Recompute() { var mts = GetInput(0) as MVTimeSeries; var uts = GetInput(0) as TimeSeries; var lts = GetInput(0) as Longitudinal; if (Spacing == 0) { Spacing = 1; // just fix it here: this is for backwards-compatibility } mvDifferences = null; differences = null; longDifferences = null; IsValid = false; if (mts != null) { mvDifferences = new MVTimeSeries(mts.Dimension) { Title = "Diffs" }; for (int i = 0; i < mts.Dimension; ++i) { mvDifferences.SubTitle[i] = mts.SubTitle[i]; } var zerov = new double[mts.Dimension]; if (!LeftTimeStamps && PadWithZeroes) { for (int t = 0; t < Lag; ++t) { if (t % Spacing == Phase) { mvDifferences.Add(mts.TimeStamp(t), zerov, false); } } } for (int t = Phase; t < mts.Count; t += Spacing) { if (t >= Lag) { var diff = new double[mts.Dimension]; for (int j = 0; j < mts.Dimension; ++j) { diff[j] = mts[t][j] - mts[t - Lag][j]; } var stamp = LeftTimeStamps ? mts.TimeStamp(t - Lag) : mts.TimeStamp(t); mvDifferences.Add(stamp, diff, false); } } if (LeftTimeStamps && PadWithZeroes) { for (int t = mts.Count - Lag; t < mts.Count; ++t) { if (t % Spacing == Phase) { mvDifferences.Add(mts.TimeStamp(t), zerov, false); } } } } if (uts != null) { differences = new TimeSeries { Title = "Diff(" + uts.Title + ")" }; if (!LeftTimeStamps && PadWithZeroes) { for (int t = 0; t < Lag; ++t) { if (t % Spacing == Phase) { differences.Add(uts.TimeStamp(t), 0, false); } } } for (int t = Phase; t < uts.Count; t += Spacing) { if (t >= Lag) { var diff = uts[t] - uts[t - Lag]; var stamp = LeftTimeStamps ? uts.TimeStamp(t - Lag) : uts.TimeStamp(t); differences.Add(stamp, diff, false); } } if (LeftTimeStamps && PadWithZeroes) { for (int t = uts.Count - Lag; t < uts.Count; ++t) { if (t % Spacing == Phase) { differences.Add(uts.TimeStamp(t), 0, false); } } } } if (lts != null) { var segments = new List <TimeSeries>(lts.Count); for (int i = 0; i < lts.Count; ++i) { uts = lts[i]; var du = new TimeSeries(); for (int t = Lag; t < uts.Count; ++t) { double diff = uts[t] - uts[t - Lag]; var stamp = LeftTimeStamps ? uts.TimeStamp(t - Lag) : uts.TimeStamp(t); du.Add(stamp, diff, false); } segments.Add(du); } longDifferences = new Longitudinal(segments); } IsValid = true; }