拉丁超立方抽样的python代码
时间: 2023-10-12 20:17:37 浏览: 120
下面是一个实现拉丁超立方抽样的Python代码:
```python
import numpy as np
def latin_hypercube(n, k):
"""
Generate a Latin hypercube sample of size n with k variables.
"""
# Create an empty array to store the sample
sample = np.zeros((n, k))
# Generate the marginal quantiles
quantiles = np.linspace(0, 1, n + 1)
# Generate the intervals for each variable
intervals = np.zeros((k, n + 1))
for i in range(k):
intervals[i, :] = np.random.permutation(quantiles)
# Generate the samples
for i in range(k):
for j in range(n):
sample[j, i] = np.random.uniform(intervals[i, j], intervals[i, j + 1])
return sample
```
其中,n是采样数量,k是变量数量。函数返回一个n行k列的采样矩阵。
阅读全文