⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeseriesminingmodel.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * Title: XELOPES Data Mining Library
 * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
 * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
 * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
 * @author Michael Thess
 * @version 1.0
 */
package com.prudsys.pdm.Models.TimeSeriesPredict;

import java.io.*;

import com.prudsys.pdm.Core.*;
import com.prudsys.pdm.Input.*;
import com.prudsys.pdm.Transform.Special.*;
import com.prudsys.pdm.Models.Supervised.*;

/**
 * Model of multivariate time series prediction methods.
 */
public class TimeSeriesMiningModel extends MiningModel
{
    private SupervisedMiningModel[] approximator;

    private int embeddingDimension = 5;

    private int stepSize = 1;

    private boolean singleApproximator = true;

    /**
     * Constructor sets function and algorithm parameters.
     */
    public TimeSeriesMiningModel()
    {
      function  = MiningModel.TIME_SERIES_PREDICTION_FUNCTION;
      algorithm = MiningModel.MULTIDIMENSIONAL_TIME_SERIES_ALGORITHM;
    }

    /**
     * Returns string representation of time series mining model.
     *
     * @return string representation of time series mining model
     */
    public String toString()
    {
        return "Time series mining model";
    }

    /**
     * Returns HTML string representation of time series mining model.
     *
     * @return HTML string representation of time series mining model
     */
    public String toHtmlString()
    {
        return toString();
    }

    /**
     * Applies function of time series mining model to a mining vector.
     * Currently not supported.
     *
     * @param miningVector mining vector where the model should be applied
     * @return function value of the mining vector
     * @throws MiningException always thrown
     */
    public double applyModelFunction(MiningVector miningVector)
        throws MiningException {
      throw new MiningException("Not implemented.");
    }

    /**
     * Applying the time series mining model to a mining input stream.
     * The mining input stream must heave similar meta data as that
     * of which the model was created. Returns the mining vector
     * of the next time step.
     *
     * @param miningData mining input stream where the model should be applied
     * @return mining vector of next time step (same meta data as mining input stream)
     * @throws MiningException if there are some errors when model is applied
     */
    public MiningMatrixElement applyModel(MiningMatrixElement miningData)
        throws MiningException {

      if (approximator == null)
        throw new MiningException("No approximators for prediction");

      MiningInputStream mis = (MiningInputStream) miningData;
      int nVec = mis.getVectorsNumber();
      int nAtt = mis.getMetaData().getAttributesNumber();

      // Get mining vector of last block:
      BlockVectorStream bvs = new BlockVectorStream();
      bvs.setBlockLength(embeddingDimension);
      bvs.setStepSize(stepSize);
      MiningVector mv = bvs.transformBlockToVector(mis, nVec-embeddingDimension);

      // Extend the mining vector for target attribute:
      double[] val2 = new double[mv.getValues().length + 1];
      for (int j = 0; j < mv.getValues().length; j++)
        val2[j] = mv.getValue(j);
      MiningVector mv2 = new MiningVector(val2);

      MiningDataSpecification mds = mv.getMetaData();
      mds.addMiningAttribute( new NumericAttribute("dummyTarget" ));

      double[] values = new double[nAtt];
      for (int j = 0; j < nAtt; j++) {
        // Get current target attribute and add to meta data:
        MiningAttribute targetAttribute = ((SupervisedMiningSettings) approximator[j].getMiningSettings()).getTarget();
        mds.setMiningAttribute(mds.getAttributesNumber()-1, targetAttribute );
          mv2.setMetaData(mds);

        // Apply approximators:
        values[j] = approximator[j].applyModelFunction(mv2);
      };

      // Create result vector:
      MiningVector mvProg = new MiningVector(values);
      mvProg.setMetaData( mis.getMetaData() );

      return mvProg;
    }

