Ejemplo n.º 1
0
        /// <summary>
        /// Creates the weather matrix. One bool neccessary!
        /// </summary>
        /// <param name="weatherSource">The weather source evaluated in Engine</param>
        /// <param name="wc">The wc.</param>
        /// <exception cref="System.Exception">No column chosen</exception>
        private void CreateWeatherMatrix(string weatherSource, WeatherColumns wc)
        {
            string weatherMatrixString = WEATHERMATRIX + " <- matrix(c(";
            int    cols = 0;

            if (wc.pressure)
            {
                weatherMatrixString += weatherSource + "$Pressure,";
                cols++;
            }
            if (wc.rain)
            {
                weatherMatrixString += weatherSource + "$Rain,";
                cols++;
            }
            if (wc.windSpeed)
            {
                weatherMatrixString += weatherSource + "$WindSpeed,";
                cols++;
            }
            if (wc.temperature)
            {
                weatherMatrixString += weatherSource + "$Temperature,";
                cols++;
            }
            if (wc.solar)
            {
                weatherMatrixString += weatherSource + "$Solar,";
                cols++;
            }
            if (wc.humitdity)
            {
                weatherMatrixString += weatherSource + "$Humidity,";
                cols++;
            }
            if (cols == 0)
            {
                log.Debug("weatherMatrixString: NULL");
                rEngine.Evaluate(WEATHERMATRIX + "<-NULL");
                return;
            }

            weatherMatrixString  = weatherMatrixString.Remove(weatherMatrixString.Length - 1, 1);
            weatherMatrixString += "),";                            //c end

            weatherMatrixString += "ncol=" + cols.ToString() + ")"; //matrix end

            log.Debug("weatherMatrixString:" + weatherMatrixString);
            rEngine.Evaluate(weatherMatrixString);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Models the change.
        /// </summary>
        /// <param name="JSONmodelWithWeather">The jso nmodel with weather.</param>
        /// <param name="modelName">Name of the model.</param>
        /// <param name="wc">The wc.</param>
        /// <exception cref="System.Exception">ARIMA error</exception>
        public void modelChange(string JSONmodelWithWeather, string modelName, WeatherColumns wc)
        {
            this.Init();
            //
            string modelData = "modelData";
            string evaluate  = string.Format("{0} <-fromJSON(\'{1}\')", modelData, JSONmodelWithWeather);

            //log.Debug("evaluateString:" + evaluate);
            rEngine.Evaluate(evaluate);

            string weatherModel = "modelData";

            //evaluate = string.Format("{0} <-fromJSON(\'{1}\')", weatherModel, JSONweather);
            //log.Debug("evaluateString:" + evaluate);
            //rEngine.Evaluate(evaluate);

            CreateWeatherMatrix(modelData, wc);

            //log.Debug(engine.Evaluate("data"));
            //engine.Evaluate("data <- predata[,1]");
            evaluate = string.Format(modelName + "<- auto.arima({0}$Amount,xreg={1})", modelData, WEATHERMATRIX);
            SymbolicExpression model;

            try
            {
                model = rEngine.Evaluate(evaluate);
            }
            catch (Exception ex)
            {
                throw new Exception("ARIMA error");
            }

            //foreach (var item in model.AsList())
            //{
            //    log.Debug(item.ToString());
            //}

            var coef = model.AsList()["coef"].AsList();

            int lengthData = rEngine.Evaluate(modelData + "$Amount").AsNumeric().Length;

            double[] dataSeries  = new double[lengthData];
            double[] errorSeries = new double[lengthData];

            model.AsList()["x"].AsNumeric().CopyTo(dataSeries, lengthData);
            model.AsList()["residuals"].AsNumeric().CopyTo(errorSeries, lengthData);
            modelWeatherColumns = wc;
            //residuals
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Finds the outliers.
        /// </summary>
        /// <param name="fittedDataName">Name of the fitted data.</param>
        /// <param name="wc">The wc.</param>
        /// <returns></returns>
        public List <Outlier> findOutliers(string fittedDataName, WeatherColumns wc)
        {
            string evaluate;

            evaluate = string.Format("resid <- residuals({0})", fittedDataName);
            var resid = rEngine.Evaluate(evaluate);

            evaluate = string.Format("pars <- coefs2poly({0})", fittedDataName);
            var pars = rEngine.Evaluate(evaluate);

            evaluate = string.Format("outliers <- locate.outliers(resid, pars)");
            var outliers = rEngine.Evaluate(evaluate).AsList();

            List <Outlier> retList = new List <Outlier>();

            for (int i = 0; i < outliers[0].AsList().Length; i++)
            {
                Outlier o = new Outlier();
                o.seriesNumber = (int)outliers[1].AsVector()[i];
                var date = rEngine.Evaluate(fittedDataName).AsList();
                // o.IDDate = date.
                o.weatherDependency.Add(wc);
                o.outlierness.Add((double)outliers[2].AsVector()[i]);
                o.tStats.Add((double)outliers[3].AsVector()[i]);

                retList.Add(o);
                //outliers[0].AsVector().ToArray()[i];//type
            }

            //foreach (var outlierColumn in outliers)
            //{
            //    var outVector = outlierColumn.AsDataFrame();
            //    var outVector2 = outlierColumn.AsList();
            //    var outVector3 = outlierColumn.AsVector();
            //    var outVector4 = outlierColumn.AsVector();
            //}

            return(retList);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Explores the defined property.
        /// </summary>
        /// <param name="JSONdataWithWeather">The jso ndata with weather.</param>
        /// <param name="JSONmodelWithWeather">The jso nmodel with weather.</param>
        /// <param name="outlierList">The outlier list.</param>
        /// <param name="wc">The wc.</param>
        /// <returns></returns>
        private List <Outlier> ExploreDefinedProperty(string JSONdataWithWeather, string JSONmodelWithWeather, List <Outlier> outlierList, WeatherColumns wc)
        {
            string fittedDataName   = "feature_data_" + wc.ToString();;
            string fittingModelName = "feature_model_" + wc.ToString();

            modelChange(JSONmodelWithWeather, fittingModelName, wc);

            fitSeriesToModel(JSONdataWithWeather, fittingModelName, fittedDataName, wc);

            //HashSet<int> oulierSet = new HashSet<int>();

            List <Outlier> returnList = findOutliers(fittedDataName, wc);

            foreach (Outlier o in returnList)
            {
                if (!outlierList.Exists(x => x.seriesNumber.Equals(o.seriesNumber)))
                {
                    continue;
                }
                outlierList.Single(x => x.seriesNumber.Equals(o.seriesNumber))
                .Add(o.weatherDependency.First(), o.outlierness.First(), o.tStats.First());
            }
            return(outlierList);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Finds the local properties.
        /// </summary>
        /// <param name="JSONdataWithWeather">The jso ndata with weather.</param>
        /// <param name="modelName">Name of the model.</param>
        /// <param name="weatherColumns">The weather columns.</param>
        /// <param name="outlierList">The outlier list.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">outlierList not defined</exception>
        public List <Outlier> FindLocalProperties(string JSONdataWithWeather, string modelName, WeatherColumns weatherColumns, List <Outlier> outlierList)
        {
            if (outlierList == null)
            {
                throw new Exception("outlierList not defined");
            }

            if (weatherColumns.temperature == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    temperature = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            if (weatherColumns.solar == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    solar = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            if (weatherColumns.pressure == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    pressure = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            if (weatherColumns.rain == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    rain = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            if (weatherColumns.windSpeed == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    windSpeed = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            if (weatherColumns.humitdity == true)
            {
                WeatherColumns wc = new WeatherColumns()
                {
                    humitdity = true
                };
                outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
            }
            //noneWeatherDependency
            {
                WeatherColumns wc = new WeatherColumns();
                if (!outlierList.Exists(o => o.weatherDependency.Contains(wc)))
                {
                    outlierList = ExploreDefinedProperty(JSONdataWithWeather, modelName, outlierList, wc);
                }
            }

            return(outlierList);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Fits the series to model.
        /// </summary>
        /// <param name="JSONdataWithWeather">The jso ndata with weather.</param>
        /// <param name="modelName">Name of the model.</param>
        /// <param name="fittedDataName">Name of the fitted data.</param>
        /// <param name="weatherColumns">The weather columns.</param>
        /// <returns></returns>
        public List <Consuption> fitSeriesToModel(string JSONdataWithWeather, string modelName, string fittedDataName, WeatherColumns weatherColumns)
        {
            string       evaluate;
            const string fittingDataName = "dataName";

            evaluate = string.Format("{0} <-fromJSON(\'{1}\')", fittingDataName, JSONdataWithWeather);
            var sourceData = rEngine.Evaluate(evaluate);

            CreateWeatherMatrix(fittingDataName, weatherColumns);

            evaluate = string.Format("{0} <- forecast::Arima({1}$Amount,xreg = {2},model={3})", fittedDataName, fittingDataName, WEATHERMATRIX, modelName);
            //evaluate = string.Format("{0} <- forecast::Arima({1}$Amount,xreg = {2})", fittedDataName, fittingDataName, WEATHERMATRIX, modelName);
            var result = rEngine.Evaluate(evaluate);

            GenericVector resultList       = result.AsList();
            var           fittedValuesList = resultList["residuals"].AsNumeric().ToList();

            var sourceDates   = rEngine.Evaluate("sourceDates <- " + fittingDataName + "$IDDate").AsList();
            var sourceAmounts =
                rEngine.Evaluate("sourceDates <- " + fittingDataName + "$Amount").AsList();
            List <Consuption> retList = new List <Consuption>();

            for (int i = 0; i < fittedValuesList.Count; i++)
            {
                Consuption c = new Consuption();
                c.Amount = (float)fittedValuesList[i] + (float)sourceAmounts.AsNumeric().ElementAt(i);
                c.IDDate = sourceDates.AsInteger().ElementAt(i);
                retList.Add(c);
            }
            return(retList);
            //List<double> returnList = result.AsList();
        }
Ejemplo n.º 7
0
 public void Add(WeatherColumns wc, double o, double ts)
 {
     this.weatherDependency.Add(wc);
     this.outlierness.Add(o);
     this.tStats.Add(ts);
 }