Skip to content

Commit

Permalink
Adds CAI Methods
Browse files Browse the repository at this point in the history
- CreateModel
- Train
- Get Results
- Predict
  • Loading branch information
AlexCatarino committed Jul 9, 2024
1 parent 2897c7f commit afa2160
Show file tree
Hide file tree
Showing 10 changed files with 1,459 additions and 10 deletions.
531 changes: 531 additions & 0 deletions CAI_Demo_20231010.ipynb

Large diffs are not rendered by default.

247 changes: 247 additions & 0 deletions Models/ModelParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using QuantConnect.Util;

namespace QuantConnect.PredictNowNET.Models;

/// <summary>
/// Type of Analysis
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum Analysis
{
/// <summary>
/// No analysis
/// </summary>
None,
/// <summary>
///
/// </summary>
Small
}

/// <summary>
/// Type of Boost algorithms
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum Boost
{
/// <summary>
/// Dropouts meet Multiple Additive Regression Trees.
/// </summary>
Dart,
/// <summary>
/// Gradient-boosted decision trees
/// </summary>
Gbdt
}

/// <summary>
/// Feature selection
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum FeatureSelection
{
/// <summary>
/// No selection
/// </summary>
None,
/// <summary>
/// SHapley Additive exPlanations
/// </summary>
Shap,
/// <summary>
/// Computer-Mediated Discourse Analysis
/// </summary>
CMDA
}

/// <summary>
/// Model modes
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum Mode
{
/// <summary>
/// Train
/// </summary>
Train,
/// <summary>
/// Live
/// </summary>
Live
}

/// <summary>
/// Model type
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ModelType
{
/// <summary>
/// Classification
/// </summary>
Classification,
/// <summary>
/// Regression
/// </summary>
Regression
}

/// <summary>
/// Represents parameters for both backtests and live prediction
/// </summary>
public class ModelParameters
{
/// <summary>
/// True if use timeseries
/// </summary>
[JsonProperty(PropertyName = "timeseries")]
[JsonConverter(typeof(YesNoJsonConverter))]
public bool Timeseries { get; private set; }

/// <summary>
/// The type of the mode. For example regression or classification
/// </summary>
[JsonProperty(PropertyName = "type")]
public ModelType Type { get; private set; }

/// <summary>
/// The feature selection
/// </summary>
[JsonProperty(PropertyName = "feature_selection")]
public FeatureSelection FeatureSelection { get; private set; }

/// <summary>
/// The analysys output
/// </summary>
[JsonProperty(PropertyName = "analysis")]
public Analysis Analysis { get; private set; }

/// <summary>
/// Select the Boost algorithm
/// </summary>
[JsonProperty(PropertyName = "boost")]
public Boost Boost { get; private set; }

/// <summary>
/// The model mode
/// </summary>
[JsonProperty(PropertyName = "mode")]
public Mode Mode { get; }

/// <summary>
/// The size of the test sample. If less than 1 --> ratio, otherwise --> exact # of rows
/// </summary>
[JsonProperty(PropertyName = "testsize")]
public string Testsize { get; private set; }

/// <summary>
/// Define if should use weights: yes, no, or custom
/// </summary>
[JsonProperty(PropertyName = "weights")]
public string Weights { get; private set; }

/// <summary>
/// True if should refine the probability
/// </summary>
[JsonProperty(PropertyName = "prob_calib")]
[JsonConverter(typeof(YesNoJsonConverter))]
public bool ProbabilityCalibration { get; private set; }

/// <summary>
/// True if should use exploratory data analysis
/// </summary>
[JsonProperty(PropertyName = "eda")]
[JsonConverter(typeof(YesNoJsonConverter))]
public bool ExploratoryDataAnalysis { get; private set; }

/// <summary>
/// Random seed for initialization
/// </summary>
[JsonProperty(PropertyName = "random_seed")]
public string RandomSeed { get; private set; } = "1";

/// <summary>
/// Define custom weights
/// </summary>
[JsonProperty(PropertyName = "custom_weights")]
public string CustomWeights { get; private set; }

/// <summary>
/// Create a new instance of ModelParameters
/// </summary>
/// <param name="mode">The model mode</param>
/// <param name="type">The type of the mode. For example regression or classification</param>
/// <param name="featureSelection">The feature selection</param>
/// <param name="analysis">The analysys output</param>
/// <param name="boost">Select the Boost algorithm</param>
/// <param name="testsize">The size of the test sample. If less than 1 --> ratio, otherwise --> exact # of rows</param>
/// <param name="timeseries">True if use timeseries</param>
/// <param name="probabilityCalibration">True if should refine the probability</param>
/// <param name="exploratoryDataAnalysis">True if should use exploratory data analysis</param>
/// <param name="weights">Define if should use weights: yes, no, or custom</param>
/// <param name="customWeights">Define custom weights</param>
/// <param name="randomSeed">Random seed for initialization</param>
public ModelParameters(Mode mode, ModelType type, FeatureSelection featureSelection, Analysis analysis, Boost boost, double testsize, bool timeseries, bool probabilityCalibration, bool exploratoryDataAnalysis, string weights, string customWeights= "", double randomSeed = 1)
{
Mode = mode;
Type = type;
FeatureSelection = featureSelection;
Analysis = analysis;
Boost = boost;
Testsize = testsize.ToString();
Timeseries = timeseries;
ProbabilityCalibration = probabilityCalibration;
ExploratoryDataAnalysis = exploratoryDataAnalysis;
Weights = weights;
CustomWeights = customWeights;
RandomSeed = randomSeed.ToString();
}

/// <summary>
/// Returns a string that represents the current object
/// </summary>
/// <returns>A string that represents the current object</returns>
public override string ToString() => JsonConvert.SerializeObject(this);
}

