Skip to content

Commit b24446d

Browse files
authored
Merge pull request #39 from dcwuser/dev/examples
Dev/examples
2 parents ccf034f + 29f1b87 commit b24446d

File tree

13 files changed

+297
-87
lines changed

13 files changed

+297
-87
lines changed

Examples/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
1313
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/Examples.dll",
14-
"args": ["EigenvaluesAndEigenvectors"],
14+
"args": [],
1515
"cwd": "${workspaceFolder}",
1616
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
1717
"console": "internalConsole",

Examples/Data.cs

Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,79 @@
99
using Meta.Numerics.Statistics;
1010
using Meta.Numerics.Statistics.Distributions;
1111

12+
using Newtonsoft.Json;
13+
1214
namespace Examples {
1315

1416
public static class Data {
1517

18+
public static void ConstructTestCsv () {
19+
20+
using (TextWriter writer = new StreamWriter(File.OpenWrite("test.csv"))) {
21+
writer.WriteLine("Id, Name, Sex, Birthdate, Height, Weight, Result");
22+
writer.WriteLine("1, John, M, 1970-01-02, 190.0, 75.0, True");
23+
writer.WriteLine("2, Mary, F, 1980-02-03, 155.0, 40.0, True");
24+
writer.WriteLine("3, Luke, M, 1990-03-04, 180.0, 60.0, False");
25+
}
26+
27+
}
28+
29+
[ExampleMethod]
30+
public static void ImportingData () {
31+
32+
FrameTable data;
33+
using (TextReader reader = File.OpenText("test.csv")) {
34+
data = FrameTable.FromCsv(reader);
35+
}
36+
37+
Console.WriteLine($"Imported CSV file with {data.Rows.Count} rows.");
38+
Console.WriteLine("The names and types of the columns are:");
39+
foreach (FrameColumn column in data.Columns) {
40+
Console.WriteLine($" {column.Name} of type {column.StorageType}");
41+
}
42+
43+
FrameTable titanic;
44+
Uri url = new Uri("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv");
45+
WebRequest request = WebRequest.Create(url);
46+
using (WebResponse response = request.GetResponse()) {
47+
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
48+
titanic = FrameTable.FromCsv(reader);
49+
}
50+
}
51+
52+
Uri jsonUrl = new Uri("https://raw.githubusercontent.com/dcwuser/metanumerics/master/Examples/Data/example.json");
53+
WebClient client = new WebClient();
54+
string input = client.DownloadString(jsonUrl);
55+
List<Dictionary<string,object>> output = JsonConvert.DeserializeObject<List<Dictionary<string,object>>>(input);
56+
FrameTable jsonExample = FrameTable.FromDictionaries(output);
57+
58+
// Define the schema.
59+
FrameTable table = new FrameTable();
60+
table.AddColumn<int>("Id");
61+
table.AddColumn<string>("Name");
62+
table.AddColumn<string>("Sex");
63+
table.AddColumn<DateTime>("Birthdate");
64+
table.AddColumn<double>("Height");
65+
table.AddColumn<double?>("Weight");
66+
table.AddColumn<bool>("Result");
67+
68+
// Add rows using as arrays of objects.
69+
table.AddRow(1, "John", "M", DateTime.Parse("1970-01-02"), 190.0, 75.0, true);
70+
table.AddRow(2, "Mary", "F", DateTime.Parse("1980-02-03"), 155.0, null, true);
71+
72+
// Add a row using a dictionary. This is more verbose, but very clear.
73+
table.AddRow(new Dictionary<string,object>(){
74+
{"Id", 3},
75+
{"Name", null},
76+
{"Sex", "M"},
77+
{"Birthdate", DateTime.Parse("1990-03-04")},
78+
{"Height", 180.0},
79+
{"Weight", 60.0},
80+
{"Result", false}
81+
});
82+
83+
}
84+
1685
[ExampleMethod]
1786
public static void ManipulatingData () {
1887

@@ -82,8 +151,18 @@ public static void AnalyzingData () {
82151
table = FrameTable.FromCsv(reader);
83152
}
84153
}
154+
FrameView view = table.WhereNotNull();
155+
156+
// Get the column with (zero-based) index 4.
157+
FrameColumn column4 = view.Columns[4];
158+
// Get the column named "Height".
159+
FrameColumn heightsColumn = view.Columns["Height"];
160+
// Even easier way to get the column named "Height".
161+
FrameColumn alsoHeightsColumn = view["Height"];
85162

86-
SummaryStatistics summary = new SummaryStatistics(table["Height"].As<double>());
163+
IReadOnlyList<double> heights = view["Height"].As<double>();
164+
165+
SummaryStatistics summary = new SummaryStatistics(view["Height"].As<double>());
87166
Console.WriteLine($"Count = {summary.Count}");
88167
Console.WriteLine($"Mean = {summary.Mean}");
89168
Console.WriteLine($"Standard Deviation = {summary.StandardDeviation}");
@@ -92,74 +171,77 @@ public static void AnalyzingData () {
92171
Console.WriteLine($"Estimated population standard deviation = {summary.PopulationStandardDeviation}");
93172

94173
IReadOnlyList<double> maleHeights =
95-
table.Where<string>("Sex", s => s == "M").Columns["Height"].As<double>();
174+
view.Where<string>("Sex", s => s == "M").Columns["Height"].As<double>();
96175
IReadOnlyList<double> femaleHeights =
97-
table.Where<string>("Sex", s => s == "F").Columns["Height"].As<double>();
176+
view.Where<string>("Sex", s => s == "F").Columns["Height"].As<double>();
98177
TestResult test = Univariate.StudentTTest(maleHeights, femaleHeights);
99-
Console.WriteLine($"{test.Statistic.Name} = {test.Statistic.Value}, P = {test.Probability}");
178+
Console.WriteLine($"{test.Statistic.Name} = {test.Statistic.Value}");
179+
Console.WriteLine($"P = {test.Probability}");
100180

101181
TestResult maleHeightNormality = maleHeights.ShapiroFranciaTest();
102-
TestResult totalHeightNormality = table["Height"].As<double>().ShapiroFranciaTest();
182+
TestResult totalHeightNormality = view["Height"].As<double>().ShapiroFranciaTest();
103183
TestResult heightCompatibility = Univariate.KolmogorovSmirnovTest(maleHeights, femaleHeights);
104184

105185
LinearRegressionResult fit =
106-
table["Weight"].As<double>().LinearRegression(table["Height"].As<double>());
186+
view["Weight"].As<double>().LinearRegression(view["Height"].As<double>());
107187
Console.WriteLine($"Model weight = ({fit.Slope}) * height + ({fit.Intercept}).");
108188
Console.WriteLine($"Model explains {fit.RSquared * 100.0}% of variation.");
109189

110190
ContingencyTable<string, bool> contingency =
111-
Bivariate.Crosstabs(table["Sex"].As<string>(), table["Result"].As<bool>());
191+
Bivariate.Crosstabs(view["Sex"].As<string>(), view["Result"].As<bool>());
112192
Console.WriteLine($"Male incidence: {contingency.ProbabilityOfColumnConditionalOnRow(true, "M")}");
113-
Console.WriteLine($"Female incidence: {contingency.ProbabilityOfColumnConditionalOnRow(false, "F")}");
193+
Console.WriteLine($"Female incidence: {contingency.ProbabilityOfColumnConditionalOnRow(true, "F")}");
114194
Console.WriteLine($"Log odds ratio = {contingency.Binary.LogOddsRatio}");
115195

116-
table.AddComputedColumn("Bmi", r => ((double) r["Weight"])/MoreMath.Sqr((double) r["Height"] / 100.0));
117-
table.AddComputedColumn("Age", r=> (DateTime.Now - (DateTime) r["Birthdate"]).TotalDays / 365.24);
196+
view.AddComputedColumn("Bmi", r => ((double) r["Weight"])/MoreMath.Sqr((double) r["Height"] / 100.0));
197+
view.AddComputedColumn("Age", r=> (DateTime.Now - (DateTime) r["Birthdate"]).TotalDays / 365.24);
118198

119199
MultiLinearLogisticRegressionResult result =
120-
table["Result"].As<bool>().MultiLinearLogisticRegression(
121-
table["Bmi"].As<double>(),
122-
table["Sex"].As<string, double>(s => s == "M" ? 1.0 : 0.0)
200+
view["Result"].As<bool>().MultiLinearLogisticRegression(
201+
view["Bmi"].As<double>(),
202+
view["Sex"].As<string, double>(s => s == "M" ? 1.0 : 0.0)
123203
);
124204
foreach (Parameter parameter in result.Parameters) {
125205
Console.WriteLine($"{parameter.Name} = {parameter.Estimate}");
126206
}
127207

128-
//TestResult ageResultPearson = Bivariate.PearsonRTest(table["Age"].As<double>(), table["Result"].As<double>());
129-
TestResult spearman = Bivariate.SpearmanRhoTest(table["Age"].As<double>(), table["Result"].As<double>());
208+
TestResult spearman = Bivariate.SpearmanRhoTest(view["Age"].As<double>(), view["Result"].As<double>());
130209
Console.WriteLine($"{spearman.Statistic.Name} = {spearman.Statistic.Value} P = {spearman.Probability}");
131210

132211
}
133212

134-
public static void ConstructData () {
213+
public static void ConstructExampleData () {
135214

136215
FrameTable table = new FrameTable();
137216
table.AddColumn<int>("Id");
138217
table.AddColumn<string>("Name");
139218
table.AddColumn<string>("Sex");
140219
table.AddColumn<DateTime>("Birthdate");
141-
table.AddColumns<double>("Height", "Weight");
220+
table.AddColumn<double>("Height");
221+
table.AddColumns<double?>("Weight");
142222
table.AddColumn<bool>("Result");
143223

144-
//Random rng = new Random(3);
145-
//Random rng = new Random(314159);
146-
// Random rng = new Random(271828);
147224
Random rng = new Random(1000001);
148225

149-
//string[] maleNames = new string[1024];
150226
string[] maleNames = new string[] {"Alex", "Chris", "David", "Eric", "Frederic", "George", "Hans", "Igor", "John", "Kevin", "Luke", "Mark", "Oscar", "Peter", "Richard", "Stephan", "Thomas", "Vincent" };
151227
AddRows(table, maleNames, "M", 175.0, 12.0, 24.0, 3.0, 1, rng);
152228

153-
//string[] femaleNames = new string[1024];
154229
string[] femaleNames = new string[] {"Anne", "Belle", "Dorothy", "Elizabeth", "Fiona", "Helen", "Julia", "Kate", "Louise", "Mary", "Natalie", "Olivia", "Ruth", "Sarah", "Theresa", "Viola" };
155230
AddRows(table, femaleNames, "F", 160.0, 10.0, 24.0, 3.0, 0, rng);
156231

157-
string path = @"C:\Users\dawright\Documents\example.csv";
232+
// add rows with nulls
233+
table.AddRow(table.Rows.Count, null, "M", DateTime.Parse("1970-07-27"), 183.0, 74.0, false);
234+
table.AddRow(table.Rows.Count, "Zoey", "F", DateTime.Parse("2007-09-17"), 138.0, null, false);
235+
236+
string path = @"example.csv";
158237
using (StreamWriter writer = new StreamWriter(File.OpenWrite(path))) {
159238
table.ToCsv(writer);
160239
}
161240
Console.WriteLine(File.Exists(path));
162241

242+
string json = JsonConvert.SerializeObject(table.ToDictionaries(), Formatting.Indented);
243+
File.WriteAllText("example.json", json);
244+
163245
}
164246

165247
private static void AddRows(FrameTable table, IReadOnlyList<string> names, string sex, double meanHeight, double stddevHeight, double meanBmi, double stddevBmi, int flag, Random rng) {

Examples/Distributions.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
using Meta.Numerics;
4+
using Meta.Numerics.Analysis;
5+
using Meta.Numerics.Functions;
6+
using Meta.Numerics.Statistics.Distributions;
7+
8+
namespace Examples {
9+
10+
public static class Distributions {
11+
12+
[ExampleMethod]
13+
public static void DistributionFunctions () {
14+
15+
ContinuousDistribution gumbel = new GumbelDistribution();
16+
17+
// Use PDF to compute absolute deviation
18+
IntegrationResult r = FunctionMath.Integrate(
19+
z => gumbel.ProbabilityDensity(z) * Math.Abs(z - gumbel.Mean),
20+
gumbel.Support
21+
);
22+
Console.WriteLine($"mean absolute deviation = {r.Value}");
23+
24+
// Shorter form
25+
double gumbelMad = gumbel.ExpectationValue(z => Math.Abs(z - gumbel.Mean));
26+
Console.WriteLine($"mean absolute deviation = {gumbelMad}");
27+
28+
double x = 1.5;
29+
30+
// PDF
31+
Console.WriteLine($"p({x}) = {gumbel.ProbabilityDensity(x)}");
32+
33+
// CDF, aka percentile
34+
double P = gumbel.LeftProbability(x);
35+
Console.WriteLine($"P({x}) = {P}");
36+
37+
// Right CDF
38+
double Q = gumbel.RightProbability(x);
39+
Console.WriteLine($"Q({x}) = {Q}");
40+
41+
Console.WriteLine($"P + Q = {P + Q}");
42+
43+
// Far tail
44+
double xt = 100.0;
45+
double qt = gumbel.RightProbability(xt);
46+
Console.WriteLine($"Q({xt}) = {qt}");
47+
48+
// Inverse CDF, aka quantile
49+
Console.WriteLine($"PI({P}) = {gumbel.InverseLeftProbability(P)}");
50+
Console.WriteLine($"QI({qt} = {gumbel.InverseRightProbability(qt)}");
51+
52+
53+
DiscreteDistribution binomial = new BinomialDistribution(0.4, 8);
54+
55+
Console.WriteLine($"support {binomial.Support}");
56+
57+
int k = 4;
58+
Console.WriteLine($"P({k}) = {binomial.ProbabilityMass(k)}");
59+
60+
double binomialMad = binomial.ExpectationValue(i => Math.Abs(i - binomial.Mean));
61+
Console.WriteLine($"mean absolute deviation = {binomialMad}");
62+
63+
Console.WriteLine($"P(k < {k}) = {binomial.LeftExclusiveProbability(k)}");
64+
Console.WriteLine($"P(k <= {k}) = {binomial.LeftInclusiveProbability(k)}");
65+
Console.WriteLine($"P(k > {k}) = {binomial.RightExclusiveProbability(k)}");
66+
67+
int k0 = binomial.InverseLeftProbability(0.5);
68+
Console.WriteLine($"min k0 to achieve P(k <= k0) > 0.5: {k0}");
69+
Console.WriteLine($"P(k < {k0}) = {binomial.LeftExclusiveProbability(k0)}");
70+
Console.WriteLine($"P(k <= {k0}) = {binomial.LeftInclusiveProbability(k0)}");
71+
72+
}
73+
74+
75+
[ExampleMethod]
76+
public static void DistributionMoments () {
77+
78+
//ContinuousDistribution d = new GumbelDistribution();
79+
DiscreteDistribution d = new PoissonDistribution(5);
80+
Console.WriteLine($"support = {d.Support}");
81+
82+
Console.WriteLine($"mean = {d.Mean}");
83+
Console.WriteLine($"mean as expectation = {d.ExpectationValue(x => x)}");
84+
85+
Console.WriteLine($"variance = {d.Variance}");
86+
Console.WriteLine($"variance as expectation = {d.ExpectationValue(x => MoreMath.Sqr(x - d.Mean))}");
87+
88+
Console.WriteLine($"standard deviation = {d.StandardDeviation}");
89+
Console.WriteLine($"skewness = {d.Skewness}");
90+
Console.WriteLine($"excess kuritosis = {d.ExcessKurtosis}");
91+
92+
for (int r = 0; r <= 4; r++) {
93+
Console.WriteLine($"M_{r} = {d.RawMoment(r)}");
94+
Console.WriteLine($"C_{r} = {d.CentralMoment(r)}");
95+
Console.WriteLine($"K_{r} = {d.Cumulant(r)}");
96+
}
97+
98+
}
99+
100+
}
101+
102+
}

Examples/Examples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Meta.Numerics" Version="4.0.5-alpha"/>
8+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2"/>
89
</ItemGroup>
910
</Project>

Examples/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ private static MethodInfo[] GetExampleMethods () {
2323

2424
static void Main(string[] args)
2525
{
26-
2726
MethodInfo[] methods = GetExampleMethods();
2827
Dictionary<string, MethodInfo> index = new Dictionary<string, MethodInfo>();
2928
foreach (MethodInfo method in methods) {

Examples/Statistics.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Linq;
23
using System.Collections.Generic;
34

45
using Meta.Numerics;
56
using Meta.Numerics.Matrices;
67
using Meta.Numerics.Statistics;
8+
using Meta.Numerics.Statistics.Distributions;
79

810
namespace Examples {
911

@@ -133,6 +135,34 @@ public static void ContingencyTable () {
133135

134136
}
135137

138+
[ExampleMethod]
139+
public static void FitToDistribution () {
140+
141+
Random rng = new Random(7);
142+
WeibullDistribution distribution = new WeibullDistribution(3.0, 1.5);
143+
List<double> sample = distribution.GetRandomValues(rng, 500).ToList();
144+
145+
WeibullFitResult weibull = sample.FitToWeibull();
146+
Console.WriteLine($"Best fit scale: {weibull.Scale}");
147+
Console.WriteLine($"Best fit shape: {weibull.Shape}");
148+
Console.WriteLine($"Probability of fit: {weibull.GoodnessOfFit.Probability}");
149+
150+
LognormalFitResult lognormal = sample.FitToLognormal();
151+
Console.WriteLine($"Best fit mu: {lognormal.Mu}");
152+
Console.WriteLine($"Best fit sigma: {lognormal.Sigma}");
153+
Console.WriteLine($"Probability of fit: {lognormal.GoodnessOfFit.Probability}");
154+
155+
var result = sample.MaximumLikelihoodFit(parameters => {
156+
return (new WeibullDistribution(parameters["Scale"], parameters["Shape"]));
157+
},
158+
new Dictionary<string, double>() { {"Scale", 1.0}, {"Shape", 1.0}}
159+
);
160+
foreach(Parameter parameter in result.Parameters) {
161+
Console.WriteLine($"{parameter.Name} = {parameter.Estimate}");
162+
}
163+
164+
}
165+
136166
}
137167

138168
}

0 commit comments

Comments
 (0)