我有这样的类层次结构
public abstract class CalendarEventBase{}
public class TrainingEvent : CalendarEventBase{}
public class AuditEvent : CalendarEventBase{}
Run Code Online (Sandbox Code Playgroud)
我想创建一个动作Action lamda,它具有CalendarEventBase类型的泛型类型参数,我可以将其分配给以下不同的方法:
public void EmailCancelation(TrainingEvent trainingEvent)
public void EmailCancelation(AuditEvent auditEvent)
Run Code Online (Sandbox Code Playgroud)
我创建了以下非法作业:
Action<CalendarEventBase> emailCancelation = _trainingService.EmailTrainingCancellation;
Run Code Online (Sandbox Code Playgroud)
编译器抱怨它期望一个带有void(CalendarEventBase)的方法作为签名.我对此感到惊讶,因为我认为它会接受更多衍生类型.
为了解决这个问题,我创建了以下委托,允许我完成我的任务:
public delegate void EmailCancelation<in T>(T calendarEvent) where T : CalendarEventBase;
Run Code Online (Sandbox Code Playgroud)
我的问题是,我是否可以完成任务而无需创建额外的代表?我以为我可以创建一个Action实例.
任何帮助或指示,非常感谢.
投资组合方差计算如下:
port_var = W'_p * S * W_p
Run Code Online (Sandbox Code Playgroud)
对于具有 N 资产的投资组合,其中
W'_p = transpose of vector of weights of stocks in portfolios
S = sample covariance matrix
W_p = vector of weights of stocks in portfolios
Run Code Online (Sandbox Code Playgroud)
我有以下 numpy 矩阵。
投资组合中股票权重的数组(向量)(有 10 只股票):
weights = np.array(
[[ 0.09],
[ 0.05],
[ 0.15],
[ 0.10],
[ 0.15],
[ 0.15],
[ 0.08],
[ 0.08],
[ 0.1 ],
[ 0.05]])
Run Code Online (Sandbox Code Playgroud)
股票收益的协方差矩阵:
covar = np.array([[ 0.00154474 0.00079555 0.00099691 0.00052596 0.0005363 0.00062005
0.00064031 0.00037494 0.00018826 0.00132809], …Run Code Online (Sandbox Code Playgroud) 在Scala 2.10.4中,这编译:
trait Foo[-U,T]{
type Contra = U
}
Run Code Online (Sandbox Code Playgroud)
但是在2.11.0中同样失败了:
逆变型U出现在类型为反对特征Foo的类型U的不变位置[-U,T] {类型Contra = U}
有可用的解决方法吗?试图将Scala库移植到2.11并且需要逆变类型以获得编译器拾取的大量隐式defs(即使U不变量似乎不是一个选项).
谢谢
我想创建一个可排序的observableCollection,所以我开始创建一个继承observable的类,用一些方法对它进行排序,然后我希望该类将索引保存到子节点中,所以我创建了一个接口,公开了一个索引属性,其中我可以写入,并且我将我的集合类的T表示为我的接口,然后我希望能够从avery项目访问parentCollection,这里问题已经开始,因为父集合的类型是通用的...我已经尝试了很多解决方案,我认为协方差或不变性是方法,但我不能让它工作......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class SortableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>, ISortableCollection<T> where T : ISortable<T>
{
public void Sort()
{
//We all know how to sort something
throw new NotImplementedException();
}
protected override void InsertItem(int index, T item)
{
item.Index = index;
item.ParentCollection = this;
base.InsertItem(index, item);
}
}
public interface ISortableCollection<T> : IList<T>
{
void Sort();
}
public interface ISortable<T>
{
Int32 Index { get; set; }
ISortableCollection<T> ParentCollection { …Run Code Online (Sandbox Code Playgroud) 我有以下混合效应模型的输出。我想谈谈模型解释了多少变化。随机效应下的方差是否对应于残差(注:这里的试验是随机效应)所解释的变异?即 58.6 % 或者有其他方法来推断这一点
\n\nREML criterion at convergence: 71.9\n\nScaled residuals: \n Min 1Q Median 3Q Max \n-1.82579 -0.59620 0.04897 0.62629 1.54639 \n\nRandom effects:\n Groups Name Variance Std.Dev.\n trial (Intercept) 0.06008 0.2451 \n Residual 0.58633 0.7974 \nNumber of obs: 60, groups: trial, 30\n\nFixed effects:\n Estimate Std. Error df t value Pr(>|t|) \n(Intercept) 1.5522 0.2684 12.6610 13.233 0.09888 \ndrugantho 0.8871 0.1753 14.0000 1.043 0.31601 \ninterventionadded 0.2513 0.2553 14.0000 -1.276 0.32436 ** \nsexmale 3.0026 0.6466 15.0000 4.066 0.00021 \n---\nSignif. codes: 0 \xe2\x80\x98***\xe2\x80\x99 …Run Code Online (Sandbox Code Playgroud) 有没有办法从具有函数 metaMDS 的 NMDS 对象确定解释的累积方差(度量拟合或 R^2m)?该对象返回压力、分数、分数的值,但我没有看到差异。该函数来自 vegan 包并执行非度量多维缩放。
metaMDS(comm, distance = "bray", k = 2, try = 20, trymax = 20,
engine = c("monoMDS", "isoMDS"), autotransform =TRUE,
noshare = (engine == "isoMDS"), wascores = TRUE, expand = TRUE,
trace = 1, plot = FALSE, previous.best, ...)
Run Code Online (Sandbox Code Playgroud)
我读到 R^2 是 1-总压力?
感谢任何建议。
我想扩展 Welford 的在线算法,以便能够使用多个数字(批量)进行更新,而不是一次只更新一个: https: //en.wikipedia.org/wiki/Algorithms_for_calculate_variance
我尝试从 wiki 页面更新算法,如下所示:
# my attempt.
def update1(existingAggregate, newValues):
(count, mean, M2) = existingAggregate
count += len(newValues)
delta = np.sum(np.subtract(newValues, [mean] * len(newValues)))
mean += delta / count
delta2 = np.sum(np.subtract(newValues, [mean] * len(newValues)))
M2 += delta * delta2
return (count, mean, M2)
# The original two functions from wikipedia.
def update(existingAggregate, newValue):
(count, mean, M2) = existingAggregate
count += 1
delta = newValue - mean
mean += delta / count
delta2 = newValue - …Run Code Online (Sandbox Code Playgroud) 我有一个包含以下列的数据框:Date、ID和Value。我需要执行均值、中位数和方差Value,我.agg这样使用:
df = dataset\
.groupby(['ID', pd.Grouper(key='Date', freq='60T')])['Value']\
.agg(['mean', 'median', 'var'])\
.reset_index()
Run Code Online (Sandbox Code Playgroud)
它成功计算平均值,但当需要计算中位数时,它只是重复平均值,并且不存储或创建 var 列。结果如下:
ID Date mean median var
0 13834 2017-02-09 12:00:00 1.474920 1.474920 NaN
1 13834 2017-02-09 16:00:00 4.424796 4.424796 NaN
2 13834 2017-02-09 20:00:00 2.241871 2.241871 NaN
3 13834 2017-02-10 00:00:00 2.654867 2.654867 NaN
4 13834 2017-02-10 04:00:00 2.654867 2.654867 NaN
5 13834 2017-02-10 08:00:00 0.511062 0.511062 NaN
Run Code Online (Sandbox Code Playgroud)
在最后一个数字的末尾应该有方差列,但我什么也没得到(或NaNs,如果在数据框中显示)。我该如何解决?
有没有办法反映一个接口来检测其泛型类型参数和返回类型的差异?换句话说,我可以使用反射来区分两个接口:
interface IVariant<out R, in A>
{
R DoSomething(A arg);
}
interface IInvariant<R, A>
{
R DoSomething(A arg);
}
Run Code Online (Sandbox Code Playgroud)
两者的IL看起来都一样.
我第一次遇到这些术语:输出安全,输入安全,输出不安全和输入不安全的C#语言规范的方差安全部分.我熟悉方差的概念(协方差和逆变),它基本上是指用一种类型代替另一种类型.
那么输出或输入安全的真正含义是什么?我们是在讨论泛型类型中的类型参数还是任何类型(引用或值)?
variance ×10
c# ×3
python ×3
.net ×2
r ×2
algorithm ×1
arrays ×1
covariance ×1
delegates ×1
generics ×1
implicits ×1
lambda ×1
mixed-models ×1
numpy ×1
pandas ×1
portfolio ×1
reflection ×1
scala ×1
statistics ×1