Questions tagged [covariance]

Covariance, contravariance and invariance describe how the existing type inheritance hierarchy changes when subjected to some transformation (such as usage within generics). If the transformation keeps the ordering of the original hierarchy, it is "covariant". If it reverses it, it is "contravariant". If it breaks it, it is "invariant".

Filter by
Sorted by
Tagged with
0 votes
1 answer
44 views

Can I create derived collections of a derived type cleanly?

I have three classes: Animal Giraffe : Animal Tiger : Animal ... and want to implement three dictionary classes: public abstract class AnimalDictionary { internal Dictionary<int, Animal> ...
Quasar's user avatar
  • 174
0 votes
0 answers
14 views

Graphical lasso implementation with independent regularization per variable

I'm currently looking for a graphical lasso implementation allowing to specify different amount of regularization per variable. As precised in the original publication (End of page 6, Remark ; also ...
Rémi Trimbour's user avatar
-1 votes
0 answers
14 views

How to calculate Beta of multiple stocks using regression and covariance in python?

i'm student and i tried to calculate beta with regression which succeeded. now i want to check it in the same method. I mean i want to add another column to the Dataframe with beta2 or sth like that, ...
Mostafa Bouzari's user avatar
1 vote
1 answer
47 views

How can I construct numpy covariance matrix from database table (load using pandas)

I have a pandas table I read from database and it contains covariance matrix (the number is random so that it is not positive semi-def). I would like a fast way to construct a numpy matrix from the ...
shigeru ajisaka's user avatar
3 votes
1 answer
155 views

Calculating (partial) correlation from a (shrunken) covariance matrix (Help porting R code to Python)

There's a paper that I found interesting and would like to use some of the methods in Python. Erb et al. 2020 implements partial correlation on compositional data and Jin et al. 2022 implements it in ...
O.rka's user avatar
  • 29.9k
1 vote
1 answer
48 views

Cast class with covariance in generic

I have a problem with invariance and covariance in generic and c# I want cast handlerConcrete, here the code [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { ...
user3401335's user avatar
  • 2,345
0 votes
0 answers
11 views

Why doesn't the nugget for a covariance matrix for a Gaussian Process appear in the whole diagonal

In the covariance matrix of a GP with noise, one adds a nugget to the top-left covariance block, which is the covariance for the observed responses. But what about the bottom-right block- the one with ...
LifeisGood94's user avatar
0 votes
1 answer
27 views

PHP - Spoofing return-type covariance for code prediction

Consider: abstract class BaseModel { protected static string $dataObjectClass; public function toDataObject(): ?IDataObject { return static::$dataObjectClass::from($this); } } ...
Rikaelus's user avatar
  • 584
1 vote
0 answers
23 views

Java Covariant Override of Method with Contravariant Consumer as a Parameter [duplicate]

In the following code example the compiler does not allow me to override the method f. public class A {} public class B extends A {} public class X { public void f(Consumer<? super A> ...
Stefan Dollase's user avatar
0 votes
0 answers
15 views

create a covariance test between a current time section and past time (1-20 years back) section of the same stock in pine script

I would like to run a covariance test between any current time section of a stock/etf, example SPY 5-3-2023, and any past historical section of the same stock, example 9-25-17. I have run into ...
lvbx9's user avatar
  • 39
1 vote
1 answer
26 views

Why is a generic implementing an interface not covariant, but a generic of a base class is?

I noticed while trying to implement a generic that there was different behavior between a class with a generic that implements an interface vs a class with a generic that extends a Base class. With ...
user43024's user avatar
0 votes
0 answers
41 views

Nan's in covariance matrix

I've got a problem connected with covariance matrix used to implement Fractional Brownian Motion using Cholesky's decomposition. Code below creates covariance matrix. for i in range(n): for j ...
Maciej Ostapiuk's user avatar
0 votes
2 answers
47 views

How to design an override-safe factory method that allows subclasses to return covariant types?

I have a parent class that is designed to be const-safe, so I want to create "factory" methods that will return an altered version of itself in a newly constructed object. I want to be able ...
Lily-Heather Crawford's user avatar
0 votes
1 answer
82 views

How to apply subtyping and variance concept

I am new to Rust and the problem that I'm currently facing relates to subtyping and variance concept(just a guess, as per the help message shown by cargo while building). use std::sync::atomic::{...
Harry's user avatar
  • 2,209
0 votes
2 answers
56 views

Casting type twice necessary in a generic class method return call [duplicate]

I have a case where I have a generic class and I want a method that returns a newly created object of the same type and the instance of the class. I was running into an error that in the end I was ...
Adam Streck's user avatar
2 votes
1 answer
60 views

What are possible difficulties in implementing return type covariance for interfaces?

The question is highly related to this one. The support of return types covariance for overriden methods was added to C# only in version 9, and still interfaces are not supported. I'm not interested ...
Alex Che's user avatar
  • 6,699
1 vote
0 answers
36 views

R functions for running a two-way repeated measures ANOVA

Hoping to get some guidance for this analysis. I am currently following this guide for running a 2 way RMANOVA. This guide focuses on the car and afex package. I'm having issues with errors on both ...
Caiti Czajkowksi's user avatar
0 votes
0 answers
31 views

Simulating a spatio-temporal Gaussian process

I'd like to simulate a Gaussian process with mean zero and a non-separable Gneiting correlation function. The R-code for this function is: Gneiting<-function(h,u, al = 0.75, s2 = 0.968, a=0.5,c=1,...
EM823823's user avatar
  • 163
0 votes
2 answers
65 views

Invariance, Covariance and Contravariance - Is there a metaphor?

I want to start by saying that I know this topic has been discussed in depth on Stack Overflow. But I don't feel like the explanations given provide a strong story that helps to retain the examples ...
Nadar's user avatar
  • 1,561
0 votes
0 answers
26 views

Contravariance of method argument types versus taking objects of subtypes as arguments

Type variance is often mentioned in the context of generic types, but it is also applicable when talking about subtyping of classes. From what I have learnt about variance and subtyping, it seems like ...
Maurycyt's user avatar
  • 676
2 votes
1 answer
70 views

Error in implementing Kotlin interface with Java: incompatible return type

I try to implement a Kotlin interface in Java, so my interface looks like this: interface KInterface { val items: Collection<ItemInterface> } interface ItemInterface { // } Then, in my ...
i_ponomarenko's user avatar
0 votes
1 answer
45 views

Using covariant return type while overriding method from granparent interface results in complier error

public interface EntityId { ... EntityId cloneWithNewId(long id); } public interface Ticket extends EntityId { /// cloneWithNewId - is not mentioned in this interface } public record TicketImpl(....
Dmitro's user avatar
  • 33
0 votes
1 answer
117 views

Find Covariance and Correlation of Joint Probability Distribution in Python

If I'm given a joint distribution of 2 random variables say A and B, how would I find the covariance of A,B? (I have known to calculate E_X, E_Y, E_XY and apply formula, what I want to find library ...
NamNguyen's user avatar
1 vote
0 answers
72 views

From a type system perspective, In Scala 3, should variance affect extensional equality between types `F[T]` and `F[_ <: T]`?

This is both a question and a poll. Considering the following case: trait Mat[+T] implicitly[Mat[Product] =:= Mat[_ <: Product]] /* Compiler error: : Cannot prove that com.tribbloids.spike....
tribbloid's user avatar
  • 3,930
0 votes
1 answer
35 views

How to generate new object in a Generic that is restricted by Interface. Cannot implicitly convert type (Covariance and Contravariance)

I am building a code generator which will generate code that follows interfaces which inherit an underlying interface. I thought that I could use a generic class for this, but I think I have hit the ...
Tom22's user avatar
  • 1
0 votes
1 answer
49 views

Generic type compatibility

I know this goes back to variance in general, but I am not sure how to resolve the issue shown below. I want to have a list of generic sensors and add a specific sensor type to the list, but this ...
lf70's user avatar
  • 1
0 votes
1 answer
95 views

Combining variance covariance matrices of mixed effects models with R

I have 10 different data sets in R and for the coefficients I have combined the estimates using Rubin's rules. How would I combine the variance covariance matrix? Should I find the mean for each value ...
ikeagoddess's user avatar
1 vote
3 answers
132 views

Is it possible to make a virtual function that would return a proper unique_ptr of derived type?

I want to make function clone() actually overridable, as I need it to be polymorphic in my application. class Component { virtual Component *cloneImpl() const = 0; public: std::unique_ptr<...
aallfik11's user avatar
4 votes
0 answers
87 views

Why is covariant subtyping of mutable members allowed?

Invariance of mutable collections The rationale for why built-in mutable collection types in Python are invariant is explained well enough in both PEP 483 and PEP 484 and a nice illustrative example ...
Daniil Fajnberg's user avatar
0 votes
0 answers
21 views

Why the Position of Type parameters swaps in delegates when used as method parameters

Can someone please explain why the position of Type parameters used in Interface swaps while in Func delegates as method parameters for Interface method To be precise I am having issue understanding ...
Learn Programming's user avatar
0 votes
0 answers
24 views

Architecture for tensorflow probability MultivariateNormalFullCovariance

Trying to learn tfp and having a brain fart. I want to model the mean and covariance of a dataset given some explanatory inputs. I know my architecture is wrong, but I'm not sure how. See the code ...
Steven Kelly's user avatar
1 vote
1 answer
53 views

In Scala, is the transitive rule of covariance missing?

E.g. trait AA[+X[n], +Y] { def x: X[Y] // [Error]: covariant type Y occurs in invariant position in type => X[Y] of method x } This appears to be the wrong message, as X[Y] can be easily ...
tribbloid's user avatar
  • 3,930
0 votes
1 answer
29 views

Python assign covariance row wise calculation

I am trying to assign the covariance value to a column based on the dataframe I have. The df is ~400k records x 30+ columns. The two data series that act as inputs for COV() are all aligned as a ...
John's user avatar
  • 479
0 votes
1 answer
41 views

Type missmatch on Slick TableQuery

I am trying to have a generic type definition that can be satisfied by slicks TableQuery type: class OrderTable(tag: Tag) extends Table[Order](tag, "orders") ... val orders = ...
Georg Prohaska's user avatar
0 votes
2 answers
54 views

Fast implementations in Python to compute the mean of products

I have a list of float elements x=[0.1, 2, 0.5, ...] with length l=len(x). I am looking for fast/vectorized implementations to compute mean of the products between all two pairs from x: S=0.0 for x1 ...
Botond's user avatar
  • 2,640
2 votes
0 answers
46 views

Why is Type[T] defined to be covariant in T?

PEP 484 declares that typing.Type[T] is covariant, i.e. that for any types A and B, B being a subtype of A implies that Type[B] is a subtype of Type[A], which is obviously wrong: >>> from ...
Feuermurmel's user avatar
  • 9,499
0 votes
0 answers
27 views

How to do covariance test for our dataset?

carData=mtcars[,c("am","mpg","hp")] carData This was the example from RStudio that we used as an example to do covariance test, what should I plug in to "mtcars&...
Nadun's user avatar
  • 11
0 votes
1 answer
63 views

Finding the covariance between 1 column with range of other columns in a dataframe in r

I have a list of dataframes consisting of a date column, a monthly stock return column and a range of columns with different (monthly) risk parametres (180 to be exact). I have grouped my data by ...
OliverNystroem's user avatar
2 votes
3 answers
73 views

Why does Kotlin produce an Unchecked Cast warning when casting a covariant type parameter to invariant type parameter?

Suppose I have a class Message and a class Channel<T : Message>. Now, how come I can't cast Channel<out Message> to Channel<Message> without an Unchecked Cast warning? Shouldn't this ...
Archmede's user avatar
  • 1,602
1 vote
1 answer
432 views

Generics in python protocols - Covariance and Contravariance

after having read up on covariance and contravariance within python I still find myself struggling to understand why an Invariant has to be made a contravariant to be used within the context of a ...
Tiago Lima's user avatar
0 votes
2 answers
62 views

Calculated Empirical Variance is negative (python, pytorch)

I have a data encoded to one cluster each, and I am trying to calculate the empirical mean and variance of each cluster. I will use N as batchsize (in other words, number of data), D as a dimension of ...
esh3390's user avatar
  • 85
1 vote
2 answers
105 views

What is Java's equivalent to Kotlin's covariant and contra-variant modifiers?

Consider the following Kotlin interface: interface Box<out T : SomeType> { val item: T } Implementing this with Kotlin would look something like this: data class BoxImpl<out T : SomeType&...
Matthew Layton's user avatar
0 votes
0 answers
31 views

Why can't concrete implementations of generic interfaces be used polymorphically? [duplicate]

I have defined an interface like this: public interface IThingDoer<T> where T : SomeArbitraryType { Task<T> DoTheThing(); } I then define a concrete class which knows how to implement ...
Bassinator's user avatar
  • 1,682
0 votes
0 answers
128 views

Type Mismatch CapturedType Error with Kotlin Covariant Interfaces of Spring Data JDBC repositories

I am facing an issue with a covariant interface in Kotlin. I have two interfaces: NewMyEntity and NewMyEntityRepository. The latter is a PagingAndSortingRepository and a CrudRepository that uses the ...
Caspar Romot's user avatar
0 votes
0 answers
19 views

Degrees of Freedom for Linear Mixed Model Likelihood Ratio Test Statistic

I am studying LMM and am confused about the degrees of freedom associated with testing covariance parameters on the boundaries of the parameter space using restricted maximum likelihood, REML. Suppose ...
user19446714's user avatar
0 votes
0 answers
25 views

lme correlation structure for three-level model - follow up question

From the example in lme specifying correlation structure for three-level model. For each Family, I was hoping to see 2 (# of subject per family) diagonal blocks of 10 x 10 (# time pts x # time pts) as ...
Tony's user avatar
  • 1
-1 votes
1 answer
38 views

C# cast from child with more specific generic to parent with less specific generic

I am trying to cast from a child with a specific generic to a parent with a more general generic. Take the following code: public class AParent { } public class AChild : AParent { } public interface ...
Montana's user avatar
  • 482
1 vote
1 answer
53 views

How to deal with a covariance of base pointers, casting from derived to base class for using methods of containers

I have the following classes: class base{}; class derived1:public base{}; class derived2:public base{}; I also have an implemented container pool to which I have no editing access. In each of the 3 ....
havakok's user avatar
  • 1,185
0 votes
0 answers
21 views

Will Correlation coefficient be impacted by the magnitude of the variables at play?

I am having doubts about whether the correlation between two variables makes sense: We have two features we are trying to check for correlation: -monthly level of engagement of our customer base -...
Guglielmo Grevaso's user avatar

1
2 3 4 5
39