标签: simulation

用心理包模拟SEM数据

我用psych包装模拟SEM(结构方程模型)的数据.我使用了使用psych包来生成和测试结构模型的第17页上给出的代码.代码是

library(psych)
set.seed(42)
fx <- matrix(c(0.9, 0.8, 0.7, rep(0, 9), 0.7, 0.6, 0.5, rep(0, 9), 0.6, 0.5, 0.4), ncol = 3)
rownames(fx) <- paste("x", 1:9, sep="")
fy <- matrix(c(0.6, 0.5, 0.4), ncol=1)
rownames(fy) <- paste("y", 1:3, sep="")
Phi  <- matrix(c(1, 0.48, 0.32, 0.4, 0.48, 1, 0.32, 0.3, 0.32, 0.32, 1, 0.2, 0.4, 0.3, 0.2, 1), ncol = 4)
twelveV <- sim.structure(fx=fx, Phi=Phi, fy=fy, n=100, raw=TRUE)
round(twelveV$model, 2)
round(twelveV$model-twelveV$r, 2)
twelveV$observed
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用sem包来分析模拟数据.代码是

sem.mod <- structure.sem(twelveV$model) …
Run Code Online (Sandbox Code Playgroud)

simulation r

1
推荐指数
1
解决办法
627
查看次数

表示简单电路时的最佳数据结构 - C++

我正在做一个简单的电路系统来模拟C++中的欧姆定律.(V = IR)

为此,我使用Qt来制作GUI.用户可以连接不同的部件/组件,如电阻器,电池,电压表,电流表,灯泡,一旦点击启动按钮,电压表和电流表应显示读数.然后用户应该能够添加/移除电池.在做这个用户时应该意识到R是一个常数.

此外,如果使用灯泡,他们应该根据法律强度点亮.

我需要知道如何在代码中的数据结构中表示.

c++ simulation qt data-structures

1
推荐指数
1
解决办法
1297
查看次数

为什么在简单计数器中延迟1个时钟周期

下面是一个简单的3位计数器.

当复位(rst)为0时,计数器值为"000",否则每个时钟的上升沿递增1.

 LIBRARY ieee;
 USE ieee.std_logic_1164.all;
 use IEEE.std_logic_unsigned.all;
 use IEEE.numeric_std.all;
 ---------------------------------------------
 ENTITY counter IS
 PORT (clk : IN STD_LOGIC;
    rst : in std_logic;
       digit : out std_logic_vector (2 downto 0)
       );
 END counter;
 ---------------------------------------------
 ARCHITECTURE counter OF counter IS
 BEGIN

 count: PROCESS(clk,rst)
 VARIABLE temp : std_logic_vector(2 downto 0);
 BEGIN
 IF (clk'EVENT AND clk='1') THEN
     if (rst = '1') then
        temp := temp + "001";
     else
        temp := "000";
     END IF;
 END IF;
 digit <= temp;
 END PROCESS count;
 END counter; …
Run Code Online (Sandbox Code Playgroud)

simulation counter delay vhdl

1
推荐指数
1
解决办法
4689
查看次数

VHDL/ModelSim - 无法找到实体

我正在尝试模拟我的VHDL文件,但遇到以下错误:

# ** Error: (vcom-11) Could not find work.lab1.
# 
# ** Error: (vcom-1195) Cannot find expanded name "work.lab1".
# 
# ** Error: Unknown expanded name.
# ** Error: VHDL Compiler exiting
# ** Error: c:/altera/12.1/modelsim_ase/win32aloem/vcom failed.
# Error in macro ./DE2_TOP_run_msim_rtl_vhdl.do line 8
# c:/altera/12.1/modelsim_ase/win32aloem/vcom failed.
#     while executing
# "vcom -93 -work work"
Run Code Online (Sandbox Code Playgroud)

在尝试模拟之前,我通过Quartus II和ModelSim编译器成功编译了代码.我的代码中有一个lab1实体和体系结构(我甚至可以在Quartus Project Navigator的Design Units选项卡中看到它),所以我真的不明白这个错误.任何人都知道是什么导致了这个?

simulation fpga vhdl modelsim

1
推荐指数
1
解决办法
2万
查看次数

为R中的Logistic分布的Mle实现Newton方法

考虑下面的物流密度:

Latex:$$ f\left(x;\theta\right)\ frac {\ exp\left\{ - \left(x_i-\theta\right)\ right \}} {\ left(1+\exp\left)\{ - \left(x_i-\theta\right)\ right \}\right)^ 2} $$