    /**
     * Applying the time series mining model to a mining input stream
     * over some time steps. The mining input stream must heave similar
     * meta data as that of which the model was created. Returns the
     * mining input stream of the next time step.
     *
     * @param miningData mining input stream where the model should be applied
     * @param nextSteps number of next time steps to predict
     * @return mining input stream of next time steps (same meta data as mining input stream)
     * @throws MiningException if there are some errors when model is applied
     */
    public MiningMatrixElement applyModel(MiningMatrixElement miningData, int nextSteps)
        throws MiningException {

      if (approximator == null)
        throw new MiningException("No approximators for prediction");

      // Mining input stream:
      MiningInputStream mis = (MiningInputStream) miningData;

      // New prognosis data stream:
      MiningStoredData msd = new MiningStoredData();

      // Add data of last vector block of mis to msd:
      msd.updateSetMetaData( mis.getMetaData() );
      int startInd = mis.getVectorsNumber() - embeddingDimension;
      try {
        mis.move(startInd);
      }
      catch (MiningException ex) {            // Move method not supported
        mis.reset();
        for (int i = 0; i < startInd+1; i++)
          mis.next();
      };
      for (int i = 0; i < embeddingDimension; i++) {
        msd.updateAppendVector( mis.read() );
        mis.next();
      };

      // Move forward by recursive prediction:
      BlockVectorStream bvs = new BlockVectorStream();
      bvs.setBlockLength(embeddingDimension);
      bvs.setStepSize(stepSize);
      int nAtt = msd.getMetaData().getAttributesNumber();
      for (int i = 0; i < nextSteps; i++) {
        // Get meining vector of block i:
        MiningVector mv = bvs.transformBlockToVector(msd, i);

        // Create mining vector with target attribute:
        double[] val2 = new double[mv.getValues().length + 1];
        for (int j = 0; j < mv.getValues().length; j++)
          val2[j] = mv.getValue(j);
        MiningVector mv2 = new MiningVector(val2);
        double[] values = new double[nAtt];
        MiningDataSpecification mds = mv.getMetaData();
        mds.addMiningAttribute( new NumericAttribute("dummyTarget" ));
        for (int j = 0; j < nAtt; j++) {
          // Get current target attribute and add to meta data:
          MiningAttribute targetAttribute = ((SupervisedMiningSettings) approximator[j].getMiningSettings()).getTarget();
          mds.setMiningAttribute(mds.getAttributesNumber()-1, targetAttribute);
          mv2.setMetaData(mds);

          // Apply approximators:
          values[j] = approximator[j].applyModelFunction(mv2);
        };

        MiningVector mvProg = new MiningVector(values);
        mvProg.setMetaData( msd.getMetaData() );
        msd.updateAppendVector(mvProg);
      };

      // Remove vectors of first block:
      MiningStoredData msd2 = new MiningStoredData();
      msd2.updateSetMetaData( msd.getMetaData() );
      for (int i = embeddingDimension; i < msd.getVectorsNumber(); i++)
        msd2.updateAppendVector( msd.read(i) );

      return msd2;
    }

    /**
     * Creates PMML object from time series mining model.
     *
     * @return PMML object of time series mining model
     */
    public Object createPmmlObject() throws com.prudsys.pdm.Core.MiningException
    {
        throw new MiningException("not implemented");
    }

    /**
     * Gets time series mining model from PMML object.
     *
     * @param pmmlObject PMML object containing time series mining model
     */
    public void parsePmmlObject(Object pmmlObject) throws com.prudsys.pdm.Core.MiningException
    {
        throw new MiningException("not implemented");
    }

    /**
     * Creates PMML document from time series mining model.
     *
     * @param writer writer for PMML document
     */
    public void writePmml(Writer writer) throws com.prudsys.pdm.Core.MiningException
    {
        throw new MiningException("not implemented");
    }

    /**
     * Reads time series mining model from PMML document.
     *
     * @param reader reader of PMML document
     */
    public void readPmml(Reader reader) throws com.prudsys.pdm.Core.MiningException
    {
        throw new MiningException("not implemented");
    }

    /**
     * Writes time series mining model into plain text.
     *
     * @param writer writer for plain text
     */
    public void writePlainText(Writer writer) throws com.prudsys.pdm.Core.MiningException
    {
        throw new MiningException("not implemented");
    }

    /**
     * Sets new embedding dimension.
     *
     * @param embeddingDimension new enbedded dimension
     */
    public void setEmbeddingDimension(int embeddingDimension)
   {
      this.embeddingDimension = embeddingDimension;
   }

   /**
    * Returns embedding dimension.
    *
    * @return embedding dimension
    */
    public int getEmbeddingDimension()
   {
     return embeddingDimension;
   }

   /**
    * Returns step size.
    *
    * @return step size
    */
   public int getStepSize()
   {
     return stepSize;
   }

   /**
    * Sets new step size.
    *
    * @param stepSize new step size
    */
   public void setStepSize(int stepSize)
   {
     this.stepSize = stepSize;
   }

   /**
    * Use one approximator for all attributes.
    *
    * @return true if one approximator, false if multiple approximators
    */
    public boolean isSingleApproximator()
    {
      return singleApproximator;
    }

    /**
     * Set use of one or multiple approximators for all attributes.
     *
     * @param singleApproximator true if one approximator, false if multiple approximators
     */
    public void setSingleApproximator(boolean singleApproximator)
    {
      this.singleApproximator = singleApproximator;
    }

    /**
     * Returns approximator model.
     *
     * @return approximator model
     */
    public SupervisedMiningModel[] getApproximator()
    {
      return approximator;
    }

    /**
     * Sets approximator model.
     *
     * @param approximator new approximator model
     */
    public void setApproximator(SupervisedMiningModel[] approximator)
    {
      this.approximator = approximator;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -