attitudedyn 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- attitudedyn-0.1.0/CMakeLists.txt +61 -0
- attitudedyn-0.1.0/LICENSE +21 -0
- attitudedyn-0.1.0/PKG-INFO +102 -0
- attitudedyn-0.1.0/README.md +64 -0
- attitudedyn-0.1.0/core/Makefile +15 -0
- attitudedyn-0.1.0/core/include/attitudedyn/api.hpp +231 -0
- attitudedyn-0.1.0/core/include/attitudedyn/normal.hpp +215 -0
- attitudedyn-0.1.0/core/src/draws.cpp +285 -0
- attitudedyn-0.1.0/core/src/draws.hpp +26 -0
- attitudedyn-0.1.0/core/src/internal.hpp +67 -0
- attitudedyn-0.1.0/core/src/layout.cpp +393 -0
- attitudedyn-0.1.0/core/src/likelihood.cpp +728 -0
- attitudedyn-0.1.0/core/src/postestimation.cpp +440 -0
- attitudedyn-0.1.0/core/src/sobol_polynomials.inc +1034 -0
- attitudedyn-0.1.0/docs/figures/gss_impulse_response.png +0 -0
- attitudedyn-0.1.0/docs/user_guide.md +569 -0
- attitudedyn-0.1.0/pyproject.toml +73 -0
- attitudedyn-0.1.0/src/attitudedyn/__init__.py +23 -0
- attitudedyn-0.1.0/src/attitudedyn/bfgs.py +229 -0
- attitudedyn-0.1.0/src/attitudedyn/coefficients.py +139 -0
- attitudedyn-0.1.0/src/attitudedyn/fit.py +509 -0
- attitudedyn-0.1.0/src/attitudedyn/gss.py +447 -0
- attitudedyn-0.1.0/src/attitudedyn/prepare.py +433 -0
- attitudedyn-0.1.0/src/attitudedyn/py.typed +0 -0
- attitudedyn-0.1.0/src/attitudedyn/result.py +672 -0
- attitudedyn-0.1.0/src/attitudedyn/simulate.py +101 -0
- attitudedyn-0.1.0/src/bindings.cpp +411 -0
- attitudedyn-0.1.0/tests/test_api.py +198 -0
- attitudedyn-0.1.0/tests/test_gss.py +124 -0
- attitudedyn-0.1.0/tests/test_optimizer.py +144 -0
- attitudedyn-0.1.0/tests/test_recovery.py +96 -0
- attitudedyn-0.1.0/tools/run_wheel_tests.py +31 -0
- attitudedyn-0.1.0/tools/stage_wheel_project.py +55 -0
- attitudedyn-0.1.0/validation/replicate_gss_exhibits.py +351 -0
- attitudedyn-0.1.0/validation/run_gss.py +247 -0
- attitudedyn-0.1.0/validation/simulation_recovery.py +258 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.15...3.28)
|
|
2
|
+
project(attitudedyn_python LANGUAGES CXX)
|
|
3
|
+
|
|
4
|
+
# The extension is a binding. The likelihood, gradient, draws, and
|
|
5
|
+
# post-estimation all come from the shared C++17 library in ../src/core.
|
|
6
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
7
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
8
|
+
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
9
|
+
|
|
10
|
+
if(NOT DEFINED SKBUILD_PLATLIB_DIR)
|
|
11
|
+
message(FATAL_ERROR "This CMake project is meant to be configured by scikit-build-core (pip install ./python).")
|
|
12
|
+
endif()
|
|
13
|
+
|
|
14
|
+
# A wheel sdist carries a copy of src/core at ./core. A checkout install
|
|
15
|
+
# (`pip install ./python`) uses the core at ../src/core.
|
|
16
|
+
set(ATTDYN_CORE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/core")
|
|
17
|
+
if(NOT EXISTS "${ATTDYN_CORE_DIR}/include/attitudedyn/api.hpp")
|
|
18
|
+
set(ATTDYN_CORE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../src/core")
|
|
19
|
+
endif()
|
|
20
|
+
if(NOT EXISTS "${ATTDYN_CORE_DIR}/include/attitudedyn/api.hpp")
|
|
21
|
+
message(FATAL_ERROR
|
|
22
|
+
"The attitudedyn C++ core was not found. "
|
|
23
|
+
"Install from a checkout of the repository: pip install ./python")
|
|
24
|
+
endif()
|
|
25
|
+
|
|
26
|
+
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
|
|
27
|
+
find_package(pybind11 CONFIG REQUIRED)
|
|
28
|
+
|
|
29
|
+
execute_process(
|
|
30
|
+
COMMAND "${Python_EXECUTABLE}" -c "import numpy; print(numpy.get_include())"
|
|
31
|
+
OUTPUT_VARIABLE ATTDYN_NUMPY_INCLUDE
|
|
32
|
+
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
33
|
+
COMMAND_ERROR_IS_FATAL ANY
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
pybind11_add_module(_core MODULE
|
|
37
|
+
src/bindings.cpp
|
|
38
|
+
"${ATTDYN_CORE_DIR}/src/layout.cpp"
|
|
39
|
+
"${ATTDYN_CORE_DIR}/src/draws.cpp"
|
|
40
|
+
"${ATTDYN_CORE_DIR}/src/likelihood.cpp"
|
|
41
|
+
"${ATTDYN_CORE_DIR}/src/postestimation.cpp"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
target_include_directories(_core PRIVATE
|
|
45
|
+
"${ATTDYN_CORE_DIR}/include"
|
|
46
|
+
"${ATTDYN_CORE_DIR}/src"
|
|
47
|
+
"${ATTDYN_NUMPY_INCLUDE}"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
target_compile_definitions(_core PRIVATE NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
|
|
51
|
+
|
|
52
|
+
if(MSVC)
|
|
53
|
+
target_compile_options(_core PRIVATE /W3 /EHsc /bigobj)
|
|
54
|
+
else()
|
|
55
|
+
target_compile_options(_core PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
|
56
|
+
endif()
|
|
57
|
+
|
|
58
|
+
find_package(Threads REQUIRED)
|
|
59
|
+
target_link_libraries(_core PRIVATE Threads::Threads)
|
|
60
|
+
|
|
61
|
+
install(TARGETS _core DESTINATION "${SKBUILD_PLATLIB_DIR}/attitudedyn")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gabriel Mihalache
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: attitudedyn
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The Nepaul-Stern Model of Attitude Stability and Constraint for Likert-scale panel attitudes.
|
|
5
|
+
Keywords: statistics,panel,likert,attitudes,simulated-likelihood
|
|
6
|
+
Author-Email: Gabriel Mihalache <mihalache.2@osu.edu>
|
|
7
|
+
Maintainer-Email: Gabriel Mihalache <mihalache.2@osu.edu>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Operating System :: MacOS
|
|
13
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Programming Language :: C++
|
|
16
|
+
Classifier: Programming Language :: Python
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Project-URL: Homepage, https://nepaulstern.github.io/
|
|
27
|
+
Project-URL: Documentation, https://nepaulstern.github.io/
|
|
28
|
+
Project-URL: Repository, https://github.com/gabrielgggg/attitudedyn
|
|
29
|
+
Project-URL: Issues, https://github.com/gabrielgggg/attitudedyn/issues
|
|
30
|
+
Project-URL: Changelog, https://github.com/gabrielgggg/attitudedyn/blob/master/NEWS.md
|
|
31
|
+
Requires-Python: >=3.10
|
|
32
|
+
Requires-Dist: numpy>=1.23
|
|
33
|
+
Requires-Dist: scipy>=1.9
|
|
34
|
+
Requires-Dist: pandas>=1.5
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# attitudedyn for Python
|
|
40
|
+
|
|
41
|
+
Python interface for The Nepaul-Stern Model of Attitude Stability and Constraint. The model is for several Likert-scale attitudes measured on the same people across survey waves. A person's latent attitude carries forward from one wave to the next, and the recorded answer is the Likert category that latent attitude falls into. Estimation uses a simulated likelihood.
|
|
42
|
+
|
|
43
|
+
The same model is available from R. The two give the same estimates. The project site is <https://nepaulstern.github.io/>.
|
|
44
|
+
|
|
45
|
+
## Getting started
|
|
46
|
+
|
|
47
|
+
[`docs/user_guide.md`](docs/user_guide.md) is the walkthrough. It reads the General Social Survey panel, fits the application, and reads the summary, the persistence matrix, the impulse responses, the variance decomposition, the predicted Likert categories, and a new panel drawn from the fit. The R guide is `vignettes/user-guide.Rmd`. A PDF of that guide is `docs/attitudedyn_user_guide.pdf` in the repository root.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
Installing from a checkout needs a C++17 compiler and CMake. The installer compiles the shared numerical code. You do not write any C++. There is no R dependency.
|
|
52
|
+
|
|
53
|
+
From a checkout of the repository:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install ./python
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Person contributions are summed after the cores finish, in person order. The `threads` argument changes the speed and not the estimates.
|
|
60
|
+
|
|
61
|
+
## A first example
|
|
62
|
+
|
|
63
|
+
These lines simulate a small panel and fit The Nepaul-Stern Model for a few steps. `y1` and `y2` are Likert items. `x` is a covariate. `id` is the person, `wave` is the interview, and `time` is the date of the wave.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import attitudedyn
|
|
67
|
+
|
|
68
|
+
panel = attitudedyn.simulate_attitudes(n_persons=50, n_waves=3, n_factors=1, seed=1)
|
|
69
|
+
result = attitudedyn.fit(
|
|
70
|
+
panel,
|
|
71
|
+
formula="cbind(y1, y2) ~ x",
|
|
72
|
+
id_col="id",
|
|
73
|
+
wave_col="wave",
|
|
74
|
+
time_col="time",
|
|
75
|
+
n_factors=1,
|
|
76
|
+
draws=8,
|
|
77
|
+
maxit=10,
|
|
78
|
+
threads=1,
|
|
79
|
+
vcov="opg",
|
|
80
|
+
)
|
|
81
|
+
print(result.summary())
|
|
82
|
+
result.to_dataframe().head()
|
|
83
|
+
result.persistence_matrix()
|
|
84
|
+
result.variance_decomposition()
|
|
85
|
+
result.impulse_response(horizon=4).to_dataframe()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`summary()` is the text table. `to_dataframe()` is one row per free coefficient, with the standard error, z statistic, and p-value. `persistence_matrix()` is on (-1, 1): the row receives the carry-over from the earlier attitude in the column. `variance_decomposition()` reports four pieces of each latent attitude. `covariate` is the part from the covariates and the wave shifts together, `wave_effect` is the wave shift on its own, `factor` is the person-specific factor, and `shock` is the part from the random shocks carried forward by persistence. `impulse_response()` traces a one-time shock. Horizon 0 is the moment of the shock.
|
|
89
|
+
|
|
90
|
+
The General Social Survey walkthrough, with the output of that call, is [`docs/user_guide.md`](docs/user_guide.md). The survey file comes from the replication deposit on Harvard Dataverse (doi:10.7910/DVN/DUCHYS), dedicated to the public domain under CC0 1.0. From a checkout, `python3 validation/fortran/fetch_vendor.py` downloads it into `validation/fortran/vendor/data/gss2.prn`. `maxit=0` evaluates `start` and does not search.
|
|
91
|
+
|
|
92
|
+
You can pass column lists instead of a formula. The formula helper accepts `cbind(...)` and bare column names, including `` 0 + `(Intercept)` `` when the data already contain a column of ones.
|
|
93
|
+
|
|
94
|
+
`result.params`, `result.bse`, and `result.pvalues` are named Series. `result.vcov()` defaults to `opg_centered`, the standard errors stored with the replication estimates. That calculation uses the variation in each person's contribution to the slope of the likelihood, after subtracting the average. `opg` skips the subtraction. `hessian` uses the curvature of the likelihood. `sandwich` combines the curvature with the person-level variation. `summary(vcov_type=...)` selects the same calculation.
|
|
95
|
+
|
|
96
|
+
The default search is BFGS, the same steps `stats::optim(method = "BFGS")` uses in the R package. L-BFGS-B uses SciPy. Both use the slope of the likelihood. A trial step that is not finite, or that would make a threshold increment overflow, is handed back to the search as a large finite penalty with a zero slope, so the search rejects it.
|
|
97
|
+
|
|
98
|
+
The GSS check is `python/validation/run_gss.py`. Notes on the source layout are in `notes/PYTHON_GUIDE.md`.
|
|
99
|
+
|
|
100
|
+
## Acknowledgments
|
|
101
|
+
|
|
102
|
+
The C++/R/Python port, the documentation, and the deployment infrastructure were developed with Grok Bot and Cursor Cloud Agent.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# attitudedyn for Python
|
|
2
|
+
|
|
3
|
+
Python interface for The Nepaul-Stern Model of Attitude Stability and Constraint. The model is for several Likert-scale attitudes measured on the same people across survey waves. A person's latent attitude carries forward from one wave to the next, and the recorded answer is the Likert category that latent attitude falls into. Estimation uses a simulated likelihood.
|
|
4
|
+
|
|
5
|
+
The same model is available from R. The two give the same estimates. The project site is <https://nepaulstern.github.io/>.
|
|
6
|
+
|
|
7
|
+
## Getting started
|
|
8
|
+
|
|
9
|
+
[`docs/user_guide.md`](docs/user_guide.md) is the walkthrough. It reads the General Social Survey panel, fits the application, and reads the summary, the persistence matrix, the impulse responses, the variance decomposition, the predicted Likert categories, and a new panel drawn from the fit. The R guide is `vignettes/user-guide.Rmd`. A PDF of that guide is `docs/attitudedyn_user_guide.pdf` in the repository root.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
Installing from a checkout needs a C++17 compiler and CMake. The installer compiles the shared numerical code. You do not write any C++. There is no R dependency.
|
|
14
|
+
|
|
15
|
+
From a checkout of the repository:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install ./python
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Person contributions are summed after the cores finish, in person order. The `threads` argument changes the speed and not the estimates.
|
|
22
|
+
|
|
23
|
+
## A first example
|
|
24
|
+
|
|
25
|
+
These lines simulate a small panel and fit The Nepaul-Stern Model for a few steps. `y1` and `y2` are Likert items. `x` is a covariate. `id` is the person, `wave` is the interview, and `time` is the date of the wave.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import attitudedyn
|
|
29
|
+
|
|
30
|
+
panel = attitudedyn.simulate_attitudes(n_persons=50, n_waves=3, n_factors=1, seed=1)
|
|
31
|
+
result = attitudedyn.fit(
|
|
32
|
+
panel,
|
|
33
|
+
formula="cbind(y1, y2) ~ x",
|
|
34
|
+
id_col="id",
|
|
35
|
+
wave_col="wave",
|
|
36
|
+
time_col="time",
|
|
37
|
+
n_factors=1,
|
|
38
|
+
draws=8,
|
|
39
|
+
maxit=10,
|
|
40
|
+
threads=1,
|
|
41
|
+
vcov="opg",
|
|
42
|
+
)
|
|
43
|
+
print(result.summary())
|
|
44
|
+
result.to_dataframe().head()
|
|
45
|
+
result.persistence_matrix()
|
|
46
|
+
result.variance_decomposition()
|
|
47
|
+
result.impulse_response(horizon=4).to_dataframe()
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`summary()` is the text table. `to_dataframe()` is one row per free coefficient, with the standard error, z statistic, and p-value. `persistence_matrix()` is on (-1, 1): the row receives the carry-over from the earlier attitude in the column. `variance_decomposition()` reports four pieces of each latent attitude. `covariate` is the part from the covariates and the wave shifts together, `wave_effect` is the wave shift on its own, `factor` is the person-specific factor, and `shock` is the part from the random shocks carried forward by persistence. `impulse_response()` traces a one-time shock. Horizon 0 is the moment of the shock.
|
|
51
|
+
|
|
52
|
+
The General Social Survey walkthrough, with the output of that call, is [`docs/user_guide.md`](docs/user_guide.md). The survey file comes from the replication deposit on Harvard Dataverse (doi:10.7910/DVN/DUCHYS), dedicated to the public domain under CC0 1.0. From a checkout, `python3 validation/fortran/fetch_vendor.py` downloads it into `validation/fortran/vendor/data/gss2.prn`. `maxit=0` evaluates `start` and does not search.
|
|
53
|
+
|
|
54
|
+
You can pass column lists instead of a formula. The formula helper accepts `cbind(...)` and bare column names, including `` 0 + `(Intercept)` `` when the data already contain a column of ones.
|
|
55
|
+
|
|
56
|
+
`result.params`, `result.bse`, and `result.pvalues` are named Series. `result.vcov()` defaults to `opg_centered`, the standard errors stored with the replication estimates. That calculation uses the variation in each person's contribution to the slope of the likelihood, after subtracting the average. `opg` skips the subtraction. `hessian` uses the curvature of the likelihood. `sandwich` combines the curvature with the person-level variation. `summary(vcov_type=...)` selects the same calculation.
|
|
57
|
+
|
|
58
|
+
The default search is BFGS, the same steps `stats::optim(method = "BFGS")` uses in the R package. L-BFGS-B uses SciPy. Both use the slope of the likelihood. A trial step that is not finite, or that would make a threshold increment overflow, is handed back to the search as a large finite penalty with a zero slope, so the search rejects it.
|
|
59
|
+
|
|
60
|
+
The GSS check is `python/validation/run_gss.py`. Notes on the source layout are in `notes/PYTHON_GUIDE.md`.
|
|
61
|
+
|
|
62
|
+
## Acknowledgments
|
|
63
|
+
|
|
64
|
+
The C++/R/Python port, the documentation, and the deployment infrastructure were developed with Grok Bot and Cursor Cloud Agent.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
CXX ?= g++
|
|
2
|
+
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra -Iinclude -Isrc
|
|
3
|
+
|
|
4
|
+
SRC = src/layout.cpp src/draws.cpp src/likelihood.cpp src/postestimation.cpp
|
|
5
|
+
|
|
6
|
+
.PHONY: test clean
|
|
7
|
+
|
|
8
|
+
test: tests/test_core
|
|
9
|
+
./tests/test_core
|
|
10
|
+
|
|
11
|
+
tests/test_core: tests/test_core.cpp $(SRC) include/attitudedyn/api.hpp include/attitudedyn/normal.hpp
|
|
12
|
+
$(CXX) $(CXXFLAGS) -o $@ tests/test_core.cpp $(SRC)
|
|
13
|
+
|
|
14
|
+
clean:
|
|
15
|
+
rm -f tests/test_core
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#ifndef ATTITUDYN_API_HPP
|
|
2
|
+
#define ATTITUDYN_API_HPP
|
|
3
|
+
|
|
4
|
+
// Public C++ API of the attitude-dynamics core.
|
|
5
|
+
//
|
|
6
|
+
// This header includes only the standard library. The R package, and any
|
|
7
|
+
// later Python or Stata wrapper, should talk to the library through the
|
|
8
|
+
// types and functions declared here. Nothing in this interface mentions R.
|
|
9
|
+
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <string>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace attitudedyn {
|
|
15
|
+
|
|
16
|
+
// kStable: the default. Double-precision log(Phi(b)-Phi(a)), with analytic gradients.
|
|
17
|
+
// kShared: reproduces the original program's normal-probability routine for comparisons.
|
|
18
|
+
enum class Numerics {
|
|
19
|
+
kStable = 0,
|
|
20
|
+
kShared = 1
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Quasi-random streams are Owen-scrambled and keyed by the person index,
|
|
24
|
+
// so two people do not share one sequence and the result does not depend
|
|
25
|
+
// on the thread count.
|
|
26
|
+
enum class DrawKind {
|
|
27
|
+
kPhilox = 0,
|
|
28
|
+
kHalton = 1,
|
|
29
|
+
kSobol = 2
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
struct DrawOptions {
|
|
33
|
+
DrawKind kind = DrawKind::kPhilox;
|
|
34
|
+
int n_draws = 10;
|
|
35
|
+
bool antithetic = true;
|
|
36
|
+
std::uint64_t seed = 1;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
struct ModelSpec {
|
|
40
|
+
int n_waves = 0;
|
|
41
|
+
int n_attitudes = 0;
|
|
42
|
+
int n_covariates = 0;
|
|
43
|
+
int n_factors = 0;
|
|
44
|
+
std::vector<int> n_categories; // length n_attitudes, each >= 2
|
|
45
|
+
std::vector<double> wave_time; // length n_waves, strictly increasing
|
|
46
|
+
bool include_wave_effects = true;
|
|
47
|
+
bool free_contemporaneous = false;
|
|
48
|
+
bool fix_last_loading = true; // lambda[J, M] = 0 when M >= 2
|
|
49
|
+
bool fix_intercepts = true; // collinear with a full set of wave effects
|
|
50
|
+
std::vector<int> intercept_columns;
|
|
51
|
+
// 1 if attitude a has no observed responses in wave t (index a * n_waves + t).
|
|
52
|
+
std::vector<char> empty_wave_effect;
|
|
53
|
+
std::vector<std::string> attitude_names;
|
|
54
|
+
std::vector<std::string> covariate_names;
|
|
55
|
+
std::vector<std::string> wave_names;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
struct LayoutEntry {
|
|
59
|
+
std::string name;
|
|
60
|
+
std::string block; // wave_effect, beta, persistence, loading, chol, threshold
|
|
61
|
+
int attitude = -1; // 0-based, or -1
|
|
62
|
+
int index2 = -1; // wave, covariate, source attitude, factor, or cut
|
|
63
|
+
char is_free = 1;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
struct Layout {
|
|
67
|
+
int n_full = 0;
|
|
68
|
+
int n_free = 0;
|
|
69
|
+
std::vector<LayoutEntry> entries;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
Layout make_layout(const ModelSpec& spec);
|
|
73
|
+
|
|
74
|
+
// One person. Waves outside [first_wave, last_wave] are not in the
|
|
75
|
+
// recursion. A wave inside the window with interviewed[t] == 0 is a gap:
|
|
76
|
+
// no item in that wave contributes a probability, and the latent state is
|
|
77
|
+
// simulated forward. Covariates on a gap are still multiplied in, as in
|
|
78
|
+
// preplik12. Store a zero regressor for a wave the data file never
|
|
79
|
+
// contained; that is the zeroed Fortran slot.
|
|
80
|
+
struct Person {
|
|
81
|
+
int first_wave = 0; // 0-based, inclusive
|
|
82
|
+
int last_wave = -1; // inclusive; last < first means "drop this person"
|
|
83
|
+
std::vector<unsigned char> interviewed; // length n_waves
|
|
84
|
+
std::vector<int> category; // (attitude * n_waves + wave), 0 = missing
|
|
85
|
+
std::vector<double> covariates; // (wave * n_covariates + k)
|
|
86
|
+
double weight = 1.0;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
struct Problem {
|
|
90
|
+
ModelSpec spec;
|
|
91
|
+
Layout layout;
|
|
92
|
+
std::vector<Person> persons;
|
|
93
|
+
DrawOptions draws;
|
|
94
|
+
// Values of every coefficient, including the fixed ones. Free entries
|
|
95
|
+
// are overwritten by the argument of evaluate().
|
|
96
|
+
std::vector<double> theta_full;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
struct Evaluation {
|
|
100
|
+
double objective = 0.0; // sum of weighted person log-likelihoods, minus penalty
|
|
101
|
+
double penalty = 0.0; // non-positive-definite contemporaneous correlation
|
|
102
|
+
int n_persons_used = 0;
|
|
103
|
+
std::vector<double> person_loglik; // length n_persons; 0 if the person was dropped
|
|
104
|
+
std::vector<double> gradient; // d(objective) / d(theta_full[grad_columns])
|
|
105
|
+
std::vector<double> person_score; // row-major n_persons * gradient.size(), if requested
|
|
106
|
+
std::string error;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
struct EvaluateRequest {
|
|
110
|
+
Numerics numerics = Numerics::kStable;
|
|
111
|
+
// Reproduce the ghk1 overwrite when the contemporaneous correlation is
|
|
112
|
+
// not the identity. Ignored when that correlation is the identity, which
|
|
113
|
+
// is the published specification: both versions then return the same
|
|
114
|
+
// conditional moments.
|
|
115
|
+
bool fortran_ghk = false;
|
|
116
|
+
int n_threads = 1;
|
|
117
|
+
bool want_scores = false;
|
|
118
|
+
// Which full-coefficient positions to differentiate, in gradient order.
|
|
119
|
+
// Empty means "no gradient".
|
|
120
|
+
std::vector<int> grad_columns;
|
|
121
|
+
// Optional standard normal draws. When non-empty, DrawOptions::kind is
|
|
122
|
+
// ignored and these values are used (the Fortran comparison does this).
|
|
123
|
+
// factor: ((person * n_draws + draw) * n_factors + factor)
|
|
124
|
+
// shock: (((person * n_draws + draw) * n_attitudes + attitude) * n_waves + wave)
|
|
125
|
+
std::vector<float> factor_draws;
|
|
126
|
+
std::vector<float> shock_draws;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// theta_free is laid out in the order of the free coefficients. It replaces
|
|
130
|
+
// problem.theta_full at those positions for this evaluation only.
|
|
131
|
+
Evaluation evaluate(const Problem& problem, const std::vector<double>& theta_free,
|
|
132
|
+
const EvaluateRequest& request);
|
|
133
|
+
|
|
134
|
+
// Same evaluation against an explicit full coefficient vector.
|
|
135
|
+
Evaluation evaluate_full(const Problem& problem, const std::vector<double>& theta_full,
|
|
136
|
+
const EvaluateRequest& request);
|
|
137
|
+
|
|
138
|
+
struct NaturalParameters {
|
|
139
|
+
// Row-major n_attitudes * n_attitudes. Row is the receiving attitude,
|
|
140
|
+
// column is the source, which is Fortran rho(to, from) and the matrix
|
|
141
|
+
// that multiplies the lagged state.
|
|
142
|
+
std::vector<double> persistence;
|
|
143
|
+
// For each attitude, the K+1 cutpoints, with the normalised ends
|
|
144
|
+
// (-100, 0, ..., +100).
|
|
145
|
+
std::vector<std::vector<double>> cutpoints;
|
|
146
|
+
std::vector<double> loadings; // attitude-major within factor, factor-major: (f * J + j)
|
|
147
|
+
std::vector<double> wave_effects; // (attitude * n_waves + wave), 0 if omitted
|
|
148
|
+
std::vector<double> beta; // (attitude * n_covariates + k)
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
NaturalParameters natural_parameters(const ModelSpec& spec, const std::vector<double>& theta_full);
|
|
152
|
+
|
|
153
|
+
// Impulse response of the persistence matrix. response[(h * J + to) * J + from]
|
|
154
|
+
// is the effect, h steps of one time unit later, of a unit innovation in
|
|
155
|
+
// attitude `from` on attitude `to`. Horizon 0 is the identity.
|
|
156
|
+
struct ImpulseResponse {
|
|
157
|
+
int horizon = 0;
|
|
158
|
+
int n_attitudes = 0;
|
|
159
|
+
std::vector<double> response;
|
|
160
|
+
double spectral_radius = 0.0;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
ImpulseResponse impulse_response(const std::vector<double>& persistence_row_major,
|
|
164
|
+
int n_attitudes, int horizon);
|
|
165
|
+
|
|
166
|
+
// Share of the long-run variance of attitude `to` that is attributable to
|
|
167
|
+
// an innovation in attitude `from`, under the equal-spacing recursion
|
|
168
|
+
// y = R y_lag + innovation (or the same recursion for the shock alone).
|
|
169
|
+
// shares[to * J + from]. Empty if the spectral radius is not strictly below 1.
|
|
170
|
+
struct InfluenceShares {
|
|
171
|
+
std::vector<double> share; // row-major to, from
|
|
172
|
+
std::vector<double> long_run_variance;
|
|
173
|
+
bool stationary = false;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
InfluenceShares influence_shares(const std::vector<double>& persistence_row_major,
|
|
177
|
+
int n_attitudes);
|
|
178
|
+
|
|
179
|
+
struct VarianceComponent {
|
|
180
|
+
std::vector<double> covariate; // variance of wave effect + x beta
|
|
181
|
+
std::vector<double> wave_effect; // variance of the wave effect
|
|
182
|
+
std::vector<double> factor; // sum of squared loadings
|
|
183
|
+
std::vector<double> shock; // person-average finite-horizon contribution
|
|
184
|
+
std::vector<double> total;
|
|
185
|
+
std::string note;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Replication variance decomposition. Moments skip each person's first
|
|
189
|
+
// observed wave and then keep every later wave in the window, including a
|
|
190
|
+
// gap. The shock piece fills gap-scaled persistence in person order and
|
|
191
|
+
// leaves a destination wave at zero until some included person has visited it.
|
|
192
|
+
VarianceComponent variance_decomposition(const Problem& problem,
|
|
193
|
+
const std::vector<double>& theta_full);
|
|
194
|
+
|
|
195
|
+
struct SimulatedPanel {
|
|
196
|
+
int n_persons = 0;
|
|
197
|
+
int n_waves = 0;
|
|
198
|
+
int n_attitudes = 0;
|
|
199
|
+
int n_covariates = 0;
|
|
200
|
+
std::vector<int> id; // length n_persons * n_waves, person-major
|
|
201
|
+
std::vector<int> wave; // 1-based wave index
|
|
202
|
+
std::vector<double> time;
|
|
203
|
+
std::vector<double> covariates;
|
|
204
|
+
std::vector<int> outcome; // (row * n_attitudes + attitude), categories 1..K
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Draw a panel from the structural model (not from the GHK truncation).
|
|
208
|
+
// Covariates are N(0, 1), except intercept columns which are 1.
|
|
209
|
+
SimulatedPanel simulate_panel(const ModelSpec& spec, const std::vector<double>& theta_full,
|
|
210
|
+
int n_persons, std::uint64_t seed);
|
|
211
|
+
|
|
212
|
+
// Category probabilities for every person-wave in `persons`, averaged over
|
|
213
|
+
// `n_draws` structural paths (Rao-Blackwellised interval probabilities).
|
|
214
|
+
// probabilities layout:
|
|
215
|
+
// ((person * n_waves + wave) * n_attitudes + attitude) * max_categories + (k-1)
|
|
216
|
+
// max_categories is the maximum of n_categories. Unused category slots are 0.
|
|
217
|
+
// `mean_latent` is the average simulated latent attitude, same person/wave/attitude
|
|
218
|
+
// order without the category dimension.
|
|
219
|
+
struct Prediction {
|
|
220
|
+
int max_categories = 0;
|
|
221
|
+
std::vector<double> probability;
|
|
222
|
+
std::vector<double> mean_latent;
|
|
223
|
+
std::string error;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
Prediction predict_probabilities(const Problem& problem, const std::vector<double>& theta_full,
|
|
227
|
+
int n_draws, std::uint64_t seed, int n_threads);
|
|
228
|
+
|
|
229
|
+
} // namespace attitudedyn
|
|
230
|
+
|
|
231
|
+
#endif
|