相应的对数似然由下式给出:

Latex:$$ l\left(\ theta\right)= n\theta -n\bar {x} -2\sum_ {i = 1} ^ {n} log\left(1+\exp\left\{ -\left(x_i - \theta\right)\ right \}\right)$$

不幸的是,$\theta $的均值,它的均值,不能以封闭形式获得,因此我必须编写一个数值优化算法.我认为使用Newton的方法寻找$ l\prime\left(\ theta\right)= 0 $的点是个好主意

现在,如果我们要使用牛顿方法,我们将需要对数似然的一阶和二阶导数,它们由下式给出:

Latex:$$ l\prime\left(\ theta\right)= n-2\sum_ {i = 1} ^ n\frac {\ exp\left\{ - \left(x_i-\theta\right)\ right \}} {\ left(1+\exp\left\{ - \left(x_i-\theta\right)\ right \}\right)} $$

Latex:$$ l\prime\prime\left(\ theta\right)= -2\sum_ {i = 1} ^ n\frac {\ exp\left\{ - \left(x_i-\theta\right)\ …

simulation r

1
推荐指数
1
解决办法
1137
查看次数

如何递归模拟随机游走?没有循环(Python)

Python问题

我有一个随机步骤的功能:

def random_step(): 
    """ chooses a random step (-1 or 1) and returns it.
        inputs: none! However, make sure to use parens when calling it.
            For example: ramdom_step()
    """
    return random.choice([-1, 1])
Run Code Online (Sandbox Code Playgroud)

我需要在我写的这个函数中调用它:

rw_outcome( start, numsteps ),需要两个输入:

  • start,一个表示梦游者起始位置的整数
  • numsteps,一个正int,表示从起始位置获取的随机步骤数

它应该模拟随机游走,其中包含numsteps随机步骤,其大小是使用调用来确定的random_step(),但我会继续返回相同的起始位置.

它应该与print返回的一个例子('start is',start):

>>> rw_outcome(40, 4)
start is 40
start is 41
start is 42
start is 41
start is 42
42
Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的:

def rw_outcome(start, numsteps):
    print('start is', start)
    if start + (numsteps*random_step()) …
Run Code Online (Sandbox Code Playgroud)

python simulation recursion random-walk

1
推荐指数
1
解决办法
901
查看次数

简单的Yahtzee模拟没有给出正确的结果?

我正在通过麻省理工学院开放式课程介绍计算机编程课程,我不确定我是否正确地解决了简单的模拟问题.

  1. Yahtzee的概率是多少啊!在第一卷?也就是说,滚动五个6面骰子的概率是多少,并且它们都显示相同的数字?
  2. 编写蒙特卡罗模拟来解决上述问题(Yahtzee问题),并将您的代码提交为

所以滚动Yahtzee的概率是1/1296或约.077%

这是我运行模拟的代码:

import random

def runYahtzee(numTrials):
    """Runs the classes version of the yahtzee simulation"""

    success = 0
    for i in range(numTrials):

        dices = []
        for i in range(6):
            dices.append(random.randrange(1,7))
        #print dices

        state = True
        for dice in dices:
            if dice != dices[0]:
                state = False
        if state == True:
            print "You got a Yahtzee"
            print dices
            success += 1

    print "numTrials is: " + str(numTrials)
    print "Success is: " + str(success)
    rate = float(success)/numTrials
    return rate

runYahtzee(10000000) …
Run Code Online (Sandbox Code Playgroud)

python simulation montecarlo python-2.7

1
推荐指数
1
解决办法
346
查看次数

R - 如何模拟随机变量?

我很乐意定期发行,比如模拟一个公平的模具.

但是,如果我想模拟以下内容怎么办:

当n为奇数时,X_n = 2(概率为0.6)或-1(概率为0.4).

当n为偶数时,X_n = -2(概率为0.6)或1(概率为0.4).

有什么建议?

simulation r

1
推荐指数
1
解决办法
224
查看次数

如何获得特定范围内的汽车数量

我正在尝试模拟一种VANET场景,在这种场景中,如果道路被阻塞,那么在特定时间之后,汽车会广播一条消息,其中包括被阻塞的roadId和它周围100米处的车辆数量。

在TraCIDemo11p应用程序中,当汽车停下来超过10秒钟时,它将节点颜色更改为红色(以表示事故),并向包含阻塞道路ID的其他汽车发送消息,所有这些操作均在handlePositionUpdate方法中完成:

findHost()->getDisplayString().updateWith("r=16,red");
sentMessage = true;

WaveShortMessage* wsm = new WaveShortMessage();
populateWSM(wsm);
wsm->setWsmData(mobility->getRoadId().c_str());

//host is standing still due to crash
if (dataOnSch) {
    startService(Channels::SCH2, 42, "Traffic Information Service");
    //started service and server advertising, schedule message to self to send later
    scheduleAt(computeAsynchronousSendingTime(1,type_SCH),wsm);
}
else {
    //send right away on CCH, because channel switching is disabled
    sendDown(wsm);
}
Run Code Online (Sandbox Code Playgroud)

可以通过更新.ini文件中的maxInterfDist值来设置100m的限制

*.connectionManager.maxInterfDist = 100m
Run Code Online (Sandbox Code Playgroud)

现在我的问题是如何获取车辆数量和100m的面积,我有一个想法,这将使用TraCI来完成,最有可能使用LaneAreaDetector的getJamLengthVehicle来完成,但是我不知道如何做到这一点。他们任何等效的方法还是我看错了方向?

c++ simulation omnet++ veins sumo

1
推荐指数
1
解决办法
385
查看次数

将Python MeshGrid拆分为单元格

问题陈述

需要将N维MeshGrid拆分为“多维数据集”:

例)二维情况:

(-1,1)|(0,1)|(1,1)

(-1,0)|(0,0)|(1,0)

(-1,-1)|(0,-1)|(1,-1)

将有4个单元格,每个单元格具有2 ^ D点:

我希望能够处理网格,将每个单元的坐标点放入容器中以进行进一步处理。

Cells =   [{(-1,1) (0,1)(-1,0),(0,0)},

          {(0,1),(1,1),(0,0),(1,0)},

          {(-1,0),(0,0)(-1,-1),(0,-1)}

          {(0,0),(1,0)(0,-1),(1,-1)}]
Run Code Online (Sandbox Code Playgroud)

我使用以下代码为任意尺寸d生成网格:

grid = [np.linspace(-1.0 , 1.0, num = K+1) for i in range(d)]
res_to_unpack = np.meshgrid(*grid,indexing = 'ij')
Run Code Online (Sandbox Code Playgroud)

哪个有输出:

[array([[-1., -1., -1.],
   [ 0.,  0.,  0.],
   [ 1.,  1.,  1.]]), array([[-1.,  0.,  1.],
   [-1.,  0.,  1.],
   [-1.,  0.,  1.]])]
Run Code Online (Sandbox Code Playgroud)

因此,我希望能够为给定的D维网格生成上述单元容器。在给定的K上除以2的幂。

我需要这个容器,因此对于每个像元,我需要引用所有关联的2 ^ D点并计算距原点的距离。

编辑说明

K应该将网格划分为K ** D个像元,并具有(K + 1)** D个点。每个像元应具有2 ** D个点。每个“单元”的体积为(2 ​​/ K)^ D。

所以对于K = 4,D = 2

Cells = [ …
Run Code Online (Sandbox Code Playgroud)

python simulation geometry numpy computational-geometry

1
推荐指数
1
解决办法
322
查看次数