covariance

Create rolling covariance matrix in pandas

心不动则不痛 提交于 2020-01-23 03:38:07
问题 I am trying to create a set of rolling covariance matrices on financial data (window size = 60). Returns is a 125x3 df. import pandas as pd roll_rets = returns.rolling(window=60) Omega = roll_rets.cov() Omega is a 375x3 data frame with what looks like a multi-index - i.e. there are 3 values for each timestamp. What I actually want this to return is a set of 66 3x3 covariance matrices (i.e. one for each period), but I can't work out how to iterate over returns correctly to do this. I think I'm

How is covariance cooler than polymorphism…and not redundant?

雨燕双飞 提交于 2020-01-21 01:44:05
问题 .NET 4 introduces covariance. I guess it is useful. After all, MS went through all the trouble of adding it to the C# language. But, why is Covariance more useful than good old polymorphism? I wrote this example to understand why I should implement Covariance, but I still don't get it. Please enlighten me. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sample { class Demo { public delegate void ContraAction<in T>(T a); public interface

Why is Task<T> not co-variant?

≡放荡痞女 提交于 2020-01-18 16:05:41
问题 class ResultBase {} class Result : ResultBase {} Task<ResultBase> GetResult() { return Task.FromResult(new Result()); } The compiler tells me that it cannot implicitly convert Task<Result> to Task<ResultBase> . Can someone explain why this is? I would have expected co-variance to enable me to write the code in this way. 回答1: According to someone who may be in the know... The justification is that the advantage of covariance is outweighed by the disadvantage of clutter (i.e. everyone would

Why is Task<T> not co-variant?

帅比萌擦擦* 提交于 2020-01-18 15:59:30
问题 class ResultBase {} class Result : ResultBase {} Task<ResultBase> GetResult() { return Task.FromResult(new Result()); } The compiler tells me that it cannot implicitly convert Task<Result> to Task<ResultBase> . Can someone explain why this is? I would have expected co-variance to enable me to write the code in this way. 回答1: According to someone who may be in the know... The justification is that the advantage of covariance is outweighed by the disadvantage of clutter (i.e. everyone would

Why the interface IOrderedEnumerable<T> isn't covariant in T?

大憨熊 提交于 2020-01-14 12:37:34
问题 I was Looking at the declaration of IOrderedEnumerable an I was suprised that it isn't covariant in it's TElement type parameter . public interface IOrderedEnumerable<TElement> : IEnumerable<TElement>, IEnumerable { IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending); } What's the reason for which it was not made covariant ? 回答1: It's an oversight and that was fixed in .NET Core. Here is (closed) issue about

Running (one pass) calculation of covariance

随声附和 提交于 2020-01-13 09:44:31
问题 I got a set of 3d vectors (x,y,z), and I want to calculate the covariance matrix without storing the vectors. I will do it in C#, but eventually I will implement it in C on a microcontroller, so I need the algorithm in itself, and not a library. Pseudocode would be great also. 回答1: The formula is simple if you have Matrix and Vector classes at hand: Vector mean; Matrix covariance; for (int i = 0; i < points.size(); ++i) { Vector diff = points[i] - mean; mean += diff / (i + 1); covariance +=

C#: Is a private inner interface possible?

无人久伴 提交于 2020-01-12 19:09:47
问题 I have a generic class X<T> ; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T> . However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private . I.e., inside the class itself, I can upcast to IX<T> and use it covariantly. E.g.: class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to

C#: Is a private inner interface possible?

房东的猫 提交于 2020-01-12 19:09:28
问题 I have a generic class X<T> ; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T> . However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private . I.e., inside the class itself, I can upcast to IX<T> and use it covariantly. E.g.: class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to

Variance rules in C#

你说的曾经没有我的故事 提交于 2020-01-12 14:25:28
问题 The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it is: 1) a pointer type, or a non-generic type. Pointers and non-generic types are not variant in C#, except for arrays and non-generic delegates. Generic classes, structs and enums are invariant. Am I right here? 2) An array type T[] where T is valid

Variance rules in C#

不羁岁月 提交于 2020-01-12 14:23:12
问题 The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it is: 1) a pointer type, or a non-generic type. Pointers and non-generic types are not variant in C#, except for arrays and non-generic delegates. Generic classes, structs and enums are invariant. Am I right here? 2) An array type T[] where T is valid