001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.sandbox.ml.linear.learner.stream.experiments;
031
032import java.io.IOException;
033import java.net.MalformedURLException;
034import java.util.Arrays;
035import java.util.List;
036import java.util.Map;
037
038import org.openimaj.demos.sandbox.ml.linear.learner.stream.IncrementalLearnerFunction;
039import org.openimaj.demos.sandbox.ml.linear.learner.stream.IncrementalLearnerWorldSelectingEvaluator;
040import org.openimaj.demos.sandbox.ml.linear.learner.stream.ModelStats;
041import org.openimaj.demos.sandbox.ml.linear.learner.stream.StockPriceAggregator;
042import org.openimaj.demos.sandbox.ml.linear.learner.stream.twitter.CountryCodeNameStrategy;
043import org.openimaj.demos.sandbox.ml.linear.learner.stream.twitter.USMFStatusBagOfWords;
044import org.openimaj.demos.sandbox.ml.linear.learner.stream.twitter.USMFTickMongoDBQueryStream;
045import org.openimaj.ml.linear.evaluation.SumLossEvaluator;
046import org.openimaj.ml.linear.learner.BilinearLearnerParameters;
047import org.openimaj.ml.linear.learner.init.FirstValueInitStrat;
048import org.openimaj.ml.linear.learner.init.SingleValueInitStrat;
049import org.openimaj.ml.linear.learner.init.SparseZerosInitStrategy;
050import org.openimaj.tools.twitter.modes.preprocessing.StopwordMode;
051import org.openimaj.twitter.USMFStatus;
052import org.openimaj.util.data.Context;
053import org.openimaj.util.function.Operation;
054import org.openimaj.util.function.context.ContextFunctionAdaptor;
055import org.openimaj.util.stream.window.WindowAverage;
056
057import com.mongodb.ServerAddress;
058
059/**
060 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
061 *
062 */
063public class GeoFinancialMongoStreamLearningExperiment {
064        /**
065         * @param args
066         * @throws MalformedURLException
067         * @throws IOException
068         */
069        public static void main(String[] args) throws MalformedURLException, IOException {
070                BilinearLearnerParameters params = new BilinearLearnerParameters();
071                params.put(BilinearLearnerParameters.ETA0_U, 0.02);
072                params.put(BilinearLearnerParameters.ETA0_W, 0.02);
073                params.put(BilinearLearnerParameters.LAMBDA, 0.001);
074                params.put(BilinearLearnerParameters.BICONVEX_TOL, 0.01);
075                params.put(BilinearLearnerParameters.BICONVEX_MAXITER, 10);
076                params.put(BilinearLearnerParameters.BIAS, true);
077                params.put(BilinearLearnerParameters.ETA0_BIAS, 0.5);
078                params.put(BilinearLearnerParameters.WINITSTRAT, new SingleValueInitStrat(0.1));
079                params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy());
080                params.put(BilinearLearnerParameters.EXPANDEDUINITSTRAT, new SparseZerosInitStrategy());
081                params.put(BilinearLearnerParameters.EXPANDEDWINITSTRAT, new SingleValueInitStrat(0.05));
082                FirstValueInitStrat biasInitStrat = new FirstValueInitStrat();
083                params.put(BilinearLearnerParameters.BIASINITSTRAT, biasInitStrat);
084
085                List<ServerAddress> serverList = Arrays.asList(
086                                new ServerAddress("rumi", 27017),
087                                new ServerAddress("hafez", 27017)
088                                );
089                // Get the USMF status objects and financial ticks from the mongodb
090                new USMFTickMongoDBQueryStream(serverList,"searchapi_yahoo_billgeo")
091                // Transform the usmf status instances to bags of words
092                .map(
093                        new ContextFunctionAdaptor<List<USMFStatus>, Map<String,Map<String,Double>>>(
094                                new USMFStatusBagOfWords(
095                                        new StopwordMode(),
096                                        new CountryCodeNameStrategy()
097                                ),"usmfstatuses",
098                                "bagofwords"
099                        )
100                )
101                // transform the financial ticks to the average tick
102                .map(
103                        new ContextFunctionAdaptor<List<Map<String,Double>>,Map<String,Double>>(new WindowAverage(),"ticks","averageticks")
104                )
105                // Group together identical stock ticks
106                .transform(new StockPriceAggregator(0.0001))
107                // Train the model
108                .map(
109                        new IncrementalLearnerWorldSelectingEvaluator(
110                                new SumLossEvaluator(),
111                                new IncrementalLearnerFunction(params)
112                        )
113                )
114                // Consume the model statistics
115                .forEach(new Operation<Context>() {
116
117                        @Override
118                        public void perform(Context context) {
119                                ModelStats object = context.getTyped("modelstats");
120                                object.printSummary();
121                        }
122                });
123
124        }
125}