covariance

Diagonal element for covariance matrix not 1 pandas/numpy

依然范特西╮ 提交于 2020-05-26 06:34:27
问题 I have the following dataframe: A B 0 1 5 1 2 6 2 3 7 3 4 8 I wish to calculate the covariance a = df.iloc[:,0].values b = df.iloc[:,1].values Using numpy for cov as : numpy.cov(a,b) I get: array([[ 1.66666667, 1.66666667], [ 1.66666667, 1.66666667]]) Shouldn't the diagonal elements be 1? How do I get the diagonal elements to 1? 回答1: No they shouldn't. I think you might be confusing it with Correlation. Correlation and Covariance are different. What you see in the diagonals is simply the

Diagonal element for covariance matrix not 1 pandas/numpy

99封情书 提交于 2020-05-26 06:34:08
问题 I have the following dataframe: A B 0 1 5 1 2 6 2 3 7 3 4 8 I wish to calculate the covariance a = df.iloc[:,0].values b = df.iloc[:,1].values Using numpy for cov as : numpy.cov(a,b) I get: array([[ 1.66666667, 1.66666667], [ 1.66666667, 1.66666667]]) Shouldn't the diagonal elements be 1? How do I get the diagonal elements to 1? 回答1: No they shouldn't. I think you might be confusing it with Correlation. Correlation and Covariance are different. What you see in the diagonals is simply the

Diagonal element for covariance matrix not 1 pandas/numpy

走远了吗. 提交于 2020-05-26 06:33:47
问题 I have the following dataframe: A B 0 1 5 1 2 6 2 3 7 3 4 8 I wish to calculate the covariance a = df.iloc[:,0].values b = df.iloc[:,1].values Using numpy for cov as : numpy.cov(a,b) I get: array([[ 1.66666667, 1.66666667], [ 1.66666667, 1.66666667]]) Shouldn't the diagonal elements be 1? How do I get the diagonal elements to 1? 回答1: No they shouldn't. I think you might be confusing it with Correlation. Correlation and Covariance are different. What you see in the diagonals is simply the

How to pass extential type of tuple to invariant generic function?

青春壹個敷衍的年華 提交于 2020-05-17 06:13:07
问题 I have an extential type of tuple, and I want to pass it to a generic function (I use ClassTag in this example but it is a custom type class with invariant type parameter): type TC = (ClassTag[T], Option[T]) forSome {type T} def foo[T](ct: ClassTag[T], o: Option[T]) = {} val tc: TC = (classTag[String], Option[String](null)) foo(tc._1, tc._2) This gives me error: error: type mismatch; found : scala.reflect.ClassTag[T] required: scala.reflect.ClassTag[Any] Note: T <: Any, but trait ClassTag is

MyPy doesn't allow constrained TypeVar's to be covariant? Defining a generic dict with constrained but covariant key-val types

隐身守侯 提交于 2020-05-12 04:48:48
问题 I'm trying to define a custom generic dict, whose keys are of type T_key and values are of type T_val . I also want to put constraints on T_key and T_val , such that T_key can only be of type A or B or their subclass. How do I accomplish this? from typing import TypeVar, Generic class A: ... class B: ... class Asub(A): ... class Bsub(B): ... T_key = TypeVar('T_key', A, B, covariant=True) T_val = TypeVar('T_val', A, B, covariant=True) class MyDict(Generic[T_key, T_val]): ... w: MyDict[ A, B] x

MyPy doesn't allow constrained TypeVar's to be covariant? Defining a generic dict with constrained but covariant key-val types

China☆狼群 提交于 2020-05-12 04:47:18
问题 I'm trying to define a custom generic dict, whose keys are of type T_key and values are of type T_val . I also want to put constraints on T_key and T_val , such that T_key can only be of type A or B or their subclass. How do I accomplish this? from typing import TypeVar, Generic class A: ... class B: ... class Asub(A): ... class Bsub(B): ... T_key = TypeVar('T_key', A, B, covariant=True) T_val = TypeVar('T_val', A, B, covariant=True) class MyDict(Generic[T_key, T_val]): ... w: MyDict[ A, B] x

Is C# type system sound and decidable?

感情迁移 提交于 2020-03-17 03:53:32
问题 I know that Java's type system is unsound (it fails to type check constructs that are semantically legal) and undecidable (it fails to type check some construct). For instance, if you copy/paste the following snippet in a class and compile it, the compiler will crash with a StackOverflowException (how apt). This is undecidability. static class ListX<T> {} static class C<P> extends ListX<ListX<? super C<C<P>>>> {} ListX<? super C<Byte>> crash = new C<Byte>(); Java uses wildcards with type

Is C# type system sound and decidable?

梦想的初衷 提交于 2020-03-17 03:50:04
问题 I know that Java's type system is unsound (it fails to type check constructs that are semantically legal) and undecidable (it fails to type check some construct). For instance, if you copy/paste the following snippet in a class and compile it, the compiler will crash with a StackOverflowException (how apt). This is undecidability. static class ListX<T> {} static class C<P> extends ListX<ListX<? super C<C<P>>>> {} ListX<? super C<Byte>> crash = new C<Byte>(); Java uses wildcards with type

IList using covariance and contravariance in c#, is this possible?

依然范特西╮ 提交于 2020-02-03 05:17:45
问题 would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could use this to actually implement covariance and contravariance in the same interface. 回答1: No, you can't. In your example IList<T> is invariant. IList<T> would require to declare in

Understanding scala's _ vs Any/Nothing

こ雲淡風輕ζ 提交于 2020-01-24 02:07:59
问题 If a class has a convariant type parameter such as Iterable[+A], is there any difference between declaring def foo(bar: Iterable[_]) and def foo(bar: Iterable[Any]) ? If a class has a contravariant type parameter such as Growable[-A], is there any difference between declaring def foo(bar: Growable[_]) and def foo(bar: Growable[Nothing]) ? 回答1: It does make a little difference when generic parameter is bounded. For example, if you had class BoundedIterable[+A <: Something] class