/// <summary>
/// Defines how bool should be serialized to json
/// </summary>
public class YesNoJsonConverter : TypeChangeJsonConverter<bool, string>
{
/// <summary>
/// Convert the input value to a value to be serialized
/// </summary>
/// <param name="value">The input value to be converted before serialization</param>
/// <returns>A new instance of TResult that is to be serialized</returns>
protected override string Convert(bool value) => value ? "yes" : "no";

/// <summary>
/// Converts the input value to be deserialized
/// </summary>
/// <param name="value">The deserialized value that needs to be converted to T</param>
/// <returns>The converted value</returns>
protected override bool Convert(string value) => value.ToLower() switch
{
"yes" => true,
"no" => false,
_ => throw new ArgumentOutOfRangeException(nameof(value), $"Not expected value: {value}")
};
}
48 changes: 48 additions & 0 deletions Models/ModelResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Newtonsoft.Json;

namespace QuantConnect.PredictNowNET.Models;

/// <summary>
/// Base model response
/// </summary>
public class ModelResponse
{
/// <summary>
/// Model Name
/// </summary>
[JsonProperty(PropertyName = "model_name")]
public string ModelName { get; internal set; } = string.Empty;

/// <summary>
/// Result Message
/// </summary>
[JsonProperty(PropertyName = "message")]
public string Message { get; internal set; } = string.Empty;

/// <summary>
/// True if sucessfull
/// </summary>
[JsonProperty(PropertyName = "success")]
public bool Success { get; internal set; }

/// <summary>
/// Returns a string that represents the current object
/// </summary>
/// <returns>A string that represents the current object</returns>
public override string ToString() => JsonConvert.SerializeObject(this);
}
87 changes: 87 additions & 0 deletions Models/PredictResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Newtonsoft.Json;

namespace QuantConnect.PredictNowNET.Models;

/// <summary>
/// Model Prediction Result
/// </summary>
public class PredictResult
{
/// <summary>
/// True if should use exploratory data analysis
/// </summary>
[JsonProperty(PropertyName = "eda")]
public string ExploratoryDataAnalysis { get; internal set; } = string.Empty;

/// <summary>
/// Name of the file with the prediction
/// </summary>
[JsonProperty(PropertyName = "filename")]
public string Filename { get; internal set; } = string.Empty;

/// <summary>
/// Labels used in the prediction
/// </summary>
[JsonProperty(PropertyName = "labels")]
public string Labels { get; internal set; } = string.Empty;

/// <summary>
/// Objective function
/// </summary>
[JsonProperty(PropertyName = "objective")]
public string Objective { get; internal set; } = string.Empty;

/// <summary>
/// True if should refine the probability
/// </summary>
[JsonProperty(PropertyName = "prob_calib")]
public string ProbabilityCalibration { get; internal set; } = string.Empty;

/// <summary>
/// Probabilities
/// </summary>
[JsonProperty(PropertyName = "probabilities")]
public string Probabilities { get; internal set; } = string.Empty;

/// <summary>
/// Title
/// </summary>
[JsonProperty(PropertyName = "title")]
public string Title { get; internal set; } = string.Empty;

/// <summary>
///
/// </summary>
[JsonProperty(PropertyName = "too_many_nulls_list")]
public string TooManyNullsList { get; internal set; } = string.Empty;

/// <summary>
/// Result of the Prediction
/// </summary>
/// <param name="title"></param>
public PredictResult(string title)
{
Title = title;
}

/// <summary>
/// Returns a string that represents the current object
/// </summary>
/// <returns>A string that represents the current object</returns>
public override string ToString() => JsonConvert.SerializeObject(this);
}
Loading

0 comments on commit afa2160

Please sign in to comment.