|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Runtime.Serialization; |
| 8 | +using Elasticsearch.Net; |
| 9 | +using Elasticsearch.Net.Utf8Json; |
| 10 | + |
| 11 | +namespace Nest |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A metrics aggregation that performs a statistical hypothesis test in which the test statistic follows a |
| 15 | + /// Student’s t-distribution under the null hypothesis on numeric values extracted from the aggregated documents or |
| 16 | + /// generated by provided scripts. In practice, this will tell you if the difference between two population means |
| 17 | + /// are statistically significant and did not occur by chance alone. |
| 18 | + /// <para /> |
| 19 | + /// Available in Elasticsearch 7.8.0+ with at least basic license level |
| 20 | + /// </summary> |
| 21 | + [InterfaceDataContract] |
| 22 | + [ReadAs(typeof(TTestAggregation))] |
| 23 | + public interface ITTestAggregation : IAggregation |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// T-test population A |
| 27 | + /// </summary> |
| 28 | + [DataMember(Name= "a")] |
| 29 | + public ITTestPopulation A { get; set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// T-test population B |
| 33 | + /// </summary> |
| 34 | + [DataMember(Name= "b")] |
| 35 | + public ITTestPopulation B { get; set; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// T-test type |
| 39 | + /// </summary> |
| 40 | + [DataMember(Name = "type")] |
| 41 | + public TTestType? Type { get; set; } |
| 42 | + } |
| 43 | + |
| 44 | + /// <inheritdoc cref="ITTestAggregation" /> |
| 45 | + public class TTestAggregation : AggregationBase, ITTestAggregation |
| 46 | + { |
| 47 | + internal TTestAggregation() { } |
| 48 | + |
| 49 | + public TTestAggregation(string name) : base(name) { } |
| 50 | + |
| 51 | + internal override void WrapInContainer(AggregationContainer c) => c.TTest = this; |
| 52 | + |
| 53 | + /// <inheritdoc /> |
| 54 | + public ITTestPopulation A { get; set; } |
| 55 | + /// <inheritdoc /> |
| 56 | + public ITTestPopulation B { get; set; } |
| 57 | + /// <inheritdoc /> |
| 58 | + public TTestType? Type { get; set; } |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc cref="ITTestAggregation" /> |
| 62 | + public class TTestAggregationDescriptor<T> |
| 63 | + : DescriptorBase<TTestAggregationDescriptor<T>, ITTestAggregation>, ITTestAggregation |
| 64 | + where T : class |
| 65 | + { |
| 66 | + IDictionary<string, object> IAggregation.Meta { get; set; } |
| 67 | + string IAggregation.Name { get; set; } |
| 68 | + ITTestPopulation ITTestAggregation.A { get; set; } |
| 69 | + ITTestPopulation ITTestAggregation.B { get; set; } |
| 70 | + TTestType? ITTestAggregation.Type { get; set; } |
| 71 | + |
| 72 | + /// <inheritdoc cref="ITTestAggregation.A"/> |
| 73 | + public TTestAggregationDescriptor<T> A(Func<TTestPopulationDescriptor<T>, ITTestPopulation> selector) => |
| 74 | + Assign(selector, (a, v) => a.A = v?.Invoke(new TTestPopulationDescriptor<T>())); |
| 75 | + |
| 76 | + /// <inheritdoc cref="ITTestAggregation.A"/> |
| 77 | + public TTestAggregationDescriptor<T> A<TOther>(Func<TTestPopulationDescriptor<TOther>, ITTestPopulation> selector) where TOther : class => |
| 78 | + Assign(selector, (a, v) => a.A = v?.Invoke(new TTestPopulationDescriptor<TOther>())); |
| 79 | + |
| 80 | + /// <inheritdoc cref="ITTestAggregation.B"/> |
| 81 | + public TTestAggregationDescriptor<T> B(Func<TTestPopulationDescriptor<T>, ITTestPopulation> selector) => |
| 82 | + Assign(selector, (a, v) => a.B = v?.Invoke(new TTestPopulationDescriptor<T>())); |
| 83 | + |
| 84 | + /// <inheritdoc cref="ITTestAggregation.B"/> |
| 85 | + public TTestAggregationDescriptor<T> B<TOther>(Func<TTestPopulationDescriptor<TOther>, ITTestPopulation> selector) where TOther : class => |
| 86 | + Assign(selector, (a, v) => a.B = v?.Invoke(new TTestPopulationDescriptor<TOther>())); |
| 87 | + |
| 88 | + /// <inheritdoc cref="ITTestAggregation.Type"/> |
| 89 | + public TTestAggregationDescriptor<T> Type(TTestType? type) => Assign(type, (a, v) => a.Type = v); |
| 90 | + |
| 91 | + /// <inheritdoc cref="IAggregation.Meta"/> |
| 92 | + public TTestAggregationDescriptor<T> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) => |
| 93 | + Assign(selector, (a, v) => a.Meta = v?.Invoke(new FluentDictionary<string, object>())); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// The type of t-test |
| 98 | + /// </summary> |
| 99 | + [StringEnum] |
| 100 | + public enum TTestType |
| 101 | + { |
| 102 | + /// <summary> |
| 103 | + /// performs paired t-test |
| 104 | + /// </summary> |
| 105 | + [EnumMember(Value = "paired")] |
| 106 | + Paired, |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// performs two-sample equal variance test |
| 110 | + /// </summary> |
| 111 | + [EnumMember(Value = "homoscedastic")] |
| 112 | + Homoscedastic, |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// performs two-sample unequal variance test (this is default) |
| 116 | + /// </summary> |
| 117 | + [EnumMember(Value = "heteroscedastic")] |
| 118 | + Heteroscedastic, |
| 119 | + } |
| 120 | +} |
0 commit comments